-
Notifications
You must be signed in to change notification settings - Fork 1
/
dijkstra.R
142 lines (122 loc) · 2.64 KB
/
dijkstra.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
############
### Implementation (It is considered that the initial node is the 1 and the end one the only one that has no leaving edges)
############
M <- 10000 #value big enough (change the number in order to suit your needs)
dijkstraSolver <- function(weightMatrix){
#Step 1: initialization
processedNodes <- c()
notProcessedNodes <- c(1:nrow(weightMatrix))
pi <- rep(+Inf, nrow(weightMatrix))
E <- rep(-1, nrow(weightMatrix))
#Step 2: first node initialization
processedNodes <- c(processedNodes, 1)
notProcessedNodes <- notProcessedNodes[-which(notProcessedNodes==1)]
pi[1] = 0
E[1] = 0
for(j in 1:ncol(weightMatrix)){
if(is.na(weightMatrix[1, j]) == FALSE){
pi[j] = A[1, j]
E[j] = 1
}
}
#Step 3: processing of the rest of nodes
while(length(notProcessedNodes) > 0){
#not processed node with least weight is searched
min = M
index = -1
for(i in notProcessedNodes){
if(pi[i] < min){
min = pi[i]
index = i
}
}
#lists of processed and not processed nodes are updated
processedNodes <- c(processedNodes, index)
notProcessedNodes <- notProcessedNodes[-which(notProcessedNodes==index)]
#vectors E and PI are updated
for(j in 1:ncol(weightMatrix)){
if(is.na(weightMatrix[index, j]) == FALSE && pi[index]+weightMatrix[index, j] < pi[j]){
pi[j] = pi[index]+weightMatrix[index, j]
E[j] = index
}
}
}
#preparation of the solution to return
endNode <- -1
flag = FALSE
for(i in nrow(weightMatrix)){
for(j in ncol(weightMatrix)){
if(is.na(weightMatrix[i, j]) == FALSE){
flag = TRUE
break
}
}
if(flag == FALSE){
endNode <- i
break
}else{
flag = FALSE
}
}
solutionRoute <- c(endNode)
while(1 %in% solutionRoute == FALSE){
solutionRoute <- c(solutionRoute, E[solutionRoute[length(solutionRoute)]])
}
return(list('solution' = rev(solutionRoute), 'objective value' = pi[endNode]))
}
############
### Example 1
############
nodes<-15
#weights:
A<-matrix(nrow=nodes,ncol=nodes)
A[1,2]<-1
A[1,3]<-9
A[2,5]<-1
A[2,4]<-2
A[2,3]<-5
A[3, 6]<-3
A[4, 8]<-6
A[4,9]<-7
A[4,6]<-7
A[4,3]<-9
A[5,7]<-7
A[5,8]<-1
A[5,4]<-7
A[6,11]<-2
A[6,10]<-6
A[7,13]<-9
A[7,8]<-2
A[8,12]<-8
A[8,9]<-3
A[9,14]<-5
A[9,11]<-9
A[9,6]<-6
A[10,11]<-9
A[10,15]<-3
A[11,14]<-5
A[11,15]<-1
A[12,14]<-1
A[12,9]<-3
A[13,12]<-3
A[13,14]<-3
A[14,15]<-9
A
############
### Example 2
############
nodes<-5
#weights:
A<-matrix(nrow=nodes,ncol=nodes)
A[1, 2]<-2
A[1, 3]<-2
A[1, 4]<-3
A[2, 5]<-4
A[3, 2]<-4
A[3, 5]<-3
A[4, 3]<-1
A[4, 5]<-0
############
### Execution
############
dijkstraSolver(A)