| 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 |
|
| 11 |
|
| 12 |
func Example_euclid() { |
| 13 |
type Point struct{ x, y int } |
| 14 |
|
| 15 |
|
| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
points := []Point{ |
| 26 |
{0, 0}, {0, 1}, {0, 2}, |
| 27 |
{4, 0}, {4, 1}, {4, 2}, |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
g := build.Kmn(3, 3).AddCostFunc(func(v, w int) int64 { |
| 34 |
|
| 35 |
return int64(1000 * Euclid(points[v], points[w])) |
| 36 |
}) |
| 37 |
|
| 38 |
|
| 39 |
path, dist := graph.ShortestPath(g, 0, 2) |
| 40 |
fmt.Println("path:", path, "length:", float64(dist)/1000) |
| 41 |
|
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
func Example_maxflow() { |
| 47 |
. |
| 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 |
|
| 54 |
grid = grid.Keep(func(v, w int) bool { return v < w }) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 81 |
flow, _ := graph.MaxFlow(grid_S_T, n*n, n*n+1) |
| 82 |
fmt.Println(flow) |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func ExampleVirtual_Keep() { |
| 88 |
|
| 89 |
Kn4 := build.Kn(4) |
| 90 |
fmt.Println(Kn4) |
| 91 |
|
| 92 |
|
| 93 |
Kn4MinusZero := Kn4.Keep(func(v, w int) bool { return v != 0 && w != 0 }) |
| 94 |
fmt.Println(Kn4MinusZero) |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
func ExampleVirtual_Intersect() { |
| 102 |
|
| 103 |
Kn4 := build.Kn(4) |
| 104 |
fmt.Println(Kn4) |
| 105 |
|
| 106 |
|
| 107 |
Cn4 := build.Cycle(4) |
| 108 |
fmt.Println(Cn4) |
| 109 |
|
| 110 |
|
| 111 |
Kn4DiffCn4 := Kn4.Intersect(Cn4.Complement()) |
| 112 |
fmt.Println(Kn4DiffCn4) |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
func ExampleVirtual_Connect_barbell() { |
| 121 |
|
| 122 |
|
| 123 |
n := 2 |
| 124 |
bar := build.Grid(1, 2).AddCost(20) |
| 125 |
redPlate := build.Kn(n).AddCost(25) |
| 126 |
|
| 127 |
|
| 128 |
barbell := bar.Connect(0, redPlate).Connect(1, redPlate) |
| 129 |
fmt.Println(barbell) |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
func ExampleVirtual_Join_wheel() { |
| 135 |
|
| 136 |
|
| 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 |
|
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
func ExampleVirtual_Match_petersen() { |
| 148 |
|
| 149 |
|
| 150 |
pentagon := build.Cycle(5) |
| 151 |
pentagram := pentagon.Complement() |
| 152 |
petersen := pentagon.Match(pentagram, build.AllEdges()) |
| 153 |
fmt.Println(petersen) |
| 154 |
|
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
₂. |
| 159 |
func ExampleVirtual_Tensor() { |
| 160 |
G. |
| 161 |
Vⱼ |
| 162 |
≠ j. |
| 163 |
|
| 164 |
k4 := build.Kn(4) |
| 165 |
crown8 := k4.Tensor(build.Kn(2)) |
| 166 |
fmt.Println(crown8) |
| 167 |
|
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
|
| 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 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
func ExampleVirtual_Match_cube() { |
| 195 |
|
| 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 |
|
| 201 |
|
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
func ExampleGeneric() { |
| 207 |
|
| 208 |
g := build.Generic(10, func(v, w int) bool { |
| 209 |
|
| 210 |
return v%2 == 1 && w%2 == 0 |
| 211 |
}) |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
order, acyclic := graph.TopSort(g) |
| 216 |
fmt.Println("Acyclic:", acyclic) |
| 217 |
fmt.Println(order) |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
} |