2023-02-27 20:34:30 +08:00
|
|
|
|
// File: graph_dfs.go
|
|
|
|
|
// Created Time: 2023-02-18
|
|
|
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
|
|
|
|
|
|
package chapter_graph
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
. "github.com/krahets/hello-algo/pkg"
|
|
|
|
|
)
|
|
|
|
|
|
2023-12-28 18:06:09 +08:00
|
|
|
|
/* 深度优先遍历辅助函数 */
|
2023-02-27 20:34:30 +08:00
|
|
|
|
func dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {
|
|
|
|
|
// append 操作会返回新的的引用,必须让原引用重新赋值为新slice的引用
|
|
|
|
|
*res = append(*res, vet)
|
|
|
|
|
visited[vet] = struct{}{}
|
|
|
|
|
// 遍历该顶点的所有邻接顶点
|
|
|
|
|
for _, adjVet := range g.adjList[vet] {
|
|
|
|
|
_, isExist := visited[adjVet]
|
|
|
|
|
// 递归访问邻接顶点
|
|
|
|
|
if !isExist {
|
|
|
|
|
dfs(g, visited, res, adjVet)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 18:06:09 +08:00
|
|
|
|
/* 深度优先遍历 */
|
2023-02-27 20:34:30 +08:00
|
|
|
|
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
|
|
|
|
func graphDFS(g *graphAdjList, startVet Vertex) []Vertex {
|
|
|
|
|
// 顶点遍历序列
|
|
|
|
|
res := make([]Vertex, 0)
|
2024-04-28 22:00:25 +08:00
|
|
|
|
// 哈希集合,用于记录已被访问过的顶点
|
2023-02-27 20:34:30 +08:00
|
|
|
|
visited := make(map[Vertex]struct{})
|
|
|
|
|
dfs(g, visited, &res, startVet)
|
|
|
|
|
// 返回顶点遍历序列
|
|
|
|
|
return res
|
|
|
|
|
}
|