Skip to content

Commit b36f258

Browse files
committed
bfs
1 parent f156334 commit b36f258

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

algorithms/graphs/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
#### 深度优先DFS(Depth First Search)
66

7+
如: 树的先序遍历
8+
79
思想:假设所有顶点均未被访问,从一个顶点V出发,依次访问它的各个邻接点,直到图中所有和V相通的点都被访问到,若还有未访问的顶点,重复以上过程
810

11+
12+
913
#### 广度优先BFS(Breadth First Search)
14+
15+
如:树的层次遍历
16+
17+
思想: 从顶点V出发,访问V之后依次访问v的各个未曾访问过的邻接点,然后分别从这些邻接点出发依次访问它们的邻接点
18+
19+
队列

algorithms/graphs/bfs/bfs.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
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+
}

algorithms/graphs/bfs/bfs_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
11
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+
}

algorithms/graphs/dfs/dfs_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ func TestUndirectedDfs(t *testing.T) {
1111
h := graph.NewUndirected()
1212

1313
//增加顶点,
14-
for i:=0; i<10; i++ {
14+
for i := 0; i < 10; i++ {
1515
h.AddVertex(graph.VertexId(i))
1616
}
1717
//增加边
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)
2020
}
2121
counter := 0
22-
UndirectedDfs(h,graph.VertexId(4), func(id graph.VertexId) {
22+
UndirectedDfs(h, graph.VertexId(4), func(id graph.VertexId) {
2323
counter += int(id)
2424
})
25-
assert.Equal(t,45,counter)
25+
assert.Equal(t, 45, counter)
2626
}
2727

2828
func TestDirectedDfs(t *testing.T) {
@@ -43,5 +43,5 @@ func TestDirectedDfs(t *testing.T) {
4343
counter += int(v)
4444
})
4545

46-
assert.Equal(t,42,counter)
47-
}
46+
assert.Equal(t, 42, counter)
47+
}

0 commit comments

Comments
 (0)