← Back

Breadth-First Search (BFS)

Explore a graph level by level with a queue — the basis for shortest paths on unweighted graphs.

1 / 1
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {

}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // A FIFO queue is what makes the traversal go level by level.
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // A FIFO queue is what makes the traversal go level by level.
    visited := make([]bool, len(graph))
    order := []int{}
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // A FIFO queue is what makes the traversal go level by level.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // A FIFO queue is what makes the traversal go level by level.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    queue := []int{start}
    visited[start] = true
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // A FIFO queue is what makes the traversal go level by level.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    queue := []int{start}
    visited[start] = true

        // Take the front of the queue (FIFO) and record it.
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // A FIFO queue is what makes the traversal go level by level.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    queue := []int{start}
    visited[start] = true

    for len(queue) > 0 {
        // Take the front of the queue (FIFO) and record it.
        node := queue[0]
        queue = queue[1:]
        order = append(order, node)
    }
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // A FIFO queue is what makes the traversal go level by level.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    queue := []int{start}
    visited[start] = true

    for len(queue) > 0 {
        // Take the front of the queue (FIFO) and record it.
        node := queue[0]
        queue = queue[1:]
        order = append(order, node)

        // Enqueue every unvisited neighbor, marking it now to keep it unique.
    }
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // A FIFO queue is what makes the traversal go level by level.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    queue := []int{start}
    visited[start] = true

    for len(queue) > 0 {
        // Take the front of the queue (FIFO) and record it.
        node := queue[0]
        queue = queue[1:]
        order = append(order, node)

        // Enqueue every unvisited neighbor, marking it now to keep it unique.
        for _, next := range graph[node] {
            if !visited[next] {
                visited[next] = true
                queue = append(queue, next)
            }
        }
    }
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // A FIFO queue is what makes the traversal go level by level.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    queue := []int{start}
    visited[start] = true

    for len(queue) > 0 {
        // Take the front of the queue (FIFO) and record it.
        node := queue[0]
        queue = queue[1:]
        order = append(order, node)

        // Enqueue every unvisited neighbor, marking it now to keep it unique.
        for _, next := range graph[node] {
            if !visited[next] {
                visited[next] = true
                queue = append(queue, next)
            }
        }
    }

    return order // O(V + E)
}
from collections import deque

# graph is an adjacency list: graph[node] = list of neighbors.
def bfs(graph, start):
    pass
from collections import deque

# graph is an adjacency list: graph[node] = list of neighbors.
def bfs(graph, start):
    # A FIFO queue is what makes the traversal go level by level.
from collections import deque

# graph is an adjacency list: graph[node] = list of neighbors.
def bfs(graph, start):
    # A FIFO queue is what makes the traversal go level by level.
    visited = [False] * len(graph)
    order = []
from collections import deque

# graph is an adjacency list: graph[node] = list of neighbors.
def bfs(graph, start):
    # A FIFO queue is what makes the traversal go level by level.
    visited = [False] * len(graph)
    order = []

    # Seed the queue with the start node. Mark nodes visited when they ENTER
    # the queue, not when they leave — that avoids enqueuing them twice.
from collections import deque

# graph is an adjacency list: graph[node] = list of neighbors.
def bfs(graph, start):
    # A FIFO queue is what makes the traversal go level by level.
    visited = [False] * len(graph)
    order = []

    # Seed the queue with the start node. Mark nodes visited when they ENTER
    # the queue, not when they leave — that avoids enqueuing them twice.
    queue = deque([start])
    visited[start] = True
from collections import deque

# graph is an adjacency list: graph[node] = list of neighbors.
def bfs(graph, start):
    # A FIFO queue is what makes the traversal go level by level.
    visited = [False] * len(graph)
    order = []

    # Seed the queue with the start node. Mark nodes visited when they ENTER
    # the queue, not when they leave — that avoids enqueuing them twice.
    queue = deque([start])
    visited[start] = True

        # Take the front of the queue (FIFO) and record it.
