Skip to content

Instantly share code, notes, and snippets.

@phc15
Created January 31, 2021 13:07
Show Gist options
  • Save phc15/0d080dd87289f522b66250750f19d1ac to your computer and use it in GitHub Desktop.
Save phc15/0d080dd87289f522b66250750f19d1ac to your computer and use it in GitHub Desktop.
void bfs(Graph graph, int s, boolean[] visited) {
Queue<Integer> queue = new LinkedList<Integer>();
visited[s] = true;
queue.add(s); // enqueue source s
while (!queue.isEmpty()) {
int S = queue.poll(); // dequeue
System.out.println("Visited vertex: " + S);
// check the current vertex's adjacent vertices
for (int n : graph.adj.get(S)) {
if (!visited[n]) {
visited[n] = true;
queue.add(n);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment