Debian Codegraph @

build/examples_test.go

raw symbols SCIP
1 package build_test
2
3 import (
4 "fmt"
5 "github.com/yourbasic/graph"
6 "github.com/yourbasic/graph/build"
7 "math"
8 )
9
10 // Find a shortest path going back and forth between
11 // two sets of points in the plane.
12 func Example_euclid() {
13 type Point struct{ x, y int }
14
15 // Euclidean distance.
16 Euclid := func(p, q Point) float64 {
17 xd := p.x - q.x
18 yd := p.y - q.y
19 return math.Sqrt(float64(xd*xd + yd*yd))
20 }
21
22 // 0 3
23 // 1 4
24 // 2 5
25 points := []Point{
26 {0, 0}, {0, 1}, {0, 2},
27 {4, 0}, {4, 1}, {4, 2},
28 }
29
30 // Build a complete bipartite graph, connecting the three
31 // left-hand points to the three points on the right,
32 // and then apply a cost function to the edges of the graph.
33 g := build.Kmn(3, 3).AddCostFunc(func(v, w int) int64 {
34 // Distance to three decimal places.
35 return int64(1000 * Euclid(points[v], points[w]))
36 })
37
38 // Find a shortest path from 0 to 2.
39 path, dist := graph.ShortestPath(g, 0, 2)
40 fmt.Println("path:", path, "length:", float64(dist)/1000)
41 // Output:
42 // path: [0 4 2] length: 8.246
43 }
44
45 // Find a maximum flow in a virtual grid graph.
46 func Example_maxflow() {
47 // Build an undirected n×n grid with a silly edge cost.
48 n := 10
49 grid := build.Grid(n, n).AddCostFunc(func(v, w int) int64 {
50 return int64((v + w) * (w - v))
51 })
52
53 // Keep only the forward-pointing edges.
54 grid = grid.Keep(func(v, w int) bool { return v < w })
55
56 // Join the top row, 0..n-1, of the grid to a source S.
57 // The vertex in S will get index n*n in the new graph.
58 // The new edges should point from S to the grid.
59 // Give the new edges maximum capacity.
60 S := build.Empty(1)
61 grid_S := grid.Join(S, build.EdgeSet{
62 From: build.Range(0, n),
63 To: build.Vertex(n * n),
64 Keep: func(v, w int) bool { return v > w },
65 Cost: build.Cost(graph.Max),
66 })
67
68 // Join the bottom row, n*(n-1)..n*n-1, of the grid to a sink T.
69 // The vertex in T will get index n*n+1 in the new graph.
70 // The new edges should point from the grid to T.
71 // Give the new edges maximum capacity.
72 T := build.Empty(1)
73 grid_S_T := grid_S.Join(T, build.EdgeSet{
74 From: build.Range(n*(n-1), n*n),
75 To: build.Vertex(n*n + 1),
76 Keep: func(v, w int) bool { return v < w },
77 Cost: build.Cost(graph.Max),
78 })
79
80 // Find the maximum flow from S to T.
81 flow, _ := graph.MaxFlow(grid_S_T, n*n, n*n+1)
82 fmt.Println(flow)
83 // Output: 1900
84 }
85
86 // Filter a graph with a function.
87 func ExampleVirtual_Keep() {
88 // The complete graph with four vertices.
89 Kn4 := build.Kn(4)
90 fmt.Println(Kn4)
91
92 // Remove all edges incident with vertex 0.
93 Kn4MinusZero := Kn4.Keep(func(v, w int) bool { return v != 0 && w != 0 })
94 fmt.Println(Kn4MinusZero)
95 // Output:
96 // 4 [{0 1} {0 2} {0 3} {1 2} {1 3} {2 3}]
97 // 4 [{1 2} {1 3} {2 3}]
98 }
99
100 // Compute the difference of two graphs.
101 func ExampleVirtual_Intersect() {
102 // The complete graph with four vertices.
103 Kn4 := build.Kn(4)
104 fmt.Println(Kn4)
105
106 // The circle graph with four vertices.
107 Cn4 := build.Cycle(4)
108 fmt.Println(Cn4)
109
110 // Remove all edges belonging to Cn4 from Kn4.
111 Kn4DiffCn4 := Kn4.Intersect(Cn4.Complement())
112 fmt.Println(Kn4DiffCn4)
113 // Output:
114 // 4 [{0 1} {0 2} {0 3} {1 2} {1 3} {2 3}]
115 // 4 [{0 1} {0 3} {1 2} {2 3}]
116 // 4 [{0 2} {1 3}]
117 }
118
119 // Build a weighted barbell graph.
120 func ExampleVirtual_Connect_barbell() {
121 // The n-barbell graph consists of two non-overlapping n-vertex cliques
122 // together with a single edge that has an endpoint in each clique.
123 n := 2
124 bar := build.Grid(1, 2).AddCost(20)
125 redPlate := build.Kn(n).AddCost(25)
126
127 // Connect one plate to each end of the bar.
128 barbell := bar.Connect(0, redPlate).Connect(1, redPlate)
129 fmt.Println(barbell)
130 // Output: 4 [{0 1}:20 {0 2}:25 {1 3}:25]
131 }
132
133 // Build a wheel graph.
134 func ExampleVirtual_Join_wheel() {
135 // A wheel graph is a graph formed by joining a single vertex
136 // to all vertices of a cycle.
137 n := 32
138 hub := build.Empty(1)
139 rim := build.Cycle(n)
140 wheel := hub.Join(rim, build.AllEdges())
141 fmt.Println("Number of spokes:", wheel.Degree(0))
142 // Output:
143 // Number of spokes: 32
144 }
145
146 // Build a Petersen graph.
147 func ExampleVirtual_Match_petersen() {
148 // The Petersen graph is most commonly drawn as a pentagon
149 // with a pentagram inside, with five spokes.
150 pentagon := build.Cycle(5)
151 pentagram := pentagon.Complement()
152 petersen := pentagon.Match(pentagram, build.AllEdges())
153 fmt.Println(petersen)
154 // Output:
155 // 10 [{0 1} {0 4} {0 5} {1 2} {1 6} {2 3} {2 7} {3 4} {3 8} {4 9} {5 7} {5 8} {6 8} {6 9} {7 9}]
156 }
157
158 // Build a crown graph by multiplying Kₙ with K₂.
159 func ExampleVirtual_Tensor() {
160 // The tensor product G × K₂ is a bipartite graph called the bipartite double cover of G.
161 // A crown graph on 2n vertices is an undirected graph with two sets of vertices Uᵢ and Vⱼ
162 // and with an edge from Uᵢ to Vⱼ whenever i ≠ j.
163 // It can be computed as the bipartite double cover of Kn.
164 k4 := build.Kn(4)
165 crown8 := k4.Tensor(build.Kn(2))
166 fmt.Println(crown8)
167 // Output:
168 // 8 [{0 3} {0 5} {0 7} {1 2} {1 4} {1 6} {2 5} {2 7} {3 4} {3 6} {4 7} {5 6}]
169 }
170
171 // Build a cube graph by multiplying a single edge with itself.
172 func ExampleVirtual_Cartesian() {
173 edge := build.Grid(1, 2)
174 square := edge.Cartesian(edge)
175 cube := square.Cartesian(edge)
176
177 fmt.Println(edge)
178 fmt.Println(square)
179 fmt.Println(cube)
180
181 fmt.Println(graph.Equal(edge, build.Hyper(1)))
182 fmt.Println(graph.Equal(square, build.Hyper(2)))
183 fmt.Println(graph.Equal(cube, build.Hyper(3)))
184 // Output:
185 // 2 [{0 1}]
186 // 4 [{0 1} {0 2} {1 3} {2 3}]
187 // 8 [{0 1} {0 2} {0 4} {1 3} {1 5} {2 3} {2 6} {3 7} {4 5} {4 6} {5 7} {6 7}]
188 // true
189 // true
190 // true
191 }
192
193 // Build a cube graph.
194 func ExampleVirtual_Match_cube() {
195 // Build a cube graph.
196 square := build.Grid(2, 2)
197 cube := square.Match(square, build.AllEdges())
198 fmt.Println(cube)
199 fmt.Println(graph.Equal(cube, build.Hyper(3)))
200 // Output:
201 // 8 [{0 1} {0 2} {0 4} {1 3} {1 5} {2 3} {2 6} {3 7} {4 5} {4 6} {5 7} {6 7}]
202 // true
203 }
204
205 // Build a directed graph containing all edges (v, w) for which v is odd and w even.
206 func ExampleGeneric() {
207 // Define a graph by a function.
208 g := build.Generic(10, func(v, w int) bool {
209 // Include all edges with v odd and w even.
210 return v%2 == 1 && w%2 == 0
211 })
212
213 // In a topological ordering of this graph,
214 // odd numbered vertices must come before even.
215 order, acyclic := graph.TopSort(g)
216 fmt.Println("Acyclic:", acyclic)
217 fmt.Println(order)
218 // Output:
219 // Acyclic: true
220 // [1 3 5 7 9 0 2 4 6 8]
221 }