-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstras_algo.go
115 lines (97 loc) · 2.22 KB
/
dijkstras_algo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"container/heap"
"fmt"
"math"
)
type Item struct {
vertexIdx int
distance int
}
type MinHeap []Item
func (h MinHeap) Less(i, j int) bool {
return h[i].distance < h[j].distance
}
func (h MinHeap) Len() int {
return len(h)
}
func (h MinHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *MinHeap) Push(x interface{}) {
*h = append(*h, x.(Item))
}
func (h *MinHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}
// Time: O((v+e)*log(v)) | Space: O(v)
func dikstrasAlgorithm(start int, edges [][][]int) []int {
numberOfVertices := len(edges)
distances := make([]int, 0, numberOfVertices)
for range edges {
distances = append(distances, math.MaxInt32)
}
distances[start] = 0
minDistancesHeap := &MinHeap{}
heap.Push(minDistancesHeap, Item{vertexIdx: start, distance: 0})
// visited := make(map[int]bool)
for minDistancesHeap.Len() > 0 {
item := heap.Pop(minDistancesHeap).(Item)
vertex, currentMinDistance := item.vertexIdx, item.distance
// if _, found := visited[vertex]; found {
// continue
// }
// visited[vertex] = true
for _, edge := range edges[vertex] {
adjacentVertexIdx, distanceToAdjacentVertex := edge[0], edge[1]
// if _, found := visited[adjacentVertexIdx]; found {
// continue
// }
newPathDistance := currentMinDistance + distanceToAdjacentVertex
currentAdjacentVertexDistance := distances[adjacentVertexIdx]
if newPathDistance < currentAdjacentVertexDistance {
distances[adjacentVertexIdx] = newPathDistance
heap.Push(minDistancesHeap, Item{vertexIdx: adjacentVertexIdx, distance: newPathDistance})
}
}
}
return distances
}
func main() {
edges := [][][]int{
// for vertex 0
{
{1, 2}, {3, 6}, // {vertexIdx, distance} ex {1, 2} distance from vertex 0 to vertex 1 is 2
},
// for vertex 1
{
{0, 2}, {2, 5},
},
// for vertex 2
{
{1, 5}, {3, 7}, {4, 6}, {5, 9},
},
// for vertex 3
{
{0, 6}, {2, 7}, {4, 10},
},
// for vertex 4
{
{2, 6}, {3, 10}, {5, 6},
},
// for vertex 5
{
{2, 9}, {4, 6},
},
}
// source/start node is 0
distances := dikstrasAlgorithm(0, edges)
fmt.Println("distances: ", distances)
/* output
distances: [0 2 7 6 13 16]
*/
}