@@ -5,90 +5,69 @@ 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
-
13
8
func main () {
14
- // the graph
15
- graph = make (map [string ]map [string ]float64 )
16
- graph ["start" ] = make (map [string ]float64 )
9
+ graph := make (map [string ]map [string ]int )
10
+ graph ["start" ] = map [string ]int {}
17
11
graph ["start" ]["a" ] = 6
18
12
graph ["start" ]["b" ] = 2
19
13
20
- graph ["a" ] = make ( map [string ]float64 )
21
- graph ["a" ]["fin " ] = 1
14
+ graph ["a" ] = map [string ]int {}
15
+ graph ["a" ]["finish " ] = 1
22
16
23
- graph ["b" ] = make ( map [string ]float64 )
17
+ graph ["b" ] = map [string ]int {}
24
18
graph ["b" ]["a" ] = 3
25
- graph ["b" ]["fin " ] = 5
19
+ graph ["b" ]["finish " ] = 5
26
20
27
- graph ["fin " ] = make ( map [string ]float64 )
21
+ graph ["finish " ] = map [string ]int {}
28
22
29
- // the costs table
30
- costs = make (map [string ]float64 )
31
- costs ["a" ] = 6
32
- costs ["b" ] = 2
33
- costs ["fin" ] = math .Inf (1 )
23
+ costs , parents := findShortestPath (graph , "start" , "finish" )
24
+ fmt .Println (costs , parents )
25
+ }
34
26
35
- // the parents table
36
- parents = make (map [string ]string )
37
- parents ["a" ] = "start"
38
- parents ["b" ] = "start"
39
- parents ["fin" ] = ""
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
40
31
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.
32
+ parents := make (map [string ]string )
33
+ parents [finishNode ] = ""
44
34
45
- for node != "" {
46
- cost := costs [node ]
47
- // Go through all the neighbors of this node.
48
- neighbors := graph [node ]
35
+ processed := make (map [string ]bool )
49
36
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
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
58
52
}
59
53
}
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 )
64
- }
65
54
66
- fmt .Println (costs )
55
+ processed [lowestCostNode ] = true
56
+ lowestCostNode = findLowestCostNode (costs , processed )
57
+ }
67
58
59
+ return costs , parents
68
60
}
69
61
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
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
82
69
}
83
70
}
84
- return lowest_cost_node
85
- }
86
71
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
72
+ return lowestCostNode
94
73
}
0 commit comments