Created
January 25, 2018 21:10
-
-
Save nthState/abe4ef418147115253e48ad45ccda114 to your computer and use it in GitHub Desktop.
Dijkstra Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
My attempt at Dijkstra Algorithm | |
https://www.youtube.com/watch?v=0nVYi3o161A | |
https://www.youtube.com/watch?v=5GT5hYzjNoo | |
*/ | |
import UIKit | |
class Node : CustomDebugStringConvertible, Hashable, Equatable | |
{ | |
var name:String! | |
var processed:Bool = false | |
var connections:[Node:Int] = [Node:Int]() | |
func connectsTo(node:Node, weight:Int) | |
{ | |
connections[node] = weight | |
} | |
init(name:String) | |
{ | |
self.name = name | |
} | |
var hashValue: Int { return name.hash } | |
var debugDescription: String { return "\(name)" } | |
static func ==(lhs: Node, rhs: Node) -> Bool | |
{ | |
return lhs.hashValue == rhs.hashValue | |
} | |
} | |
let a = Node(name: "a") | |
let b = Node(name: "b") | |
let c = Node(name: "c") | |
let d = Node(name: "d") | |
let e = Node(name: "e") | |
let f = Node(name: "f") | |
let g = Node(name: "g") | |
let h = Node(name: "h") | |
//a.connectsTo(node: b, weight: 8) | |
//a.connectsTo(node: d, weight: 5) | |
//a.connectsTo(node: c, weight: 2) | |
// | |
//b.connectsTo(node: a, weight: 8) | |
//b.connectsTo(node: d, weight: 2) | |
//b.connectsTo(node: f, weight: 13) | |
// | |
//c.connectsTo(node: a, weight: 2) | |
//c.connectsTo(node: d, weight: 2) | |
//c.connectsTo(node: e, weight: 5) | |
// | |
//d.connectsTo(node: a, weight: 5) | |
//d.connectsTo(node: b, weight: 2) | |
//d.connectsTo(node: f, weight: 6) | |
//d.connectsTo(node: g, weight: 3) | |
//d.connectsTo(node: e, weight: 1) | |
//d.connectsTo(node: c, weight: 2) | |
// | |
//e.connectsTo(node: c, weight: 5) | |
//e.connectsTo(node: d, weight: 1) | |
//e.connectsTo(node: g, weight: 1) | |
// | |
//f.connectsTo(node: b, weight: 13) | |
//f.connectsTo(node: d, weight: 6) | |
//f.connectsTo(node: g, weight: 2) | |
//f.connectsTo(node: h, weight: 3) | |
// | |
//g.connectsTo(node: e, weight: 1) | |
//g.connectsTo(node: d, weight: 3) | |
//g.connectsTo(node: f, weight: 2) | |
//g.connectsTo(node: h, weight: 6) | |
// | |
//h.connectsTo(node: g, weight: 6) | |
//h.connectsTo(node: f, weight: 3) | |
// | |
//var nodes:[Node] = [Node]() | |
//nodes.append(a) | |
//nodes.append(b) | |
//nodes.append(c) | |
//nodes.append(d) | |
//nodes.append(e) | |
//nodes.append(f) | |
//nodes.append(g) | |
//nodes.append(h) | |
// ------new | |
a.connectsTo(node: b, weight: 3) | |
a.connectsTo(node: c, weight: 5) | |
a.connectsTo(node: d, weight: 6) | |
b.connectsTo(node: a, weight: 3) | |
b.connectsTo(node: d, weight: 2) | |
c.connectsTo(node: a, weight: 5) | |
c.connectsTo(node: d, weight: 2) | |
c.connectsTo(node: e, weight: 6) | |
c.connectsTo(node: f, weight: 3) | |
c.connectsTo(node: g, weight: 7) | |
d.connectsTo(node: b, weight: 2) | |
d.connectsTo(node: a, weight: 6) | |
d.connectsTo(node: c, weight: 2) | |
d.connectsTo(node: f, weight: 9) | |
e.connectsTo(node: c, weight: 6) | |
e.connectsTo(node: f, weight: 5) | |
e.connectsTo(node: g, weight: 2) | |
f.connectsTo(node: c, weight: 3) | |
f.connectsTo(node: d, weight: 9) | |
f.connectsTo(node: e, weight: 5) | |
f.connectsTo(node: g, weight: 1) | |
g.connectsTo(node: c, weight: 7) | |
g.connectsTo(node: e, weight: 2) | |
g.connectsTo(node: f, weight: 1) | |
var nodes:[Node] = [Node]() | |
nodes.append(a) | |
nodes.append(b) | |
nodes.append(c) | |
nodes.append(d) | |
nodes.append(e) | |
nodes.append(f) | |
nodes.append(g) | |
var nodeWeights:[Int] = [Int]() | |
for node in nodes { | |
nodeWeights.append(Int.max) | |
} | |
var froms:[Node] = [Node]() | |
for node in nodes { | |
froms.append(a) | |
} | |
func shortestPath() | |
{ | |
var next:Node? = a | |
var runningWeight:Int = 0 | |
while next != nil | |
{ | |
let outerNode = next! | |
print("Checking: \(outerNode)") | |
outerNode.processed = true | |
for (node, weight) in outerNode.connections | |
{ | |
if let index = nodes.index(of: node) | |
{ | |
let currentWeight = nodeWeights[index] | |
if (weight + runningWeight) < currentWeight | |
{ | |
nodeWeights[index] = weight + runningWeight | |
froms[index] = outerNode | |
} | |
} | |
} | |
var smallest:Int = Int.max | |
for (index, weight) in nodeWeights.enumerated() | |
{ | |
//print("index: \(index) \(weight)") | |
let tmp = nodes[index] | |
if weight < smallest && tmp.processed == false | |
{ | |
next = tmp | |
smallest = weight | |
} | |
} | |
print("smallest: \(smallest) \(next)") | |
//print("i: \(i)") | |
print("Nodes: \(nodes)") | |
print("Weights: \(nodeWeights)") | |
print("Froms: \(froms)") | |
runningWeight = smallest | |
print("---------------") | |
let finish = nodes.contains(where: { (n:Node) -> Bool in | |
return n.processed == false | |
}) | |
if finish == false | |
{ | |
next = nil | |
} | |
} | |
} | |
shortestPath() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment