From 3a50470e3d7ed011b924b27566c908753dd7800d Mon Sep 17 00:00:00 2001 From: Evgeny Samsonov Date: Tue, 12 Nov 2019 18:18:00 +0300 Subject: [PATCH] Add dijkstras algorithm in golang (#118) * Golang dijkstra algorithm * New line in end of file --- .../Golang/01_dijkstras_algorithm.go | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 07_dijkstras_algorithm/Golang/01_dijkstras_algorithm.go diff --git a/07_dijkstras_algorithm/Golang/01_dijkstras_algorithm.go b/07_dijkstras_algorithm/Golang/01_dijkstras_algorithm.go new file mode 100644 index 0000000..b25fb00 --- /dev/null +++ b/07_dijkstras_algorithm/Golang/01_dijkstras_algorithm.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "math" +) + +func main() { + graph := make(map[string]map[string]int) + graph["start"] = map[string]int{} + graph["start"]["a"] = 6 + graph["start"]["b"] = 2 + + graph["a"] = map[string]int{} + graph["a"]["finish"] = 1 + + graph["b"] = map[string]int{} + graph["b"]["a"] = 3 + graph["b"]["finish"] = 5 + + graph["finish"] = map[string]int{} + + costs, parents := findShortestPath(graph, "start", "finish") + fmt.Println(costs, parents) +} + +// Finds shortest path using dijkstra algorithm +func findShortestPath(graph map[string]map[string]int, startNode string, finishNode string) (map[string]int, map[string]string) { + costs := make(map[string]int) + costs[finishNode] = math.MaxInt32 + + parents := make(map[string]string) + parents[finishNode] = "" + + processed := make(map[string]bool) + + // Initialization of costs and parents + for node, cost := range graph[startNode] { + costs[node] = cost + parents[node] = startNode + } + + lowestCostNode := findLowestCostNode(costs, processed) + for ; lowestCostNode != "" ; { + // Calculation costs for neighbours + for node, cost := range graph[lowestCostNode] { + newCost := costs[lowestCostNode] + cost + if newCost < costs[node] { + // Set new cost for this node + costs[node] = newCost + parents[node] = lowestCostNode + } + } + + processed[lowestCostNode] = true + lowestCostNode = findLowestCostNode(costs, processed) + } + + return costs, parents +} + +func findLowestCostNode(costs map[string]int, processed map[string]bool) string { + lowestCost := math.MaxInt32 + lowestCostNode := "" + for node, cost := range costs { + if _, inProcessed := processed[node]; cost < lowestCost && !inProcessed { + lowestCost = cost + lowestCostNode = node + } + } + + return lowestCostNode +}