n <- 1000
d <- runif(n*n)
d[d < 0.80] <- NA
d <- matrix(d,nrow=n,ncol=n)
diag(d) <- NA # no self-loops
d[upper.tri(d)] = t(d)[upper.tri(d)]
AdjMatrix2List <- function (d) {
head<-c()
tail<-c()
weight<-c()
a<-0
for(i in 1:n) {
for(j in 1:n){
if(!is.na(d[i,j])){
a<-a+1
head[a]<-i
tail[a]<-j
weight[a]<-d[i,j]
}
}
}
b<-cbind(head,tail, weight)
}
ds <- AdjMatrix2List(d)
str(d)
## num [1:1000, 1:1000] NA 0.873 NA 0.946 0.912 ...
str(ds)
## num [1:200636, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:3] "head" "tail" "weight"
head(ds)
## head tail weight
## [1,] 1 2 0.8731795
## [2,] 1 4 0.9459925
## [3,] 1 5 0.9115088
## [4,] 1 7 0.8882128
## [5,] 1 32 0.9788768
## [6,] 1 34 0.9678812
n <- 50
x <- round(runif(n)*1000)
y <- round(runif(n)*1000)
#plot(x,y,pch=16)
q <- c()
for (i in 1:n){
for (j in 1:n){
q <- append(q, sqrt((x[j]-x[i])^2+(y[j]-y[i])^2))
}
}
matrix3 <- matrix(q, nrow=50)
p <- AdjMatrix2List(matrix3)
v <- c()
for (i in 1:nrow(p)){
if (p[i,1] != p[i,2]){
v <- append(v, p[i,2])
}
}
dimnames(v) <- NULL
v1 <- matrix(1:50, ncol =1)
v <- matrix(v, ncol = 49, byrow = TRUE)
ds <- cbind(v1,v)
ds.mst <- msTreePrim(1:n, AdjMatrix2List(matrix3)) # nodes, arcs
str(ds.mst)
## List of 4
## $ tree.nodes : num [1:50] 1 12 11 2 29 25 10 16 30 31 ...
## $ tree.arcs : num [1:49, 1:3] 1 1 12 2 29 25 11 16 30 30 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr [1:3] "ept1" "ept2" "weight"
## $ stages : num 49
## $ stages.arcs: num [1:49] 1 2 3 4 5 6 7 8 9 10 ...
plot.mst <- function(arcList){
for (i in 1:nrow(arcList)){
segments(x0=x[arcList[i,1]], y0=y[arcList[i,1]], x1=x[arcList[i,2]], y1=y[arcList[i,2]])
}
}
plot(x,y,pch=16)
plot.mst(ds.mst$tree.arcs)
s.labels <- c('a','b','c','d','e','f','g','h','i','j')
s.nodes <- c(90,15,5,20,21,25,14,28,30,45)
#s.nodes1 <- c('-90','-15','-5','-20','-21','-25','-14','-28','-30','-45')
n <- 13
d1 <- NA
d1 <- matrix(d1,nrow=n,ncol=n)
d1[1,2] <- -90; d1[1,9] <- -90; d1[1,6] <- -90;
d1[2,3] <- -15;
d1[3,7] = -5;
d1[7,4] = -14;
d1[4,10] =-20; d1[4,8]=-20;
d1[4,5] <- -20;
d1[9,10]=-30;
d1[6,7]= -25;
d1[10,11] = -45
d1[5,12] = -21
d1[8,13] = -28
diag(d1) <- NA
ak <- AdjMatrix2List(d1)
zlb <- getShortestPathTree(1:13,ak,algorithm = "Bellman-Ford", directed = TRUE)
##
## Shortest path tree
## Algorithm: Bellman-Ford
## Stages: 12 | Time: 0.01
## ------------------------------
## head tail weight
## 1 2 -90
## 1 6 -90
## 1 9 -90
## 2 3 -15
## 4 5 -20
## 4 8 -20
## 4 10 -20
## 5 12 -21
## 6 7 -25
## 7 4 -14
## 8 13 -28
## 10 11 -45
## ------------------------------
## Total = -478
##
## Distances from source:
## ------------------------------
## source node distance
## 1 2 -90
## 1 3 -105
## 1 4 -129
## 1 5 -149
## 1 6 -90
## 1 7 -115
## 1 8 -149
## 1 9 -90
## 1 10 -149
## 1 11 -194
## 1 12 -170
## 1 13 -177
## ------------------------------
### 5.5
CT <- as.Date("2019-11-01")-min(zlb$distances)
print(paste("Earliest Overall Project Completion Time will be:", CT))
## [1] "Earliest Overall Project Completion Time will be: 2020-05-13"
EF <- c(90, 105, 110, 149, 170, 115, 129, 177, 120, 194)
n <- 11
d2 <- NA
d2 <- matrix(d2,nrow=n,ncol=n)
d2[11, 5] <- 0
d2[11,8] <- 0
d2[11,10] <- 0
d2[5,4] <- -21
d2[8,4] <- -28
d2[4,7] <- -20
d2[10,4] <- -45
d2[10,9] <- -45
d2[9,1] <- -30
d2[7,6] <- -14
d2[7,3] <- -14
d2[6,1] <- -25
d2[3,2] <- -5
d2[2,1] <- -15
diag(d2) <- NA
ak1 <- AdjMatrix2List(d2)
zlb1 <- getShortestPathTree(1:11,ak1,algorithm = "Bellman-Ford", source.node = 11, directed = TRUE)
##
## Shortest path tree
## Algorithm: Bellman-Ford
## Stages: 10 | Time: 0
## ------------------------------
## head tail weight
## 3 2 -5
## 4 7 -20
## 6 1 -25
## 7 3 -14
## 7 6 -14
## 10 4 -45
## 10 9 -45
## 11 5 0
## 11 8 0
## 11 10 0
## ------------------------------
## Total = -168
##
## Distances from source:
## ------------------------------
## source node distance
## 11 1 -104
## 11 2 -84
## 11 3 -79
## 11 4 -45
## 11 5 0
## 11 6 -79
## 11 7 -65
## 11 8 0
## 11 9 -45
## 11 10 0
## ------------------------------
LF <- c(194 + zlb1$distances)
LF <- LF[1:10]
LS <- LF - s.nodes
slack <- LF - EF