Skip to content

Instantly share code, notes, and snippets.

@shyamvlmna
Last active September 4, 2022 12:34
Show Gist options
  • Save shyamvlmna/80c1e378395bb6eb9735e165b8f81aa5 to your computer and use it in GitHub Desktop.
Save shyamvlmna/80c1e378395bb6eb9735e165b8f81aa5 to your computer and use it in GitHub Desktop.
ALGORITHM FOR BREADTH FIRST SEARCH IN A GRAPH
ALGORITHM FOR BREADTH FIRST SEARCH IN A GRAPH
have a hashmap to add visited nodes
have a queue to keep visited nodes
start from the root node
add root to visited
push root to queue
for queue is not empty
pop node from queue
process the node
check for neighbours of the node
if neighbour not visited already
add to visited
push to queue
if not visited
continue
#Go Code
queue := new(Queue)
visited := make(map[string]bool)
for queue.Length != 0 {
node := queue.pop
process(node)
for id, conn := range node.Connections {
if !visited[id] {
visited[id] = true
queue.Push(node)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment