2023-02-05 16:08:08 +08:00
|
|
|
|
// File: graph_adjacency_list.go
|
|
|
|
|
// Created Time: 2023-01-31
|
|
|
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
|
|
|
|
|
|
package chapter_graph
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2023-02-27 20:34:30 +08:00
|
|
|
|
. "github.com/krahets/hello-algo/pkg"
|
|
|
|
|
)
|
2023-02-05 16:08:08 +08:00
|
|
|
|
|
|
|
|
|
/* 基于邻接表实现的无向图类 */
|
|
|
|
|
type graphAdjList struct {
|
2023-12-02 06:21:34 +08:00
|
|
|
|
// 邻接表,key:顶点,value:该顶点的所有邻接顶点
|
2023-02-27 20:34:30 +08:00
|
|
|
|
adjList map[Vertex][]Vertex
|
2023-02-05 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 16:37:56 +08:00
|
|
|
|
/* 构造函数 */
|
2023-02-27 20:34:30 +08:00
|
|
|
|
func newGraphAdjList(edges [][]Vertex) *graphAdjList {
|
2023-02-05 16:08:08 +08:00
|
|
|
|
g := &graphAdjList{
|
2023-02-27 20:34:30 +08:00
|
|
|
|
adjList: make(map[Vertex][]Vertex),
|
2023-02-05 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
// 添加所有顶点和边
|
|
|
|
|
for _, edge := range edges {
|
|
|
|
|
g.addVertex(edge[0])
|
|
|
|
|
g.addVertex(edge[1])
|
|
|
|
|
g.addEdge(edge[0], edge[1])
|
|
|
|
|
}
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取顶点数量 */
|
|
|
|
|
func (g *graphAdjList) size() int {
|
|
|
|
|
return len(g.adjList)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加边 */
|
2023-02-27 20:34:30 +08:00
|
|
|
|
func (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {
|
2023-02-05 16:08:08 +08:00
|
|
|
|
_, ok1 := g.adjList[vet1]
|
|
|
|
|
_, ok2 := g.adjList[vet2]
|
|
|
|
|
if !ok1 || !ok2 || vet1 == vet2 {
|
|
|
|
|
panic("error")
|
|
|
|
|
}
|
|
|
|
|
// 添加边 vet1 - vet2, 添加匿名 struct{},
|
2023-02-27 20:34:30 +08:00
|
|
|
|
g.adjList[vet1] = append(g.adjList[vet1], vet2)
|
|
|
|
|
g.adjList[vet2] = append(g.adjList[vet2], vet1)
|
2023-02-05 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除边 */
|
2023-02-27 20:34:30 +08:00
|
|
|
|
func (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {
|
2023-02-05 16:08:08 +08:00
|
|
|
|
_, ok1 := g.adjList[vet1]
|
|
|
|
|
_, ok2 := g.adjList[vet2]
|
|
|
|
|
if !ok1 || !ok2 || vet1 == vet2 {
|
|
|
|
|
panic("error")
|
|
|
|
|
}
|
2023-02-27 20:34:30 +08:00
|
|
|
|
// 删除边 vet1 - vet2
|
2023-07-04 21:08:32 +08:00
|
|
|
|
g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)
|
|
|
|
|
g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)
|
2023-02-05 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加顶点 */
|
2023-02-27 20:34:30 +08:00
|
|
|
|
func (g *graphAdjList) addVertex(vet Vertex) {
|
2023-02-05 16:08:08 +08:00
|
|
|
|
_, ok := g.adjList[vet]
|
|
|
|
|
if ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-02-15 03:35:54 +08:00
|
|
|
|
// 在邻接表中添加一个新链表
|
2023-02-27 20:34:30 +08:00
|
|
|
|
g.adjList[vet] = make([]Vertex, 0)
|
2023-02-05 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除顶点 */
|
2023-02-27 20:34:30 +08:00
|
|
|
|
func (g *graphAdjList) removeVertex(vet Vertex) {
|
2023-02-05 16:08:08 +08:00
|
|
|
|
_, ok := g.adjList[vet]
|
|
|
|
|
if !ok {
|
|
|
|
|
panic("error")
|
|
|
|
|
}
|
|
|
|
|
// 在邻接表中删除顶点 vet 对应的链表
|
|
|
|
|
delete(g.adjList, vet)
|
2023-04-09 19:12:37 +08:00
|
|
|
|
// 遍历其他顶点的链表,删除所有包含 vet 的边
|
2023-07-04 21:08:32 +08:00
|
|
|
|
for v, list := range g.adjList {
|
|
|
|
|
g.adjList[v] = DeleteSliceElms(list, vet)
|
2023-02-05 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 打印邻接表 */
|
|
|
|
|
func (g *graphAdjList) print() {
|
|
|
|
|
var builder strings.Builder
|
|
|
|
|
fmt.Printf("邻接表 = \n")
|
|
|
|
|
for k, v := range g.adjList {
|
2023-02-27 20:34:30 +08:00
|
|
|
|
builder.WriteString("\t\t" + strconv.Itoa(k.Val) + ": ")
|
|
|
|
|
for _, vet := range v {
|
|
|
|
|
builder.WriteString(strconv.Itoa(vet.Val) + " ")
|
2023-02-05 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
fmt.Println(builder.String())
|
|
|
|
|
builder.Reset()
|
|
|
|
|
}
|
|
|
|
|
}
|