Skip to content

Commit 773507a

Browse files
Merge pull request #43 from ayaanlehashi11/master
dijkstra's algorithm
2 parents 5b6f832 + 8844fe6 commit 773507a

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

graph/spanning_tree/dijkstra.swift

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
class Node : CustomStringConvertible {
2+
// unique identifier required for each node
3+
var identifier : Int
4+
var distance : Int = Int.max
5+
var edges = [Edge]()
6+
var visited = false
7+
8+
var description: String {
9+
var edgesString = String()
10+
edges.forEach{ edgesString.append($0.description)}
11+
return "{ Node, identifier: \(identifier.description) + Edges: \(edgesString) + }"
12+
}
13+
14+
init(visited: Bool, identifier: Int, edges: [Edge]) {
15+
self.visited = visited
16+
self.identifier = identifier
17+
self.edges = edges
18+
}
19+
20+
static func == (lhs: Node, rhs: Node) -> Bool {
21+
return lhs.identifier == rhs.identifier
22+
}
23+
}
24+
25+
class Edge {
26+
var from: Node // does not actually need to be stored!
27+
var to: Node
28+
var weight: Int
29+
var description : String {
30+
return "{ Edge, from: \(from.identifier), to: \(to.identifier), weight: \(weight) }"
31+
32+
}
33+
init(to: Node, from: Node, weight: Int) {
34+
self.to = to
35+
self.weight = weight
36+
self.from = from
37+
}
38+
}
39+
40+
class Graph {
41+
var nodes: [Node] = []
42+
}
43+
44+
45+
// Complete the quickestWayUp function below.
46+
func setupGraphwith(edges: [[Int]]) -> Graph {
47+
let graph = Graph()
48+
49+
// create all the nodes
50+
// The first and last node need to be included, so need nodes from "to" and "from"
51+
let nodeNames = Set ( edges.map{ $0[0] } + edges.map{ $0[1]} )
52+
for node in nodeNames {
53+
let newNode = Node(visited: false, identifier: node, edges: [])
54+
graph.nodes.append(newNode)
55+
}
56+
57+
// create all the edges to link the nodes
58+
for edge in edges {
59+
if let fromNode = graph.nodes.first(where: { $0.identifier == edge[0] }) {
60+
if let toNode = graph.nodes.first(where: { $0.identifier == edge[1] }) {
61+
let forwardEdge = Edge(to: toNode, from: fromNode, weight: edge[2])
62+
fromNode.edges.append(forwardEdge)
63+
}
64+
}
65+
}
66+
return graph
67+
}
68+
69+
func shortestPath (source: Int, destination: Int, graph: Graph) -> Int {
70+
71+
var currentNode = graph.nodes.first{ $0.identifier == source }!
72+
currentNode.visited = true
73+
currentNode.distance = 0
74+
var toVisit = [Node]()
75+
toVisit.append(currentNode)
76+
while ( !toVisit.isEmpty) {
77+
toVisit = toVisit.filter{ $0.identifier != currentNode.identifier }
78+
currentNode.visited = true
79+
// Go to each adjacent vertex and update the path length
80+
for connectedEdge in currentNode.edges {
81+
let dist = currentNode.distance + connectedEdge.weight
82+
83+
if (dist < connectedEdge.to.distance) {
84+
85+
connectedEdge.to.distance = dist
86+
toVisit.append(connectedEdge.to)
87+
if (connectedEdge.to.visited == true) {
88+
89+
connectedEdge.to.visited = false
90+
}
91+
}
92+
}
93+
94+
currentNode.visited = true
95+
//set current node to the smallest vertex
96+
if !toVisit.isEmpty {
97+
currentNode = toVisit.min(by: { (a, b) -> Bool in
98+
return a.distance < b.distance
99+
})!
100+
}
101+
102+
if (currentNode.identifier == destination) {
103+
return currentNode.distance
104+
}
105+
}
106+
107+
return -1
108+
}

0 commit comments

Comments
 (0)