Debian Codegraph @

examples_test.go

raw symbols SCIP
1 package graph_test
2
3 import (
4 "fmt"
5 "github.com/yourbasic/graph"
6 )
7
8 // Build a plain graph and visit all of its edges.
9 func Example_basics() {
10 // Build a graph with four vertices and four undirected edges.
11 // (Each of these edges are, in fact, represented by two directed
12 // edges pointing in opposite directions.)
13 g := graph.New(4)
14 g.AddBoth(0, 1) // 0 -- 1
15 g.AddBoth(0, 2) // | |
16 g.AddBoth(2, 3) // 2 -- 3
17 g.AddBoth(1, 3)
18
19 // The vertices of all graphs in this package are numbered 0..n-1.
20 // The edge iterator is a method called Visit; it calls a function
21 // for each neighbor of a given vertex. Together with the Order
22 // method — which returns the number of vertices in a graph — it
23 // constitutes an Iterator. All algorithms in this package operate
24 // on any graph implementing this interface.
25
26 // Visit all edges of a graph.
27 for v := 0; v < g.Order(); v++ {
28 g.Visit(v, func(w int, c int64) (skip bool) {
29 // Visiting edge (v, w) of cost c.
30 return
31 })
32 }
33
34 // The immutable data structure created by Sort has an Iterator
35 // that returns neighbors in increasing order.
36
37 // Visit the edges in order.
38 for v := 0; v < g.Order(); v++ {
39 graph.Sort(g).Visit(v, func(w int, c int64) (skip bool) {
40 // Visiting edge (v, w) of cost c.
41 return
42 })
43 }
44
45 // The return value of an iterator function is used to break
46 // out of the iteration. Visit, in turn, returns a boolean
47 // indicating if it was aborted.
48
49 // Skip the iteration at the first edge (v, w) with w equal to 3.
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 // Aborts the call to Visit.
55 }
56 return
57 })
58 if aborted {
59 break
60 }
61 }
62 // output:
63 // 0 1
64 // 0 2
65 // 1 0
66 // 1 3
67 }
68
69 // Find the shortest distances from a vertex in an unweighted graph.
70 func ExampleBFS() {
71 gm := graph.New(6)
72 gm.AddBoth(0, 1) // 0--1--2
73 gm.AddBoth(0, 3) // | | |
74 gm.AddBoth(1, 2) // 3--4 5
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 // Output:
87 // 0 to 1
88 // 0 to 3
89 // 1 to 2
90 // 1 to 4
91 // 2 to 5
92 // dist: [0 1 2 1 2 3]
93 }
94
95 // Print a graph.
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) // 8
105 g4.AddBoth(0, 1) // 0 <--> 1 <--- 2 3
106 g4.AddCost(2, 1, 8)
107 fmt.Println(g4)
108 // Output:
109 // 0 []
110 // 1 [(0 0)]
111 // 4 [{0 1} (2 1):8]
112 }
113
114 // Find an Euler walk in a directed graph.
115 func ExampleEulerDirected() {
116 // 0 <--> 1 --> 2 3
117 g := graph.New(4)
118 g.AddBoth(0, 1)
119 g.Add(1, 2)
120
121 fmt.Println(graph.EulerDirected(g))
122 // Output: [1 0 1 2] true
123 }
124
125 // Find an Euler walk in an undirected graph.
126 func ExampleEulerUndirected() {
127 // 0 1
128 // |
129 // 2--3---
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 // Output:
139 // [1 3 3 2] true
140 }
141
142 // Find a shortest path between two vertices in a graph.
143 func ExampleShortestPath() {
144 g := graph.New(6)
145 g.AddBothCost(0, 1, 8) // 0==1--2
146 g.AddBothCost(0, 3, 2) // | | |
147 g.AddBothCost(1, 2, 2) // 3--4==5
148 g.AddBothCost(1, 4, 2) //
149 g.AddBothCost(2, 5, 2) // -- cost 2
150 g.AddBothCost(3, 4, 2) // == cost 8
151 g.AddBothCost(4, 5, 8)
152
153 path, dist := graph.ShortestPath(g, 0, 5)
154 fmt.Println("path:", path, "length:", dist)
155 // Output:
156 // path: [0 3 4 1 2 5] length: 10
157 }
158
159 // Find the strongly connected components in a directed graph.
160 func ExampleStrongComponents() {
161 g := graph.New(6)
162 g.AddBoth(0, 1) // 0 <--> 1 <--> 2
163 g.AddBoth(1, 2) // ^ ^ ^
164 g.Add(3, 0) // | | |
165 g.AddBoth(3, 4) // 3 <--> 4 ---> 5
166 g.Add(4, 1)
167 g.Add(4, 5)
168 g.Add(5, 2)
169
170 fmt.Println(graph.StrongComponents(g))
171 // Output: [[2 1 0] [5] [4 3]]
172 }
173
174 // Find the weakly connected components in a directed graph.
175 func ExampleComponents() {
176 g := graph.New(6)
177 g.AddBoth(0, 1) // 0 <--> 1 ---> 2
178 g.Add(1, 2) // ^
179 g.Add(4, 3) // |
180 g.AddBoth(5, 2) // 3 <--- 4 5
181
182 fmt.Println(graph.Components(g))
183 // Output: [[0 1 2 5] [3 4]]
184 }