forked from AllAlgorithms/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.swift
53 lines (42 loc) · 1.66 KB
/
dijkstra.swift
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
struct Constants {
static let numVertices:Int = 9
}
func minimumDistance(_ distances: [Int], _ shortestPathSet: [Bool]) -> Int {
var minimum = Int.max
var minimumIndex = -1
for vertex in 0..<Constants.numVertices {
if !shortestPathSet[vertex] && distances[vertex] <= minimum {
minimum = distances[vertex]
minimumIndex = vertex
}
}
return minimumIndex
}
func dijkstra(graph: [[Int]], source: Int) {
var distances: [Int] = Array(repeating: Int.max, count: Constants.numVertices)
var shortestPathSet: [Bool] = Array(repeating: false, count: Constants.numVertices)
distances[source] = 0
for _ in 0..<Constants.numVertices-1 {
let u = minimumDistance(distances, shortestPathSet)
shortestPathSet[u] = true
for vertex in 0..<Constants.numVertices {
if !shortestPathSet[vertex] && graph[u][vertex] != 0 && distances[u] != Int.max && distances[u] + graph[u][vertex] < distances[vertex] {
distances[vertex] = distances[u] + graph[u][vertex]
}
}
}
print("Vertex Distance from Source")
for i in 0..<Constants.numVertices {
print("\(i) \(distances[i])")
}
}
dijkstra(graph: [[0, 4, 0, 0, 0, 0, 0, 8, 0],
[4, 0, 8, 0, 0, 0, 0, 11, 0],
[0, 8, 0, 7, 0, 4, 0, 0, 2],
[0, 0, 7, 0, 9, 14, 0, 0, 0],
[0, 0, 0, 9, 0, 10, 0, 0, 0],
[0, 0, 4, 14, 10, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 2, 0, 1, 6],
[8, 11, 0, 0, 0, 0, 1, 0, 7],
[0, 0, 2, 0, 0, 0, 6, 7, 0]], source: 0)
//Referenced from https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/