forked from ailidani/paxi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.go
232 lines (194 loc) · 4.03 KB
/
graph.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package lib
import "container/list"
type Graph struct {
vertices Set
from map[interface{}]Set
to map[interface{}]Set
}
func NewGraph() *Graph {
return &Graph{
vertices: NewSet(),
from: make(map[interface{}]Set),
to: make(map[interface{}]Set),
}
}
func (g *Graph) Size() int {
return len(g.vertices)
}
func (g *Graph) Has(v interface{}) bool {
return g.vertices.Has(v)
}
func (g *Graph) Add(v interface{}) {
if !g.Has(v) {
g.vertices.Add(v)
g.from[v] = NewSet()
g.to[v] = NewSet()
}
}
func (g *Graph) Remove(v interface{}) {
if !g.Has(v) {
return
}
g.vertices.Remove(v)
for u := range g.vertices {
g.from[u].Remove(v)
g.to[u].Remove(v)
}
delete(g.from, v)
delete(g.to, v)
}
func (g *Graph) AddEdge(from, to interface{}) {
if from == to {
panic("graph: adding self edge")
}
if !g.Has(from) {
g.Add(from)
}
if !g.Has(to) {
g.Add(to)
}
g.from[from].Add(to)
g.to[to].Add(from)
}
func (g *Graph) RemoveEdge(from, to interface{}) {
if !g.Has(from) || !g.Has(to) {
return
}
g.from[from].Remove(to)
g.to[to].Remove(from)
}
func (g *Graph) Vertices() Set {
return g.vertices
}
// From returns all vertices in graph that can be reached directly from v
func (g *Graph) From(v interface{}) Set {
return g.from[v]
}
// To returns all vertices that can reach to v
func (g *Graph) To(v interface{}) Set {
return g.to[v]
}
// BFS returns breadth first search vertices from a given source
func (g *Graph) BFS(v interface{}) []interface{} {
result := make([]interface{}, 0)
visited := make(map[interface{}]bool)
queue := list.New()
visited[v] = true
queue.PushBack(v)
for queue.Len() > 0 {
s := queue.Front()
result = append(result, s.Value)
queue.Remove(s)
for t := range g.from[s.Value] {
if !visited[t] {
visited[t] = true
queue.PushBack(t)
}
}
}
return result
}
// DFS returns depth first search vertices from a given source
func (g *Graph) DFS(v interface{}) []interface{} {
result := make([]interface{}, 0)
visited := NewSet()
stack := NewStack()
stack.Push(v)
for !stack.Empty() {
s := stack.Pop()
if !visited.Has(s) {
visited.Add(s)
result = append(result, s)
}
for i := range g.from[s] {
if !visited.Has(i) {
stack.Push(i)
}
}
}
return result
}
func (g *Graph) BFSReverse(v interface{}) []interface{} {
vertices := make([]interface{}, 0)
visited := make(map[interface{}]bool)
queue := list.New()
visited[v] = true
queue.PushBack(v)
for queue.Len() > 0 {
s := queue.Front()
vertices = append(vertices, s.Value)
queue.Remove(s)
for t := range g.to[s.Value] {
if !visited[t] {
visited[t] = true
queue.PushBack(t)
}
}
}
return vertices
}
// Transpose return transpose graph
func (g *Graph) Transpose() *Graph {
t := NewGraph()
t.vertices = g.vertices.Clone()
for v := range g.vertices {
t.from[v] = g.to[v].Clone()
t.to[v] = g.from[v].Clone()
}
return t
}
type color int
const (
white color = iota // unvisited nodes
gray // visiting nodes
black // visited nodes
)
func (g *Graph) visit(v interface{}, colors map[interface{}]color) bool {
colors[v] = gray
for _, u := range g.from[v].Slice() {
if colors[u] == gray {
return true
}
if colors[u] == white && g.visit(u, colors) {
return true
}
}
colors[v] = black
return false
}
// Cyclic returns true if the graph contains a cycle
func (g *Graph) Cyclic() bool {
colors := make(map[interface{}]color)
for v := range g.vertices {
colors[v] = white
}
for v := range g.vertices {
if colors[v] == white {
if g.visit(v, colors) {
return true
}
}
}
return false
}
// Cycle returns the first cycle with vertices
func (g *Graph) Cycle() []interface{} {
colors := make(map[interface{}]color)
for v := range g.vertices {
colors[v] = white
}
for v := range g.vertices {
if colors[v] == white {
if g.visit(v, colors) {
cycle := make([]interface{}, 0)
for v, color := range colors {
if color == gray {
cycle = append(cycle, v)
}
}
return cycle
}
}
}
return nil
}