-
Notifications
You must be signed in to change notification settings - Fork 20
/
Dijkstra.go
70 lines (66 loc) · 1.32 KB
/
Dijkstra.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
package main
import "fmt"
import "sort"
// O(n)
func MinCost(cost []int, dist []int) (int, int) {
minn := 100000000
to := 100000000
for j:=0; j<len(cost); j++ {
if cost[j] != 0 && dist[j] != 1 {
if cost[j] < minn {
minn = cost[j]
to = j
}
}
}
return minn, to
}
// O(n^2)
func Dijkstra(graph [][]int, vertex int) (map[string]int, int) {
min := 0
path := make(map[string]int)
count := 0
next := 0
path_temp := ""
dist := make([]int, vertex)
for len(path) < vertex-1 {
dist[next] = 1
a, b := MinCost(graph[next], dist)
path_temp = string(count+'1')+". "+string(next+'A')+"->"+string(b+'A')
path[path_temp] = a
min += a
next = b
count += 1
}
return path, min
}
func main(){
vertex := 5
/*
2 3
A----B----C
| / \ |
6| 8/ \5 |7
| / \ |
D---------E
9
*/
graph := [][]int{
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0},
}
path, min := Dijkstra(graph, vertex)
fmt.Println("Shortest path : ")
var keys []string
for k := range path {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Println(k, ":", path[k])
}
fmt.Println("Total cost :", min)
}