Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
247 lines (195 loc) · 5.8 KB
/
Copy pathexample_test.go
File metadata and controls
247 lines (195 loc) · 5.8 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package gograph_test
import (
"errors"
"fmt"
"github.com/hmdsefi/gograph"
"github.com/hmdsefi/gograph/path"
"github.com/hmdsefi/gograph/traverse"
)
func Example() {
// An edge A -> B means A has to happen before B.
g := gograph.New[string](gograph.Acyclic())
checkout := g.AddVertexByLabel("checkout")
build := g.AddVertexByLabel("build")
test := g.AddVertexByLabel("test")
release := g.AddVertexByLabel("release")
_, _ = g.AddEdge(checkout, build)
_, _ = g.AddEdge(build, test)
_, _ = g.AddEdge(test, release)
// Acyclic graphs reject any edge that would create a cycle.
_, err := g.AddEdge(release, checkout)
fmt.Println(errors.Is(err, gograph.ErrDAGCycle))
order, _ := gograph.TopologySort(g)
for _, v := range order {
fmt.Println(v.Label())
}
// Output:
// true
// checkout
// build
// test
// release
}
func ExampleNew_directed() {
g := gograph.New[int](gograph.Directed())
_, _ = g.AddEdge(gograph.NewVertex(1), gograph.NewVertex(2))
_, _ = g.AddEdge(gograph.NewVertex(1), gograph.NewVertex(3))
_, _ = g.AddEdge(gograph.NewVertex(2), gograph.NewVertex(4))
_, _ = g.AddEdge(gograph.NewVertex(3), gograph.NewVertex(4))
_, _ = g.AddEdge(gograph.NewVertex(4), gograph.NewVertex(5))
_, _ = g.AddEdge(gograph.NewVertex(5), gograph.NewVertex(6))
fmt.Println(g.Order(), g.Size())
for _, e := range g.AllEdges() {
fmt.Println(e.Source().Label(), "->", e.Destination().Label())
}
// Output:
// 6 6
// 1 -> 2
// 1 -> 3
// 2 -> 4
// 3 -> 4
// 4 -> 5
// 5 -> 6
}
func ExampleNew_acyclic() {
g := gograph.New[int](gograph.Acyclic())
_, _ = g.AddEdge(gograph.NewVertex(1), gograph.NewVertex(2))
_, _ = g.AddEdge(gograph.NewVertex(2), gograph.NewVertex(3))
_, err := g.AddEdge(gograph.NewVertex(3), gograph.NewVertex(1))
fmt.Println(err)
// Output:
// edges would create cycle
}
func ExampleNew_undirected() {
// Graphs are undirected by default.
g := gograph.New[string]()
a := g.AddVertexByLabel("A")
b := g.AddVertexByLabel("B")
c := g.AddVertexByLabel("C")
d := g.AddVertexByLabel("D")
_, _ = g.AddEdge(a, b)
_, _ = g.AddEdge(a, d)
_, _ = g.AddEdge(b, c)
_, _ = g.AddEdge(b, d)
// Every undirected edge can be followed both ways.
fmt.Println(g.ContainsEdge(a, b), g.ContainsEdge(b, a))
// Output:
// true true
}
func ExampleNew_weighted() {
g := gograph.New[string](gograph.Weighted())
a := g.AddVertexByLabel("A")
b := g.AddVertexByLabel("B")
c := g.AddVertexByLabel("C")
d := g.AddVertexByLabel("D")
_, _ = g.AddEdge(a, b, gograph.WithEdgeWeight(4))
_, _ = g.AddEdge(a, d, gograph.WithEdgeWeight(3))
_, _ = g.AddEdge(b, c, gograph.WithEdgeWeight(3))
_, _ = g.AddEdge(b, d, gograph.WithEdgeWeight(1))
_, _ = g.AddEdge(c, d, gograph.WithEdgeWeight(2))
dist := path.Dijkstra(g, "A")
fmt.Println(dist["C"])
// Output:
// 5
}
func Example_breadthFirst() {
g := gograph.New[string](gograph.Directed())
_, _ = g.AddEdge(gograph.NewVertex("A"), gograph.NewVertex("B"))
_, _ = g.AddEdge(gograph.NewVertex("A"), gograph.NewVertex("C"))
_, _ = g.AddEdge(gograph.NewVertex("B"), gograph.NewVertex("D"))
it, err := traverse.NewBreadthFirstIterator(g, "A")
if err != nil {
fmt.Println(err)
return
}
for it.HasNext() {
fmt.Println(it.Next().Label())
}
// Output:
// A
// B
// C
// D
}
func ExampleWithVertexWeight() {
g := gograph.New[string](gograph.Directed(), gograph.Weighted())
a := g.AddVertexByLabel("A", gograph.WithVertexWeight(3))
b := g.AddVertexByLabel("B", gograph.WithVertexWeight(2))
c := g.AddVertexByLabel("C", gograph.WithVertexWeight(4))
_, _ = g.AddEdge(a, b)
_, _ = g.AddEdge(b, c)
fmt.Println(a.Weight(), b.Weight(), c.Weight())
// Output:
// 3 2 4
}
func ExampleWithEdgeLabel() {
g := gograph.New[string](gograph.Directed())
edge, _ := g.AddEdge(
gograph.NewVertex("service"),
gograph.NewVertex("database"),
gograph.WithEdgeLabel("queries"),
)
fmt.Println(edge.Label())
// Output:
// queries
}
func ExampleVertex_AllNeighbors() {
g := gograph.New[string](gograph.Directed())
a := g.AddVertexByLabel("A")
for _, label := range []string{"B", "C", "D"} {
_, _ = g.AddEdge(a, g.AddVertexByLabel(label))
}
// The loop gets the graph's own vertices, and can stop early.
for n := range a.AllNeighbors() {
fmt.Println(n.Label(), n == g.GetVertexByID(n.Label()))
if n.Label() == "C" {
break
}
}
// Output:
// B true
// C true
}
func ExampleDiff() {
before := gograph.New[string](gograph.Directed())
app := before.AddVertexByLabel("app")
_, _ = before.AddEdge(app, gograph.NewVertex("db"), gograph.WithEdgeLabel("reads"))
_, _ = before.AddEdge(app, gograph.NewVertex("log"))
after := gograph.Clone(before)
after.RemoveVertices(after.GetVertexByID("log"))
_, _ = after.AddEdge(after.GetVertexByID("app"), gograph.NewVertex("cache"), gograph.WithEdgeWeight(2))
after.RemoveEdges(after.GetEdge(after.GetVertexByID("app"), after.GetVertexByID("db")))
_, _ = after.AddEdge(after.GetVertexByID("app"), after.GetVertexByID("db"), gograph.WithEdgeLabel("writes"))
d, err := gograph.Diff(before, after)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(d)
// Output:
// + vertex cache
// - vertex log
// + edge app -> cache weight 2
// - edge app -> log
// ~ edge app -> db label "reads" -> "writes"
}
func ExampleGraphDiff_Apply() {
v1 := gograph.New[string](gograph.Acyclic())
_, _ = v1.AddEdge(gograph.NewVertex("parse"), gograph.NewVertex("check"))
v2 := gograph.Clone(v1)
_, _ = v2.AddEdge(v2.GetVertexByID("check"), gograph.NewVertex("emit"))
d, _ := gograph.Diff(v1, v2)
patched, err := d.Apply(v1)
if err != nil {
fmt.Println(err)
return
}
rest, _ := gograph.Diff(patched, v2)
fmt.Println(v1.Order(), patched.Order(), rest.IsEmpty())
// Applying it again finds the new vertex already there.
_, err = d.Apply(patched)
fmt.Println(errors.Is(err, gograph.ErrDiffMismatch))
// Output:
// 2 3 true
// true
}