mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 03:36:28 +08:00
870e3e5cb2
* Sync zh and zh-hant versions * Update en/README.md * Add a Q&A for chapter of introduction * Update the callout headers * Sync zh ang zh-hant versions * Bug fixes
36 lines
1,015 B
Go
36 lines
1,015 B
Go
// File: graph_dfs.go
|
||
// Created Time: 2023-02-18
|
||
// Author: Reanon (793584285@qq.com)
|
||
|
||
package chapter_graph
|
||
|
||
import (
|
||
. "github.com/krahets/hello-algo/pkg"
|
||
)
|
||
|
||
/* 深度優先走訪輔助函式 */
|
||
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)
|
||
}
|
||
}
|
||
}
|
||
|
||
/* 深度優先走訪 */
|
||
// 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點
|
||
func graphDFS(g *graphAdjList, startVet Vertex) []Vertex {
|
||
// 頂點走訪序列
|
||
res := make([]Vertex, 0)
|
||
// 雜湊集合,用於記錄已被訪問過的頂點
|
||
visited := make(map[Vertex]struct{})
|
||
dfs(g, visited, &res, startVet)
|
||
// 返回頂點走訪序列
|
||
return res
|
||
}
|