Created
January 31, 2021 13:07
-
-
Save phc15/0d080dd87289f522b66250750f19d1ac to your computer and use it in GitHub Desktop.
This file contains 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
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