Breadth-First Search Examples:

Java
Java
Python
Python
PHP
PHP
C#
C#
C++
C++
TypeScript
TypeScript
▸ Breadth-First Search Quick Review

Breadth-first Search in Java

import java.util.LinkedList;
import java.util.Queue;

public class BFS {

public static class Graph {
    LinkedList<Integer>[] adj;
    int size;
}

public static void bfs(Graph G, int startVert) {
    
    boolean[] visited = new boolean[G.size];
    Queue<Integer> q = new LinkedList<>();
    
    visited[startVert] = true;
    
    q.add(startVert);
    
    while(!q.isEmpty()) {
        int v = q.remove();
        for(int adjV: G.adj[v]) {
            if(!visited[adjV]) {
                visited[adjV] = true;
                q.add(adjV);
            }
        }
    }
    
}

Find any bugs in the code? let us know!