-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphlib.go
More file actions
187 lines (176 loc) · 4.11 KB
/
graphlib.go
File metadata and controls
187 lines (176 loc) · 4.11 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package graphlib
type Node struct {
Name string
links []Edge
}
type Edge struct {
from *Node
to *Node
cost uint
}
type Graph struct {
nodes map[string]*Node
exists map[string]bool
GraphType string
}
func NewGraph(GT string) *Graph {
if GT != "undirected" && GT != "directed" {
panic("The argument for graph creation can be \"directed\" or \"undirected\" only")
}
return &Graph{nodes: map[string]*Node{}, exists: map[string]bool{}, GraphType: GT}
}
func (g *Graph) AddNodes(names []string) {
for _, name := range names {
if _, ok := g.nodes[name]; !ok {
g.nodes[name] = &Node{Name: name, links: []Edge{}}
g.exists[name] = true
}
}
}
func (g *Graph) AddLink(a, b string, cost int) {
aNode := g.nodes[a]
bNode := g.nodes[b]
if aNode == nil || bNode == nil {
panic("creating edge for node that does not exist!")
}
aNode.links = append(aNode.links, Edge{from: aNode, to: bNode, cost: uint(cost)})
if g.GraphType == "undirected" {
bNode.links = append(bNode.links, Edge{from: bNode, to: aNode, cost: uint(cost)})
}
}
func (g *Graph) DistBetn(source string, destination string) ([]string, uint) {
dist, prev := map[string]uint{}, map[string]string{}
var path []string
reverse := func(input []string) []string {
var res []string
for i := len(input) - 1; i >= 0; i-- {
res = append(res, input[i])
}
return res
}
getClosestNonVisitedNode := func(dist map[string]uint, visited map[string]bool) string {
lowestCost := INFINITY
lowestNode := ""
for key, dis := range dist {
if _, ok := visited[key]; dis == INFINITY || ok {
continue
}
if dis < lowestCost {
lowestCost = dis
lowestNode = key
}
}
return lowestNode
}
if !g.exists[source] || !g.exists[destination] {
panic("one of the nodes does not exist!")
}
for _, node := range g.nodes {
dist[node.Name] = INFINITY
prev[node.Name] = ""
}
visited := map[string]bool{}
dist[source] = 0
for u := source; u != ""; u = getClosestNonVisitedNode(dist, visited) {
if source == destination {
break
}
currDist := dist[u]
for _, link := range g.nodes[u].links {
if _, ok := visited[link.to.Name]; ok {
continue
}
alt := currDist + link.cost
v := link.to.Name
if alt < dist[v] {
dist[v] = alt
prev[v] = u
}
}
visited[u] = true
}
cur := destination
for cur != "" {
path = append(path, cur)
cur = prev[cur]
}
path = reverse(path)
return path, dist[destination]
}
const INFINITY = ^uint(0)
func (g *Graph) Dijkstra(source string) (map[string]uint, map[string]string) {
getClosestNonVisitedNode := func(dist map[string]uint, visited map[string]bool) string {
lowestCost := INFINITY
lowestNode := ""
for key, dis := range dist {
if _, ok := visited[key]; dis == INFINITY || ok {
continue
}
if dis < lowestCost {
lowestCost = dis
lowestNode = key
}
}
return lowestNode
}
dist, prev := map[string]uint{}, map[string]string{}
for _, node := range g.nodes {
dist[node.Name] = INFINITY
prev[node.Name] = ""
}
visited := map[string]bool{}
dist[source] = 0
for u := source; u != ""; u = getClosestNonVisitedNode(dist, visited) {
// fmt.Println(u)
currDist := dist[u]
for _, link := range g.nodes[u].links {
if _, ok := visited[link.to.Name]; ok {
continue
}
alt := currDist + link.cost
v := link.to.Name
if alt < dist[v] {
dist[v] = alt
prev[v] = u
}
}
visited[u] = true
}
return dist, prev
}
func (g *Graph) TopologicalSort() []string {
in_degree := make(map[string]int)
for name := range g.nodes {
for _, link := range g.nodes[name].links {
if _, ok := in_degree[name]; !ok {
in_degree[link.to.Name] = 1
} else {
in_degree[link.to.Name]++
}
}
}
var q []string
for name := range g.nodes {
if in_degree[name] == 0 {
q = append(q, name)
}
}
cnt := 0
var result []string
for len(q) > 0 {
cur := q[0]
q = q[1:]
result = append(result, cur)
for _, link := range g.nodes[cur].links {
in_degree[link.to.Name]--
if in_degree[link.to.Name] == 0 {
q = append(q, link.to.Name)
}
}
cnt++
}
if cnt != len(g.nodes) {
panic("there exists a cycle in the graph")
}
return result
}