@@ -5,69 +5,90 @@ import (
55 "math"
66)
77
8+ var graph map [string ]map [string ]float64
9+ var costs map [string ]float64
10+ var parents map [string ]string
11+ var processed []string
12+
813func main () {
9- graph := make (map [string ]map [string ]int )
10- graph ["start" ] = map [string ]int {}
14+ // the graph
15+ graph = make (map [string ]map [string ]float64 )
16+ graph ["start" ] = make (map [string ]float64 )
1117 graph ["start" ]["a" ] = 6
1218 graph ["start" ]["b" ] = 2
1319
14- graph ["a" ] = map [string ]int {}
15- graph ["a" ]["finish " ] = 1
20+ graph ["a" ] = make ( map [string ]float64 )
21+ graph ["a" ]["fin " ] = 1
1622
17- graph ["b" ] = map [string ]int {}
23+ graph ["b" ] = make ( map [string ]float64 )
1824 graph ["b" ]["a" ] = 3
19- graph ["b" ]["finish " ] = 5
25+ graph ["b" ]["fin " ] = 5
2026
21- graph ["finish " ] = map [string ]int {}
27+ graph ["fin " ] = make ( map [string ]float64 )
2228
23- costs , parents := findShortestPath (graph , "start" , "finish" )
24- fmt .Println (costs , parents )
25- }
29+ // the costs table
30+ costs = make (map [string ]float64 )
31+ costs ["a" ] = 6
32+ costs ["b" ] = 2
33+ costs ["fin" ] = math .Inf (1 )
2634
27- // Finds shortest path using dijkstra algorithm
28- func findShortestPath (graph map [string ]map [string ]int , startNode string , finishNode string ) (map [string ]int , map [string ]string ) {
29- costs := make (map [string ]int )
30- costs [finishNode ] = math .MaxInt32
35+ // the parents table
36+ parents = make (map [string ]string )
37+ parents ["a" ] = "start"
38+ parents ["b" ] = "start"
39+ parents ["fin" ] = ""
3140
32- parents := make (map [string ]string )
33- parents [finishNode ] = ""
41+ // Find the lowest-cost node that you haven't processed yet.
42+ node := find_lowest_cost_node (costs )
43+ // If you've processed all the nodes, this while loop is done.
3444
35- processed := make (map [string ]bool )
45+ for node != "" {
46+ cost := costs [node ]
47+ // Go through all the neighbors of this node.
48+ neighbors := graph [node ]
3649
37- // Initialization of costs and parents
38- for node , cost := range graph [startNode ] {
39- costs [node ] = cost
40- parents [node ] = startNode
41- }
42-
43- lowestCostNode := findLowestCostNode (costs , processed )
44- for ; lowestCostNode != "" ; {
45- // Calculation costs for neighbours
46- for node , cost := range graph [lowestCostNode ] {
47- newCost := costs [lowestCostNode ] + cost
48- if newCost < costs [node ] {
49- // Set new cost for this node
50- costs [node ] = newCost
51- parents [node ] = lowestCostNode
50+ for node , _ := range neighbors {
51+ new_cost := cost + neighbors [node ]
52+ // If it's cheaper to get to this neighbor by going through this node...
53+ if costs [node ] > new_cost {
54+ // ... update the cost for this node.
55+ costs [node ] = new_cost
56+ // This node becomes the new parent for this neighbor.
57+ parents [node ] = node
5258 }
5359 }
54-
55- processed [lowestCostNode ] = true
56- lowestCostNode = findLowestCostNode (costs , processed )
60+ // Mark the node as processed.
61+ processed = append (processed , node )
62+ // Find the next node to process, and loop.
63+ node = find_lowest_cost_node (costs )
5764 }
5865
59- return costs , parents
66+ fmt .Println (costs )
67+
6068}
6169
62- func findLowestCostNode (costs map [string ]int , processed map [string ]bool ) string {
63- lowestCost := math .MaxInt32
64- lowestCostNode := ""
65- for node , cost := range costs {
66- if _ , inProcessed := processed [node ]; cost < lowestCost && ! inProcessed {
67- lowestCost = cost
68- lowestCostNode = node
70+ func find_lowest_cost_node (costs map [string ]float64 ) string {
71+ lowest_cost := math .Inf (1 )
72+ lowest_cost_node := ""
73+
74+ for node , _ := range costs {
75+ // fmt.Println("Node:", node, "Value:", value)
76+ cost := costs [node ]
77+ // If it's the lowest cost so far and hasn't been processed yet...
78+ if cost < lowest_cost && ! contains (processed , node ) {
79+ // ... set it as the new lowest-cost node.
80+ lowest_cost = cost
81+ lowest_cost_node = node
6982 }
7083 }
84+ return lowest_cost_node
85+ }
7186
72- return lowestCostNode
87+ func contains (s []string , e string ) bool {
88+ for _ , a := range s {
89+ if a == e {
90+ return true
91+ }
92+ }
93+ return false
7394}
0 commit comments