from collections import deque

# graph is an adjacency list: graph[node] = list of neighbors.
def bfs(graph, start):
    # A FIFO queue is what makes the traversal go level by level.
    visited = [False] * len(graph)
    order = []

    # Seed the queue with the start node. Mark nodes visited when they ENTER
    # the queue, not when they leave — that avoids enqueuing them twice.
    queue = deque([start])
    visited[start] = True

    while queue:
        # Take the front of the queue (FIFO) and record it.
        node = queue.popleft()
        order.append(node)
from collections import deque

# graph is an adjacency list: graph[node] = list of neighbors.
def bfs(graph, start):
    # A FIFO queue is what makes the traversal go level by level.
    visited = [False] * len(graph)
    order = []

    # Seed the queue with the start node. Mark nodes visited when they ENTER
    # the queue, not when they leave — that avoids enqueuing them twice.
    queue = deque([start])
    visited[start] = True

    while queue:
        # Take the front of the queue (FIFO) and record it.
        node = queue.popleft()
        order.append(node)

        # Enqueue every unvisited neighbor, marking it now to keep it unique.
from collections import deque

# graph is an adjacency list: graph[node] = list of neighbors.
def bfs(graph, start):
    # A FIFO queue is what makes the traversal go level by level.
    visited = [False] * len(graph)
    order = []

    # Seed the queue with the start node. Mark nodes visited when they ENTER
    # the queue, not when they leave — that avoids enqueuing them twice.
    queue = deque([start])
    visited[start] = True

    while queue:
        # Take the front of the queue (FIFO) and record it.
        node = queue.popleft()
        order.append(node)

        # Enqueue every unvisited neighbor, marking it now to keep it unique.
        for nxt in graph[node]:
            if not visited[nxt]:
                visited[nxt] = True
                queue.append(nxt)
from collections import deque

