forked from karalabe/cookiejar
/
example_test.go
executable file
·41 lines (36 loc) · 963 Bytes
/
example_test.go
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
// CookieJar - A contestant's algorithm toolbox
// Copyright 2013 Peter Szilagyi. All rights reserved.
//
// CookieJar is dual licensed: use of this source code is governed by a BSD
// license that can be found in the LICENSE file. Alternatively, the CookieJar
// toolbox may be used in accordance with the terms and conditions contained
// in a signed written agreement between you and the author(s).
package graph_test
import (
"fmt"
"gopkg.in/karalabe/cookiejar.v2/graph"
)
// Creates a simple graph, and prints the degree of each vertex.
func Example_usage() {
// Create a star shaped 5 vertex graph
g := graph.New(5)
g.Connect(0, 2)
g.Connect(0, 3)
g.Connect(1, 3)
g.Connect(1, 4)
g.Connect(2, 4)
// For each vertex, count the outgoing edges
for v := 0; v < g.Vertices(); v++ {
degree := 0
g.Do(v, func(peer interface{}) {
degree++
})
fmt.Printf("%v: %v\n", v, degree)
}
// Output:
// 0: 2
// 1: 2
// 2: 2
// 3: 2
// 4: 2
}