Files
hello-algo/en/codes/go/chapter_graph/graph_bfs.go
Yudong Jin 2778a6f9c7 Translate all code to English (#1836)
* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
2025-12-31 07:44:52 +08:00

42 lines
1.2 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 */
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
func graphBFS(g *graphAdjList, startVet Vertex) []Vertex {
// Vertex traversal sequence
res := make([]Vertex, 0)
// Hash set for recording vertices that have been visited
visited := make(map[Vertex]struct{})
visited[startVet] = struct{}{}
// Queue used to implement BFS, using slice to simulate queue
queue := make([]Vertex, 0)
queue = append(queue, startVet)
// Starting from vertex vet, loop until all vertices are visited
for len(queue) > 0 {
// Dequeue the front vertex
vet := queue[0]
queue = queue[1:]
// Record visited vertex
res = append(res, vet)
// Traverse all adjacent vertices of this vertex
for _, adjVet := range g.adjList[vet] {
_, isExist := visited[adjVet]
// Only enqueue unvisited vertices
if !isExist {
queue = append(queue, adjVet)
visited[adjVet] = struct{}{}
}
}
}
// Return vertex traversal sequence
return res
}