mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-24 12:06:07 +00:00
feat(graph): add go codes (#314)
* feat(graph): add go codes * feat(go/graph): add graph_adjacency_list * doc(graph): add go codes * fix(graph): fix go codes * Update graph_adjacency_matrix_test.go --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
// File: graph_adjacency_matrix.go
|
||||
// Created Time: 2023-01-31
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_graph
|
||||
|
||||
import "fmt"
|
||||
|
||||
/* 基于邻接矩阵实现的无向图类 */
|
||||
type graphAdjMat struct {
|
||||
// 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
vertices []int
|
||||
// 邻接矩阵,行列索引对应“顶点索引”
|
||||
adjMat [][]int
|
||||
}
|
||||
|
||||
func newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {
|
||||
// 添加顶点
|
||||
n := len(vertices)
|
||||
adjMat := make([][]int, n)
|
||||
for i := range adjMat {
|
||||
adjMat[i] = make([]int, n)
|
||||
}
|
||||
// 初始化图
|
||||
g := &graphAdjMat{
|
||||
vertices: vertices,
|
||||
adjMat: adjMat,
|
||||
}
|
||||
// 添加边
|
||||
// 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
|
||||
for i := range edges {
|
||||
g.addEdge(edges[i][0], edges[i][1])
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
/* 获取顶点数量 */
|
||||
func (g *graphAdjMat) size() int {
|
||||
return len(g.vertices)
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
func (g *graphAdjMat) addVertex(val int) {
|
||||
n := g.size()
|
||||
// 向顶点列表中添加新顶点的值
|
||||
g.vertices = append(g.vertices, val)
|
||||
// 在邻接矩阵中添加一行
|
||||
newRow := make([]int, n)
|
||||
g.adjMat = append(g.adjMat, newRow)
|
||||
// 在邻接矩阵中添加一列
|
||||
for i := range g.adjMat {
|
||||
g.adjMat[i] = append(g.adjMat[i], 0)
|
||||
}
|
||||
}
|
||||
|
||||
/* 删除顶点 */
|
||||
func (g *graphAdjMat) removeVertex(index int) {
|
||||
if index >= g.size() {
|
||||
return
|
||||
}
|
||||
// 在顶点列表中移除索引 index 的顶点
|
||||
g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)
|
||||
// 在邻接矩阵中删除索引 index 的行
|
||||
g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)
|
||||
// 在邻接矩阵中删除索引 index 的列
|
||||
for i := range g.adjMat {
|
||||
g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
/* 添加边 */
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
func (g *graphAdjMat) addEdge(i, j int) {
|
||||
// 索引越界与相等处理
|
||||
if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {
|
||||
fmt.Errorf("%s", "Index Out Of Bounds Exception")
|
||||
}
|
||||
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
|
||||
g.adjMat[i][j] = 1
|
||||
g.adjMat[j][i] = 1
|
||||
}
|
||||
|
||||
/* 删除边 */
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
func (g *graphAdjMat) removeEdge(i, j int) {
|
||||
// 索引越界与相等处理
|
||||
if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {
|
||||
fmt.Errorf("%s", "Index Out Of Bounds Exception")
|
||||
}
|
||||
g.adjMat[i][j] = 0
|
||||
g.adjMat[j][i] = 0
|
||||
}
|
||||
|
||||
/* 打印邻接矩阵 */
|
||||
func (g *graphAdjMat) print() {
|
||||
fmt.Printf("\t顶点列表 = %v\n", g.vertices)
|
||||
fmt.Printf("\t邻接矩阵 = \n")
|
||||
for i := range g.adjMat {
|
||||
fmt.Printf("\t\t\t%v\n", g.adjMat[i])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user