# graph is an adjacency list: graph[node] = list of neighbors.
def bfs(graph, start):
    # A FIFO queue is what makes the traversal go level by level.
    visited = [False] * len(graph)
    order = []

    # Seed the queue with the start node. Mark nodes visited when they ENTER
    # the queue, not when they leave — that avoids enqueuing them twice.
    queue = deque([start])
    visited[start] = True

    while queue:
        # Take the front of the queue (FIFO) and record it.
        node = queue.popleft()
        order.append(node)

        # Enqueue every unvisited neighbor, marking it now to keep it unique.
        for nxt in graph[node]:
            if not visited[nxt]:
                visited[nxt] = True
                queue.append(nxt)

    return order  # O(V + E)
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph, start) {

}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph, start) {
  // A FIFO queue is what makes the traversal go level by level.
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph, start) {
  // A FIFO queue is what makes the traversal go level by level.
  const visited = new Array(graph.length).fill(false);
  const order = [];
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph, start) {
  // A FIFO queue is what makes the traversal go level by level.
  const visited = new Array(graph.length).fill(false);
  const order = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph, start) {
  // A FIFO queue is what makes the traversal go level by level.
  const visited = new Array(graph.length).fill(false);
  const order = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue = [start];
  let head = 0;
  visited[start] = true;
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph, start) {
  // A FIFO queue is what makes the traversal go level by level.
  const visited = new Array(graph.length).fill(false);
  const order = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue = [start];
  let head = 0;
  visited[start] = true;

    // Take the front. A moving index beats shift(), which is O(n).
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph, start) {
  // A FIFO queue is what makes the traversal go level by level.
  const visited = new Array(graph.length).fill(false);
  const order = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue = [start];
  let head = 0;
  visited[start] = true;

  while (head < queue.length) {
    // Take the front. A moving index beats shift(), which is O(n).
    const node = queue[head++];
    order.push(node);
  }
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph, start) {
  // A FIFO queue is what makes the traversal go level by level.
  const visited = new Array(graph.length).fill(false);
  const order = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue = [start];
  let head = 0;
  visited[start] = true;

  while (head < queue.length) {
    // Take the front. A moving index beats shift(), which is O(n).
    const node = queue[head++];
    order.push(node);

    // Enqueue every unvisited neighbor, marking it now to keep it unique.
  }
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph, start) {
  // A FIFO queue is what makes the traversal go level by level.
  const visited = new Array(graph.length).fill(false);
  const order = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue = [start];
  let head = 0;
  visited[start] = true;

  while (head < queue.length) {
    // Take the front. A moving index beats shift(), which is O(n).
    const node = queue[head++];
    order.push(node);

    // Enqueue every unvisited neighbor, marking it now to keep it unique.
    for (const next of graph[node]) {
      if (!visited[next]) {
        visited[next] = true;
        queue.push(next);
      }
    }
  }
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph, start) {
  // A FIFO queue is what makes the traversal go level by level.
  const visited = new Array(graph.length).fill(false);
  const order = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue = [start];
  let head = 0;
  visited[start] = true;

  while (head < queue.length) {
    // Take the front. A moving index beats shift(), which is O(n).
    const node = queue[head++];
    order.push(node);

    // Enqueue every unvisited neighbor, marking it now to keep it unique.
    for (const next of graph[node]) {
      if (!visited[next]) {
        visited[next] = true;
        queue.push(next);
      }
    }
  }

  return order; // O(V + E)
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph: number[][], start: number): number[] {

}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph: number[][], start: number): number[] {
  // A FIFO queue is what makes the traversal go level by level.
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph: number[][], start: number): number[] {
  // A FIFO queue is what makes the traversal go level by level.
  const visited: boolean[] = new Array(graph.length).fill(false);
  const order: number[] = [];
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph: number[][], start: number): number[] {
  // A FIFO queue is what makes the traversal go level by level.
  const visited: boolean[] = new Array(graph.length).fill(false);
  const order: number[] = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph: number[][], start: number): number[] {
  // A FIFO queue is what makes the traversal go level by level.
  const visited: boolean[] = new Array(graph.length).fill(false);
  const order: number[] = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue: number[] = [start];
  let head = 0;
  visited[start] = true;
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph: number[][], start: number): number[] {
  // A FIFO queue is what makes the traversal go level by level.
  const visited: boolean[] = new Array(graph.length).fill(false);
  const order: number[] = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue: number[] = [start];
  let head = 0;
  visited[start] = true;

    // Take the front. A moving index beats shift(), which is O(n).
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph: number[][], start: number): number[] {
  // A FIFO queue is what makes the traversal go level by level.
  const visited: boolean[] = new Array(graph.length).fill(false);
  const order: number[] = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue: number[] = [start];
  let head = 0;
  visited[start] = true;

  while (head < queue.length) {
    // Take the front. A moving index beats shift(), which is O(n).
    const node = queue[head++];
    order.push(node);
  }
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph: number[][], start: number): number[] {
  // A FIFO queue is what makes the traversal go level by level.
  const visited: boolean[] = new Array(graph.length).fill(false);
  const order: number[] = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue: number[] = [start];
  let head = 0;
  visited[start] = true;

  while (head < queue.length) {
    // Take the front. A moving index beats shift(), which is O(n).
    const node = queue[head++];
    order.push(node);

    // Enqueue every unvisited neighbor, marking it now to keep it unique.
  }
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph: number[][], start: number): number[] {
  // A FIFO queue is what makes the traversal go level by level.
  const visited: boolean[] = new Array(graph.length).fill(false);
  const order: number[] = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue: number[] = [start];
  let head = 0;
  visited[start] = true;

  while (head < queue.length) {
    // Take the front. A moving index beats shift(), which is O(n).
    const node = queue[head++];
    order.push(node);

    // Enqueue every unvisited neighbor, marking it now to keep it unique.
    for (const next of graph[node]) {
      if (!visited[next]) {
        visited[next] = true;
        queue.push(next);
      }
    }
  }
}
// graph is an adjacency list: graph[node] = list of neighbors.
function bfs(graph: number[][], start: number): number[] {
  // A FIFO queue is what makes the traversal go level by level.
  const visited: boolean[] = new Array(graph.length).fill(false);
  const order: number[] = [];

  // Seed the queue with the start node. Mark nodes visited when they ENTER
  // the queue, not when they leave — that avoids enqueuing them twice.
  const queue: number[] = [start];
  let head = 0;
  visited[start] = true;

  while (head < queue.length) {
    // Take the front. A moving index beats shift(), which is O(n).
    const node = queue[head++];
    order.push(node);

    // Enqueue every unvisited neighbor, marking it now to keep it unique.
    for (const next of graph[node]) {
      if (!visited[next]) {
        visited[next] = true;
        queue.push(next);
      }
    }
  }

  return order; // O(V + E)
}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
    // graph is an adjacency list: graph.get(node) = list of neighbors.
    public List<Integer> bfs(List<List<Integer>> graph, int start) {

    }
}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
    // graph is an adjacency list: graph.get(node) = list of neighbors.
    public List<Integer> bfs(List<List<Integer>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
    }
}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
    // graph is an adjacency list: graph.get(node) = list of neighbors.
    public List<Integer> bfs(List<List<Integer>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        boolean[] visited = new boolean[graph.size()];
        List<Integer> order = new ArrayList<>();
    }
}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
    // graph is an adjacency list: graph.get(node) = list of neighbors.
    public List<Integer> bfs(List<List<Integer>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        boolean[] visited = new boolean[graph.size()];
        List<Integer> order = new ArrayList<>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
    }
}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
    // graph is an adjacency list: graph.get(node) = list of neighbors.
    public List<Integer> bfs(List<List<Integer>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        boolean[] visited = new boolean[graph.size()];
        List<Integer> order = new ArrayList<>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Deque<Integer> queue = new ArrayDeque<>();
        queue.add(start);
        visited[start] = true;
    }
}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
    // graph is an adjacency list: graph.get(node) = list of neighbors.
    public List<Integer> bfs(List<List<Integer>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        boolean[] visited = new boolean[graph.size()];
        List<Integer> order = new ArrayList<>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Deque<Integer> queue = new ArrayDeque<>();
        queue.add(start);
        visited[start] = true;

            // Take the front of the queue (FIFO) and record it.
    }
}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
    // graph is an adjacency list: graph.get(node) = list of neighbors.
    public List<Integer> bfs(List<List<Integer>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        boolean[] visited = new boolean[graph.size()];
        List<Integer> order = new ArrayList<>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Deque<Integer> queue = new ArrayDeque<>();
        queue.add(start);
        visited[start] = true;

        while (!queue.isEmpty()) {
            // Take the front of the queue (FIFO) and record it.
            int node = queue.poll();
            order.add(node);
        }
    }
}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
    // graph is an adjacency list: graph.get(node) = list of neighbors.
    public List<Integer> bfs(List<List<Integer>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        boolean[] visited = new boolean[graph.size()];
        List<Integer> order = new ArrayList<>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Deque<Integer> queue = new ArrayDeque<>();
        queue.add(start);
        visited[start] = true;

        while (!queue.isEmpty()) {
            // Take the front of the queue (FIFO) and record it.
            int node = queue.poll();
            order.add(node);

            // Enqueue every unvisited neighbor, marking it now to keep it unique.
        }
    }
}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
    // graph is an adjacency list: graph.get(node) = list of neighbors.
    public List<Integer> bfs(List<List<Integer>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        boolean[] visited = new boolean[graph.size()];
        List<Integer> order = new ArrayList<>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Deque<Integer> queue = new ArrayDeque<>();
        queue.add(start);
        visited[start] = true;

        while (!queue.isEmpty()) {
            // Take the front of the queue (FIFO) and record it.
            int node = queue.poll();
            order.add(node);

            // Enqueue every unvisited neighbor, marking it now to keep it unique.
            for (int next : graph.get(node)) {
                if (!visited[next]) {
                    visited[next] = true;
                    queue.add(next);
                }
            }
        }
    }
}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
    // graph is an adjacency list: graph.get(node) = list of neighbors.
    public List<Integer> bfs(List<List<Integer>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        boolean[] visited = new boolean[graph.size()];
        List<Integer> order = new ArrayList<>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Deque<Integer> queue = new ArrayDeque<>();
        queue.add(start);
        visited[start] = true;

        while (!queue.isEmpty()) {
            // Take the front of the queue (FIFO) and record it.
            int node = queue.poll();
            order.add(node);

            // Enqueue every unvisited neighbor, marking it now to keep it unique.
            for (int next : graph.get(node)) {
                if (!visited[next]) {
                    visited[next] = true;
                    queue.add(next);
                }
            }
        }

        return order; // O(V + E)
    }
}
using System.Collections.Generic;

