Skip to content

Commit f156334

Browse files
committed
graph
1 parent 2210961 commit f156334

File tree

19 files changed

+114
-8
lines changed

19 files changed

+114
-8
lines changed

algorithms/graphs/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## 图算法
2+
3+
### 搜索
4+
5+
#### 深度优先DFS(Depth First Search)
6+
7+
思想:假设所有顶点均未被访问,从一个顶点V出发,依次访问它的各个邻接点,直到图中所有和V相通的点都被访问到,若还有未访问的顶点,重复以上过程
8+
9+
#### 广度优先BFS(Breadth First Search)

algorithms/graphs/bfs/bfs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package bfs

algorithms/graphs/bfs/bfs_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package bfs

algorithms/graphs/dfs/dfs.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package dfs
2+
3+
import (
4+
"github.com/xiaomeng79/go-algorithm/data-structures/graph"
5+
"github.com/xiaomeng79/go-algorithm/data-structures/stack"
6+
)
7+
8+
//无向图的深度搜索
9+
10+
func UndirectedDfs(g *graph.UnGraph, v graph.VertexId, fn func(graph.VertexId)) {
11+
s := stack.New() //初始化一个栈
12+
s.Push(int(v))
13+
visited := make(map[graph.VertexId]bool) //记录是否访问过
14+
15+
for s.Len() > 0 { //只要栈里面有,就一直
16+
v, _ := s.Pop()
17+
_v := graph.VertexId(v)
18+
if _, ok := visited[_v]; !ok {
19+
visited[_v] = true
20+
fn(_v)
21+
//将此结点的邻接点压栈
22+
neighbours := g.GetNeighbours(_v).VerticesIter()
23+
for neighbour := range neighbours {
24+
s.Push(int(neighbour))
25+
}
26+
}
27+
}
28+
}
29+
30+
//有向图的深度搜索
31+
func DirectedDfs(g *graph.DirGraph, v graph.VertexId, fn func(graph.VertexId)) {
32+
s := stack.New()
33+
s.Push(int(v))
34+
visited := make(map[graph.VertexId]bool)
35+
36+
for s.Len() > 0 {
37+
v, _ := s.Pop()
38+
_v := graph.VertexId(v)
39+
if _, ok := visited[_v]; !ok {
40+
visited[_v] = true
41+
fn(_v)
42+
neighbours := g.GetSuccessors(_v).VerticesIter()
43+
for neighbour := range neighbours {
44+
s.Push(int(neighbour))
45+
}
46+
}
47+
}
48+
}

algorithms/graphs/dfs/dfs_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dfs
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"github.com/xiaomeng79/go-algorithm/data-structures/graph"
6+
"testing"
7+
)
8+
9+
func TestUndirectedDfs(t *testing.T) {
10+
//建立一个无向图
11+
h := graph.NewUndirected()
12+
13+
//增加顶点,
14+
for i:=0; i<10; i++ {
15+
h.AddVertex(graph.VertexId(i))
16+
}
17+
//增加边
18+
for i:=0;i<9;i++ {
19+
h.AddEdge(graph.VertexId(i),graph.VertexId(i+1),1)
20+
}
21+
counter := 0
22+
UndirectedDfs(h,graph.VertexId(4), func(id graph.VertexId) {
23+
counter += int(id)
24+
})
25+
assert.Equal(t,45,counter)
26+
}
27+
28+
func TestDirectedDfs(t *testing.T) {
29+
//建立一个有向图
30+
h := graph.NewDirected()
31+
32+
for i := 0; i < 10; i++ {
33+
v := graph.VertexId(i)
34+
h.AddVertex(v)
35+
}
36+
37+
for i := 0; i < 9; i++ {
38+
h.AddEdge(graph.VertexId(i), graph.VertexId(i+1), 1)
39+
}
40+
41+
counter := 0
42+
DirectedDfs(h, graph.VertexId(3), func(v graph.VertexId) {
43+
counter += int(v)
44+
})
45+
46+
assert.Equal(t,42,counter)
47+
}
File renamed without changes.
File renamed without changes.

sort/bubble/bubble_test.go renamed to algorithms/sort/bubble/bubble_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package bubble
22

33
import (
44
"github.com/stretchr/testify/assert"
5-
"github.com/xiaomeng79/go-algorithm/sort/testdata"
6-
"github.com/xiaomeng79/go-algorithm/sort/utils"
5+
"github.com/xiaomeng79/go-algorithm/algorithms/sort/testdata"
6+
"github.com/xiaomeng79/go-algorithm/algorithms/sort/utils"
77
"testing"
88
)
99

File renamed without changes.

sort/insertion/insertion_test.go renamed to algorithms/sort/insertion/insertion_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package insertion
22

33
import (
44
"github.com/stretchr/testify/assert"
5-
"github.com/xiaomeng79/go-algorithm/sort/testdata"
6-
"github.com/xiaomeng79/go-algorithm/sort/utils"
5+
"github.com/xiaomeng79/go-algorithm/algorithms/sort/testdata"
6+
"github.com/xiaomeng79/go-algorithm/algorithms/sort/utils"
77
"testing"
88
)
99

0 commit comments

Comments
 (0)