@@ -5,69 +5,90 @@ import (
5
5
"math"
6
6
)
7
7
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
+
8
13
func 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 )
11
17
graph ["start" ]["a" ] = 6
12
18
graph ["start" ]["b" ] = 2
13
19
14
- graph ["a" ] = map [string ]int {}
15
- graph ["a" ]["finish " ] = 1
20
+ graph ["a" ] = make ( map [string ]float64 )
21
+ graph ["a" ]["fin " ] = 1
16
22
17
- graph ["b" ] = map [string ]int {}
23
+ graph ["b" ] = make ( map [string ]float64 )
18
24
graph ["b" ]["a" ] = 3
19
- graph ["b" ]["finish " ] = 5
25
+ graph ["b" ]["fin " ] = 5
20
26
21
- graph ["finish " ] = map [string ]int {}
27
+ graph ["fin " ] = make ( map [string ]float64 )
22
28
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 )
26
34
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" ] = ""
31
40
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.
34
44
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 ]
36
49
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
52
58
}
53
59
}
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 )
57
64
}
58
65
59
- return costs , parents
66
+ fmt .Println (costs )
67
+
60
68
}
61
69
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
69
82
}
70
83
}
84
+ return lowest_cost_node
85
+ }
71
86
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
73
94
}
0 commit comments