public class Solution {
    // graph is an adjacency list: graph[node] = list of neighbors.
    public IList<int> Bfs(IList<IList<int>> graph, int start) {

    }
}
using System.Collections.Generic;

public class Solution {
    // graph is an adjacency list: graph[node] = list of neighbors.
    public IList<int> Bfs(IList<IList<int>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
    }
}
using System.Collections.Generic;

public class Solution {
    // graph is an adjacency list: graph[node] = list of neighbors.
    public IList<int> Bfs(IList<IList<int>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        bool[] visited = new bool[graph.Count];
        List<int> order = new List<int>();
    }
}
using System.Collections.Generic;

public class Solution {
    // graph is an adjacency list: graph[node] = list of neighbors.
    public IList<int> Bfs(IList<IList<int>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        bool[] visited = new bool[graph.Count];
        List<int> order = new List<int>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
    }
}
using System.Collections.Generic;

public class Solution {
    // graph is an adjacency list: graph[node] = list of neighbors.
    public IList<int> Bfs(IList<IList<int>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        bool[] visited = new bool[graph.Count];
        List<int> order = new List<int>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(start);
        visited[start] = true;
    }
}
using System.Collections.Generic;

public class Solution {
    // graph is an adjacency list: graph[node] = list of neighbors.
    public IList<int> Bfs(IList<IList<int>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        bool[] visited = new bool[graph.Count];
        List<int> order = new List<int>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(start);
        visited[start] = true;

            // Take the front of the queue (FIFO) and record it.
    }
}
using System.Collections.Generic;

