Skip to content

Commit ae64361

Browse files
committed
create Dijkstra
1 parent e20afaa commit ae64361

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

basics/graph/dijkstra.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package graph
2+
3+
import (
4+
"container/heap"
5+
"math"
6+
)
7+
8+
type Edge struct {
9+
To int
10+
Weight int
11+
}
12+
13+
// adjacency list
14+
type Graph struct {
15+
Nodes map[int][]Edge
16+
}
17+
18+
func (g *Graph) Dijkstra(source int) map[int]int {
19+
dist := make(map[int]int)
20+
for node := range g.Nodes {
21+
dist[node] = math.MaxInt64
22+
}
23+
dist[source] = 0
24+
25+
pq := make(PriorityQueue, 0)
26+
heap.Init(&pq)
27+
heap.Push(&pq, &Item{Node: source, Priority: 0})
28+
29+
for pq.Len() > 0 {
30+
u := heap.Pop(&pq).(*Item).Node
31+
32+
for _, edge := range g.Nodes[u] {
33+
v := edge.To
34+
weight := edge.Weight
35+
if dist[u]+weight < dist[v] {
36+
dist[v] = dist[u] + weight
37+
heap.Push(&pq, &Item{Node: v, Priority: dist[v]})
38+
}
39+
}
40+
}
41+
42+
return dist
43+
}

basics/graph/priorityqueue.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package graph
2+
3+
type PriorityQueue []*Item
4+
5+
type Item struct {
6+
Node int
7+
Priority int
8+
Index int
9+
}
10+
11+
func (pq PriorityQueue) Len() int { return len(pq) }
12+
13+
func (pq PriorityQueue) Less(i, j int) bool {
14+
return pq[i].Priority < pq[j].Priority
15+
}
16+
17+
func (pq PriorityQueue) Swap(i, j int) {
18+
pq[i], pq[j] = pq[j], pq[i]
19+
pq[i].Index = i
20+
pq[j].Index = j
21+
}
22+
23+
func (pq *PriorityQueue) Push(x interface{}) {
24+
n := len(*pq)
25+
item := x.(*Item)
26+
item.Index = n
27+
*pq = append(*pq, item)
28+
}
29+
30+
func (pq *PriorityQueue) Pop() interface{} {
31+
old := *pq
32+
n := len(old)
33+
item := old[n-1]
34+
item.Index = -1
35+
*pq = old[0 : n-1]
36+
return item
37+
}

basics/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"runtime"
77
"strings"
88

9+
"github.com/rihib/leetcode/basics/graph"
910
"github.com/rihib/leetcode/basics/queue"
1011
"github.com/rihib/leetcode/basics/sort"
1112
)
1213

1314
func main() {
1415
testSort()
1516
testPriorityQueue()
17+
testDijkstra()
1618
}
1719

1820
func testSort() {
@@ -109,4 +111,22 @@ func testPriorityQueue() {
109111

110112
node, l, isEmpty = pq.Peek(), pq.Len(), pq.IsEmpty()
111113
fmt.Printf("Peek returns nil: %t, Len: %d, IsEmpty: %t\n", node == nil, l, isEmpty)
114+
115+
fmt.Println("")
116+
}
117+
118+
func testDijkstra() {
119+
fmt.Println("=====Dijkstra=====")
120+
g := &graph.Graph{
121+
Nodes: make(map[int][]graph.Edge),
122+
}
123+
g.Nodes[0] = []graph.Edge{{To: 1, Weight: 4}, {To: 2, Weight: 1}}
124+
g.Nodes[1] = []graph.Edge{{To: 3, Weight: 1}}
125+
g.Nodes[2] = []graph.Edge{{To: 1, Weight: 2}, {To: 3, Weight: 5}}
126+
g.Nodes[3] = []graph.Edge{}
127+
128+
dist := g.Dijkstra(0)
129+
for node, distance := range dist {
130+
fmt.Printf("Distance from node 0 to node %d is %d\n", node, distance)
131+
}
112132
}

0 commit comments

Comments
 (0)