8000 update · mohuishou/go-algorithm@945fff2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 945fff2

Browse files
committed
update
1 parent 2edb428 commit 945fff2

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

Graph/Graph.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Graph
2+
3+
// EdgeType 边的权值类型
4+
type EdgeType int
5+
6+
// VextexType 顶点类型定义
7+
type VextexType int
8+
9+
// VextexDataType 顶点值类型定义
10+
type VextexDataType int
11+
12+
//EdgeNode 边的节点
13+
type EdgeNode struct {
14+
weight EdgeType //权值
15+
v VextexType //指向储存该顶点的下标
16+
next *EdgeNode //指向下一条边
17+
}
18+
19+
//VextexNode 顶点节点定义
20+
type VextexNode struct {
21+
data VextexDataType //顶点的值
22+
FisrtEdge *EdgeNode //该顶点指向的第一条边
23+
}
24+
25+
//Graph 图
26+
type Graph []VextexNode
27+
28+
//CreateGraph 创建邻接表
29+
func CreateGraph(n int) (graph Graph) {
30+
graph = make(Graph, n)
31+
for i := 0; i < n; i++ {
32+
graph[i] = VextexNode{}
33+
}
34+
return graph
35+
}
36+
37+
//AddEdge 添加边
38+
func (graph Graph) AddEdge(s, t VextexType, weight EdgeType) {
39+
edge := &EdgeNode{v: t, weight: weight}
40+
41+
//添加边到头部
42+
edge.next = graph[s].FisrtEdge
43+
graph[s].FisrtEdge = edge
44+
}

Graph/Graph_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Graph
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGraph(t *testing.T) {
8+
graph := CreateGraph(3)
9+
graph.AddEdge(0, 1, 1)
10+
graph.AddEdge(0, 2, 2)
11+
graph.AddEdge(1, 2, 3)
12+
}

Graph/README.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Graph
2+
====
3+
4+
建图

ShortestPath/Dijkstra.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
package ShortestPath
2+
3+
//Dijkstra 算法
4+
func Dijkstra() {
5+
6+
}

0 commit comments

Comments
 (0)
0