File tree Expand file tree Collapse file tree 1 file changed +11
-0
lines changed
Dijkstra Algorithm/Dijkstra.playground Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Original file line number Diff line number Diff line change @@ -14,13 +14,18 @@ func createNotConnectedVertices() {
14
14
15
15
func setupConnections( ) {
16
16
for vertex in vertices {
17
+ //the amount of edges each vertex can have
17
18
let randomEdgesCount = arc4random_uniform ( 4 ) + 1
18
19
for _ in 0 ..< randomEdgesCount {
20
+ //randomize weight value from 0 to 9
19
21
let randomWeight = Double ( arc4random_uniform ( 10 ) )
20
22
let neighborVertex = randomVertex ( except: vertex)
23
+
24
+ //we need this check to set only one connection between two equal pairs of vertices
21
25
if vertex. neighbors. contains ( where: { $0. 0 == neighborVertex } ) {
22
26
continue
23
27
}
28
+ //creating neighbors and setting them
24
29
let neighbor1 = ( neighborVertex, randomWeight)
25
30
let neighbor2 = ( vertex, randomWeight)
26
31
vertex. neighbors. append ( neighbor1)
@@ -53,9 +58,15 @@ let dijkstra = Dijkstra(vertices: vertices)
53
58
//decide which vertex will be the starting one
54
59
let startVertex = randomVertex ( )
55
60
61
+ let startTime = Date ( )
62
+
56
63
//ask algorithm to find shortest paths from start vertex to all others
57
64
dijkstra. findShortestPaths ( from: startVertex)
58
65
66
+ let endTime = Date ( )
67
+
68
+ print ( " calculation time is = \( ( endTime. timeIntervalSince ( startTime) ) ) sec " )
69
+
59
70
//printing results
60
71
let destinationVertex = randomVertex ( except: startVertex)
61
72
print ( destinationVertex. pathLengthFromStart)
You can’t perform that action at this time.
0 commit comments