| 1 |
package graph_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"github.com/yourbasic/graph" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
func Example_basics() { |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
g := graph.New(4) |
| 14 |
g.AddBoth(0, 1) |
| 15 |
g.AddBoth(0, 2) |
| 16 |
g.AddBoth(2, 3) |
| 17 |
g.AddBoth(1, 3) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
— it |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
for v := 0; v < g.Order(); v++ { |
| 28 |
g.Visit(v, func(w int, c int64) (skip bool) { |
| 29 |
|
| 30 |
return |
| 31 |
}) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
for v := 0; v < g.Order(); v++ { |
| 39 |
graph.Sort(g).Visit(v, func(w int, c int64) (skip bool) { |
| 40 |
|
| 41 |
return |
| 42 |
}) |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
for v := 0; v < g.Order(); v++ { |
| 51 |
aborted := graph.Sort(g).Visit(v, func(w int, c int64) (skip bool) { |
| 52 |
fmt.Println(v, w) |
| 53 |
if w == 3 { |
| 54 |
skip = true |
| 55 |
} |
| 56 |
return |
| 57 |
}) |
| 58 |
if aborted { |
| 59 |
break |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func ExampleBFS() { |
| 71 |
gm := graph.New(6) |
| 72 |
gm.AddBoth(0, 1) |
| 73 |
gm.AddBoth(0, 3) |
| 74 |
gm.AddBoth(1, 2) |
| 75 |
gm.AddBoth(1, 4) |
| 76 |
gm.AddBoth(2, 5) |
| 77 |
gm.AddBoth(3, 4) |
| 78 |
g := graph.Sort(gm) |
| 79 |
|
| 80 |
dist := make([]int, g.Order()) |
| 81 |
graph.BFS(g, 0, func(v, w int, _ int64) { |
| 82 |
fmt.Println(v, "to", w) |
| 83 |
dist[w] = dist[v] + 1 |
| 84 |
}) |
| 85 |
fmt.Println("dist:", dist) |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func ExampleString() { |
| 97 |
g0 := graph.New(0) |
| 98 |
fmt.Println(g0) |
| 99 |
|
| 100 |
g1 := graph.New(1) |
| 101 |
g1.Add(0, 0) |
| 102 |
fmt.Println(g1) |
| 103 |
|
| 104 |
g4 := graph.New(4) |
| 105 |
g4.AddBoth(0, 1) |
| 106 |
g4.AddCost(2, 1, 8) |
| 107 |
fmt.Println(g4) |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
func ExampleEulerDirected() { |
| 116 |
|
| 117 |
g := graph.New(4) |
| 118 |
g.AddBoth(0, 1) |
| 119 |
g.Add(1, 2) |
| 120 |
|
| 121 |
fmt.Println(graph.EulerDirected(g)) |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func ExampleEulerUndirected() { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
g := graph.New(4) |
| 133 |
g.AddBoth(1, 3) |
| 134 |
g.AddBoth(2, 3) |
| 135 |
g.AddBoth(3, 3) |
| 136 |
|
| 137 |
fmt.Println(graph.EulerUndirected(g)) |
| 138 |
|
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
func ExampleShortestPath() { |
| 144 |
g := graph.New(6) |
| 145 |
g.AddBothCost(0, 1, 8) |
| 146 |
g.AddBothCost(0, 3, 2) |
| 147 |
g.AddBothCost(1, 2, 2) |
| 148 |
g.AddBothCost(1, 4, 2) |
| 149 |
g.AddBothCost(2, 5, 2) |
| 150 |
g.AddBothCost(3, 4, 2) |
| 151 |
g.AddBothCost(4, 5, 8) |
| 152 |
|
| 153 |
path, dist := graph.ShortestPath(g, 0, 5) |
| 154 |
fmt.Println("path:", path, "length:", dist) |
| 155 |
|
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
func ExampleStrongComponents() { |
| 161 |
g := graph.New(6) |
| 162 |
g.AddBoth(0, 1) |
| 163 |
g.AddBoth(1, 2) |
| 164 |
g.Add(3, 0) |
| 165 |
g.AddBoth(3, 4) |
| 166 |
g.Add(4, 1) |
| 167 |
g.Add(4, 5) |
| 168 |
g.Add(5, 2) |
| 169 |
|
| 170 |
fmt.Println(graph.StrongComponents(g)) |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
func ExampleComponents() { |
| 176 |
g := graph.New(6) |
| 177 |
g.AddBoth(0, 1) |
| 178 |
g.Add(1, 2) |
| 179 |
g.Add(4, 3) |
| 180 |
g.AddBoth(5, 2) |
| 181 |
|
| 182 |
fmt.Println(graph.Components(g)) |
| 183 |
|
| 184 |
} |