public class Solution {
    // graph is an adjacency list: graph[node] = list of neighbors.
    public IList<int> Bfs(IList<IList<int>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        bool[] visited = new bool[graph.Count];
        List<int> order = new List<int>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(start);
        visited[start] = true;

        while (queue.Count > 0) {
            // Take the front of the queue (FIFO) and record it.
            int node = queue.Dequeue();
            order.Add(node);
        }
    }
}
using System.Collections.Generic;

public class Solution {
    // graph is an adjacency list: graph[node] = list of neighbors.
    public IList<int> Bfs(IList<IList<int>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        bool[] visited = new bool[graph.Count];
        List<int> order = new List<int>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(start);
        visited[start] = true;

        while (queue.Count > 0) {
            // Take the front of the queue (FIFO) and record it.
            int node = queue.Dequeue();
            order.Add(node);

            // Enqueue every unvisited neighbor, marking it now to keep it unique.
        }
    }
}
using System.Collections.Generic;

public class Solution {
    // graph is an adjacency list: graph[node] = list of neighbors.
    public IList<int> Bfs(IList<IList<int>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        bool[] visited = new bool[graph.Count];
        List<int> order = new List<int>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(start);
        visited[start] = true;

        while (queue.Count > 0) {
            // Take the front of the queue (FIFO) and record it.
            int node = queue.Dequeue();
            order.Add(node);

            // Enqueue every unvisited neighbor, marking it now to keep it unique.
            foreach (int next in graph[node]) {
                if (!visited[next]) {
                    visited[next] = true;
                    queue.Enqueue(next);
                }
            }
        }
    }
}
using System.Collections.Generic;

