mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-30 16:25:36 +08:00
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
// File: graph_bfs.go
|
|
// Created Time: 2023-02-18
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
package chapter_graph
|
|
|
|
import (
|
|
. "github.com/krahets/hello-algo/pkg"
|
|
)
|
|
|
|
/* Breadth-first traversal */
|
|
// Represent the graph with an adjacency list to access all neighbors of a given vertex
|
|
func graphBFS(g *graphAdjList, startVet Vertex) []Vertex {
|
|
// Vertex traversal sequence
|
|
res := make([]Vertex, 0)
|
|
// Hash set recording visited vertices
|
|
visited := make(map[Vertex]struct{})
|
|
visited[startVet] = struct{}{}
|
|
// Use a queue for BFS, represented here by a slice
|
|
queue := make([]Vertex, 0)
|
|
queue = append(queue, startVet)
|
|
// Starting from startVet, loop until all reachable vertices have been visited
|
|
for len(queue) > 0 {
|
|
// Dequeue the front vertex
|
|
vet := queue[0]
|
|
queue = queue[1:]
|
|
// Record the visited vertex
|
|
res = append(res, vet)
|
|
// Traverse all neighbors of this vertex
|
|
for _, adjVet := range g.adjList[vet] {
|
|
_, isExist := visited[adjVet]
|
|
// Enqueue only unvisited vertices
|
|
if !isExist {
|
|
queue = append(queue, adjVet)
|
|
visited[adjVet] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
// Return the vertex traversal sequence
|
|
return res
|
|
}
|