Skip to content

Commit 9575a67

Browse files
author
Taras Nikulin
committed
Added some comments to playground
1 parent b6c5b82 commit 9575a67

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Dijkstra Algorithm/Dijkstra.playground/Contents.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ func createNotConnectedVertices() {
1414

1515
func setupConnections() {
1616
for vertex in vertices {
17+
//the amount of edges each vertex can have
1718
let randomEdgesCount = arc4random_uniform(4) + 1
1819
for _ in 0..<randomEdgesCount {
20+
//randomize weight value from 0 to 9
1921
let randomWeight = Double(arc4random_uniform(10))
2022
let neighborVertex = randomVertex(except: vertex)
23+
24+
//we need this check to set only one connection between two equal pairs of vertices
2125
if vertex.neighbors.contains(where: { $0.0 == neighborVertex }) {
2226
continue
2327
}
28+
//creating neighbors and setting them
2429
let neighbor1 = (neighborVertex, randomWeight)
2530
let neighbor2 = (vertex, randomWeight)
2631
vertex.neighbors.append(neighbor1)
@@ -53,9 +58,15 @@ let dijkstra = Dijkstra(vertices: vertices)
5358
//decide which vertex will be the starting one
5459
let startVertex = randomVertex()
5560

61+
let startTime = Date()
62+
5663
//ask algorithm to find shortest paths from start vertex to all others
5764
dijkstra.findShortestPaths(from: startVertex)
5865

66+
let endTime = Date()
67+
68+
print("calculation time is = \((endTime.timeIntervalSince(startTime))) sec")
69+
5970
//printing results
6071
let destinationVertex = randomVertex(except: startVertex)
6172
print(destinationVertex.pathLengthFromStart)

0 commit comments

Comments
 (0)