public class Solution {
    // graph is an adjacency list: graph[node] = list of neighbors.
    public IList<int> Bfs(IList<IList<int>> graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        bool[] visited = new bool[graph.Count];
        List<int> order = new List<int>();

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(start);
        visited[start] = true;

        while (queue.Count > 0) {
            // Take the front of the queue (FIFO) and record it.
            int node = queue.Dequeue();
            order.Add(node);

            // Enqueue every unvisited neighbor, marking it now to keep it unique.
            foreach (int next in graph[node]) {
                if (!visited[next]) {
                    visited[next] = true;
                    queue.Enqueue(next);
                }
            }
        }

        return order; // O(V + E)
    }
}
#include <stdbool.h>
#include <stdlib.h>

// graph is an adjacency list: graph[node] holds colSizes[node] neighbors.
int* bfs(int** graph, int graphSize, int* colSizes, int start, int* returnSize) {

}
#include <stdbool.h>
#include <stdlib.h>

// graph is an adjacency list: graph[node] holds colSizes[node] neighbors.
int* bfs(int** graph, int graphSize, int* colSizes, int start, int* returnSize) {
    // A FIFO queue is what makes the traversal go level by level.
}
#include <stdbool.h>
#include <stdlib.h>

// graph is an adjacency list: graph[node] holds colSizes[node] neighbors.
int* bfs(int** graph, int graphSize, int* colSizes, int start, int* returnSize) {
    // A FIFO queue is what makes the traversal go level by level.
    bool* visited = calloc(graphSize, sizeof(bool));
    int* order = malloc(graphSize * sizeof(int));
    int count = 0;
}
#include <stdbool.h>
#include <stdlib.h>

// graph is an adjacency list: graph[node] holds colSizes[node] neighbors.
int* bfs(int** graph, int graphSize, int* colSizes, int start, int* returnSize) {
    // A FIFO queue is what makes the traversal go level by level.
    bool* visited = calloc(graphSize, sizeof(bool));
    int* order = malloc(graphSize * sizeof(int));
    int count = 0;

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    // Marking on entry also caps the queue at graphSize, so a plain array does.
}
#include <stdbool.h>
#include <stdlib.h>

// graph is an adjacency list: graph[node] holds colSizes[node] neighbors.
int* bfs(int** graph, int graphSize, int* colSizes, int start, int* returnSize) {
    // A FIFO queue is what makes the traversal go level by level.
    bool* visited = calloc(graphSize, sizeof(bool));
    int* order = malloc(graphSize * sizeof(int));
    int count = 0;

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    // Marking on entry also caps the queue at graphSize, so a plain array does.
    int* queue = malloc(graphSize * sizeof(int));
    int head = 0, tail = 0;
    queue[tail++] = start;
    visited[start] = true;
}
#include <stdbool.h>
#include <stdlib.h>

// graph is an adjacency list: graph[node] holds colSizes[node] neighbors.
int* bfs(int** graph, int graphSize, int* colSizes, int start, int* returnSize) {
    // A FIFO queue is what makes the traversal go level by level.
    bool* visited = calloc(graphSize, sizeof(bool));
    int* order = malloc(graphSize * sizeof(int));
    int count = 0;

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    // Marking on entry also caps the queue at graphSize, so a plain array does.
    int* queue = malloc(graphSize * sizeof(int));
    int head = 0, tail = 0;
    queue[tail++] = start;
    visited[start] = true;

        // Take the front of the queue (FIFO) and record it.
}
#include <stdbool.h>
#include <stdlib.h>

