File tree Expand file tree Collapse file tree 4 files changed +66
-7
lines changed Expand file tree Collapse file tree 4 files changed +66
-7
lines changed Original file line number Diff line number Diff line change 4
4
5
5
#### 深度优先DFS(Depth First Search)
6
6
7
+ 如: 树的先序遍历
8
+
7
9
思想:假设所有顶点均未被访问,从一个顶点V出发,依次访问它的各个邻接点,直到图中所有和V相通的点都被访问到,若还有未访问的顶点,重复以上过程
8
10
11
+ 栈
12
+
9
13
#### 广度优先BFS(Breadth First Search)
14
+
15
+ 如:树的层次遍历
16
+
17
+ 思想: 从顶点V出发,访问V之后依次访问v的各个未曾访问过的邻接点,然后分别从这些邻接点出发依次访问它们的邻接点
18
+
19
+ 队列
Original file line number Diff line number Diff line change 1
1
package bfs
2
+
3
+ import "github.com/xiaomeng79/go-algorithm/data-structures/graph"
4
+
5
+ //有向图的广度优先搜索
6
+ func Bfs (g * graph.DirGraph , start graph.VertexId , fn func (graph.VertexId )) {
7
+ queue := []graph.VertexId {start }
8
+ visited := make (map [graph.VertexId ]bool )
9
+
10
+ var next []graph.VertexId
11
+
12
+ for len (queue ) > 0 {
13
+ next = []graph.VertexId {}
14
+ for _ , vertex := range queue {
15
+ visited [vertex ] = true
16
+ //获取邻接点
17
+ neighbours := g .GetNeighbours (vertex ).VerticesIter ()
18
+ fn (vertex )
19
+ for neighbour := range neighbours {
20
+ if _ , ok := visited [neighbour ]; ! ok {
21
+ next = append (next , neighbour )
22
+ }
23
+ }
24
+ }
25
+ queue = next
26
+ }
27
+ }
Original file line number Diff line number Diff line change 1
1
package bfs
2
+
3
+ import (
4
+ "github.com/stretchr/testify/assert"
5
+ "github.com/xiaomeng79/go-algorithm/data-structures/graph"
6
+ "testing"
7
+ )
8
+
9
+ func TestBfs (t * testing.T ) {
10
+ h := graph .NewDirected ()
11
+
12
+ for i := 0 ; i < 10 ; i ++ {
13
+ h .AddVertex (graph .VertexId (i ))
14
+ }
15
+
16
+ for i := 0 ; i < 9 ; i ++ {
17
+ h .AddEdge (graph .VertexId (i ), graph .VertexId (i + 1 ), 1 )
18
+ }
19
+ count := 0
20
+ Bfs (h , graph .VertexId (3 ), func (id graph.VertexId ) {
21
+ count += int (id )
22
+ })
23
+ assert .Equal (t , 42 , count )
24
+ }
Original file line number Diff line number Diff line change @@ -11,18 +11,18 @@ func TestUndirectedDfs(t *testing.T) {
11
11
h := graph .NewUndirected ()
12
12
13
13
//增加顶点,
14
- for i := 0 ; i < 10 ; i ++ {
14
+ for i := 0 ; i < 10 ; i ++ {
15
15
h .AddVertex (graph .VertexId (i ))
16
16
}
17
17
//增加边
18
- for i := 0 ; i < 9 ; i ++ {
19
- h .AddEdge (graph .VertexId (i ),graph .VertexId (i + 1 ),1 )
18
+ for i := 0 ; i < 9 ; i ++ {
19
+ h .AddEdge (graph .VertexId (i ), graph .VertexId (i + 1 ), 1 )
20
20
}
21
21
counter := 0
22
- UndirectedDfs (h ,graph .VertexId (4 ), func (id graph.VertexId ) {
22
+ UndirectedDfs (h , graph .VertexId (4 ), func (id graph.VertexId ) {
23
23
counter += int (id )
24
24
})
25
- assert .Equal (t ,45 ,counter )
25
+ assert .Equal (t , 45 , counter )
26
26
}
27
27
28
28
func TestDirectedDfs (t * testing.T ) {
@@ -43,5 +43,5 @@ func TestDirectedDfs(t *testing.T) {
43
43
counter += int (v )
44
44
})
45
45
46
- assert .Equal (t ,42 ,counter )
47
- }
46
+ assert .Equal (t , 42 , counter )
47
+ }
You can’t perform that action at this time.
0 commit comments