- Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdfs_test.go
47 lines (39 loc) · 919 Bytes
/
dfs_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
42
43
44
45
46
47
package dfs
import (
"github.com/stretchr/testify/assert"
"github.com/xiaomeng79/go-algorithm/data-structures/graph"
"testing"
)
funcTestUndirectedDfs(t*testing.T) {
//建立一个无向图
h:=graph.NewUndirected()
//增加顶点,
fori:=0; i<10; i++ {
h.AddVertex(graph.VertexId(i))
}
//增加边
fori:=0; i<9; i++ {
h.AddEdge(graph.VertexId(i), graph.VertexId(i+1), 1)
}
counter:=0
UndirectedDfs(h, graph.VertexId(4), func(id graph.VertexId) {
counter+=int(id)
})
assert.Equal(t, 45, counter)
}
funcTestDirectedDfs(t*testing.T) {
//建立一个有向图
h:=graph.NewDirected()
fori:=0; i<10; i++ {
v:=graph.VertexId(i)
h.AddVertex(v)
}
fori:=0; i<9; i++ {
h.AddEdge(graph.VertexId(i), graph.VertexId(i+1), 1)
}
counter:=0
DirectedDfs(h, graph.VertexId(3), func(v graph.VertexId) {
counter+=int(v)
})
assert.Equal(t, 42, counter)
}