// graph is an adjacency list: graph[node] holds colSizes[node] neighbors.
int* bfs(int** graph, int graphSize, int* colSizes, int start, int* returnSize) {
    // A FIFO queue is what makes the traversal go level by level.
    bool* visited = calloc(graphSize, sizeof(bool));
    int* order = malloc(graphSize * sizeof(int));
    int count = 0;

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    // Marking on entry also caps the queue at graphSize, so a plain array does.
    int* queue = malloc(graphSize * sizeof(int));
    int head = 0, tail = 0;
    queue[tail++] = start;
    visited[start] = true;

    while (head < tail) {
        // Take the front of the queue (FIFO) and record it.
        int node = queue[head++];
        order[count++] = node;
    }
}
#include <stdbool.h>
#include <stdlib.h>

// graph is an adjacency list: graph[node] holds colSizes[node] neighbors.
int* bfs(int** graph, int graphSize, int* colSizes, int start, int* returnSize) {
    // A FIFO queue is what makes the traversal go level by level.
    bool* visited = calloc(graphSize, sizeof(bool));
    int* order = malloc(graphSize * sizeof(int));
    int count = 0;

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    // Marking on entry also caps the queue at graphSize, so a plain array does.
    int* queue = malloc(graphSize * sizeof(int));
    int head = 0, tail = 0;
    queue[tail++] = start;
    visited[start] = true;

    while (head < tail) {
        // Take the front of the queue (FIFO) and record it.
        int node = queue[head++];
        order[count++] = node;

        // Enqueue every unvisited neighbor, marking it now to keep it unique.
    }
}
#include <stdbool.h>
#include <stdlib.h>

// graph is an adjacency list: graph[node] holds colSizes[node] neighbors.
int* bfs(int** graph, int graphSize, int* colSizes, int start, int* returnSize) {
    // A FIFO queue is what makes the traversal go level by level.
    bool* visited = calloc(graphSize, sizeof(bool));
    int* order = malloc(graphSize * sizeof(int));
    int count = 0;

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    // Marking on entry also caps the queue at graphSize, so a plain array does.
    int* queue = malloc(graphSize * sizeof(int));
    int head = 0, tail = 0;
    queue[tail++] = start;
    visited[start] = true;

    while (head < tail) {
        // Take the front of the queue (FIFO) and record it.
        int node = queue[head++];
        order[count++] = node;

        // Enqueue every unvisited neighbor, marking it now to keep it unique.
        for (int i = 0; i < colSizes[node]; i++) {
            int next = graph[node][i];
            if (!visited[next]) {
                visited[next] = true;
                queue[tail++] = next;
            }
        }
    }
}
#include <stdbool.h>
#include <stdlib.h>

