Skip to content

Commit 366d57c

Browse files
committed
Update 01_dijkstras_algorithm.go
1 parent 7d27d15 commit 366d57c

File tree

1 file changed

+44
-65
lines changed

1 file changed

+44
-65
lines changed

07_dijkstras_algorithm/golang/01_dijkstras_algorithm.go

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,90 +5,69 @@ 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-
138
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{}
1711
graph["start"]["a"] = 6
1812
graph["start"]["b"] = 2
1913

20-
graph["a"] = make(map[string]float64)
21-
graph["a"]["fin"] = 1
14+
graph["a"] = map[string]int{}
15+
graph["a"]["finish"] = 1
2216

23-
graph["b"] = make(map[string]float64)
17+
graph["b"] = map[string]int{}
2418
graph["b"]["a"] = 3
25-
graph["b"]["fin"] = 5
19+
graph["b"]["finish"] = 5
2620

27-
graph["fin"] = make(map[string]float64)
21+
graph["finish"] = map[string]int{}
2822

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+
}
3426

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
4031

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] = ""
4434

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)
4936

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
5852
}
5953
}
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-
}
6554

66-
fmt.Println(costs)
55+
processed[lowestCostNode] = true
56+
lowestCostNode = findLowestCostNode(costs, processed)
57+
}
6758

59+
return costs, parents
6860
}
6961

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
8269
}
8370
}
84-
return lowest_cost_node
85-
}
8671

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
9473
}

0 commit comments

Comments
 (0)