Last active
September 4, 2022 12:34
-
-
Save shyamvlmna/80c1e378395bb6eb9735e165b8f81aa5 to your computer and use it in GitHub Desktop.
ALGORITHM FOR BREADTH FIRST SEARCH IN A GRAPH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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