// graph is an adjacency list: graph[node] holds colSizes[node] neighbors.
int* bfs(int** graph, int graphSize, int* colSizes, int start, int* returnSize) {
    // A FIFO queue is what makes the traversal go level by level.
    bool* visited = calloc(graphSize, sizeof(bool));
    int* order = malloc(graphSize * sizeof(int));
    int count = 0;

    // Seed the queue with the start node. Mark nodes visited when they ENTER
    // the queue, not when they leave — that avoids enqueuing them twice.
    // Marking on entry also caps the queue at graphSize, so a plain array does.
    int* queue = malloc(graphSize * sizeof(int));
    int head = 0, tail = 0;
    queue[tail++] = start;
    visited[start] = true;

    while (head < tail) {
        // Take the front of the queue (FIFO) and record it.
        int node = queue[head++];
        order[count++] = node;

        // Enqueue every unvisited neighbor, marking it now to keep it unique.
        for (int i = 0; i < colSizes[node]; i++) {
            int next = graph[node][i];
            if (!visited[next]) {
                visited[next] = true;
                queue[tail++] = next;
            }
        }
    }

    free(visited);
    free(queue);
    *returnSize = count;
    return order; // caller owns the buffer. O(V + E)
}
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
    // graph is an adjacency list: graph[node] = list of neighbors.
    vector<int> bfs(vector<vector<int>>& graph, int start) {

    }
};
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
    // graph is an adjacency list: graph[node] = list of neighbors.
    vector<int> bfs(vector<vector<int>>& graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
    }
};
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
    // graph is an adjacency list: graph[node] = list of neighbors.
    vector<int> bfs(vector<vector<int>>& graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        vector<bool> visited(graph.size(), false);
        vector<int> order;
    }
};
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
    // graph is an adjacency list: graph[node] = list of neighbors.
    vector<int> bfs(vector<vector<int>>& graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        vector<bool> visited(graph.size(), false);
        vector<int> order;

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
    }
};
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
    // graph is an adjacency list: graph[node] = list of neighbors.
    vector<int> bfs(vector<vector<int>>& graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        vector<bool> visited(graph.size(), false);
        vector<int> order;

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        queue<int> q;
        q.push(start);
        visited[start] = true;
    }
};
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
    // graph is an adjacency list: graph[node] = list of neighbors.
    vector<int> bfs(vector<vector<int>>& graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        vector<bool> visited(graph.size(), false);
        vector<int> order;

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        queue<int> q;
        q.push(start);
        visited[start] = true;

            // Take the front of the queue (FIFO) and record it.
    }
};
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
    // graph is an adjacency list: graph[node] = list of neighbors.
    vector<int> bfs(vector<vector<int>>& graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        vector<bool> visited(graph.size(), false);
        vector<int> order;

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        queue<int> q;
        q.push(start);
        visited[start] = true;

        while (!q.empty()) {
            // Take the front of the queue (FIFO) and record it.
            int node = q.front();
            q.pop();
            order.push_back(node);
        }
    }
};
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
    // graph is an adjacency list: graph[node] = list of neighbors.
    vector<int> bfs(vector<vector<int>>& graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        vector<bool> visited(graph.size(), false);
        vector<int> order;

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        queue<int> q;
        q.push(start);
        visited[start] = true;

        while (!q.empty()) {
            // Take the front of the queue (FIFO) and record it.
            int node = q.front();
            q.pop();
            order.push_back(node);

            // Enqueue every unvisited neighbor, marking it now to keep it unique.
        }
    }
};
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
    // graph is an adjacency list: graph[node] = list of neighbors.
    vector<int> bfs(vector<vector<int>>& graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        vector<bool> visited(graph.size(), false);
        vector<int> order;

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        queue<int> q;
        q.push(start);
        visited[start] = true;

        while (!q.empty()) {
            // Take the front of the queue (FIFO) and record it.
            int node = q.front();
            q.pop();
            order.push_back(node);

            // Enqueue every unvisited neighbor, marking it now to keep it unique.
            for (int next : graph[node]) {
                if (!visited[next]) {
                    visited[next] = true;
                    q.push(next);
                }
            }
        }
    }
};
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
    // graph is an adjacency list: graph[node] = list of neighbors.
    vector<int> bfs(vector<vector<int>>& graph, int start) {
        // A FIFO queue is what makes the traversal go level by level.
        vector<bool> visited(graph.size(), false);
        vector<int> order;

        // Seed the queue with the start node. Mark nodes visited when they ENTER
        // the queue, not when they leave — that avoids enqueuing them twice.
        queue<int> q;
        q.push(start);
        visited[start] = true;

        while (!q.empty()) {
            // Take the front of the queue (FIFO) and record it.
            int node = q.front();
            q.pop();
            order.push_back(node);

            // Enqueue every unvisited neighbor, marking it now to keep it unique.
            for (int next : graph[node]) {
                if (!visited[next]) {
                    visited[next] = true;
                    q.push(next);
                }
            }
        }

        return order; // O(V + E)
    }
};

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.