← 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 {
    // Micropattern: a FIFO queue drives the level-by-level order.
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // Micropattern: a FIFO queue drives the level-by-level order.
    visited := make([]bool, len(graph))
    order := []int{}
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // Micropattern: a FIFO queue drives the level-by-level order.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue and mark the start as visited when enqueued.
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // Micropattern: a FIFO queue drives the level-by-level order.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue and mark the start as visited when enqueued.
    queue := []int{start}
    visited[start] = true
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // Micropattern: a FIFO queue drives the level-by-level order.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue and mark the start as visited when enqueued.
    queue := []int{start}
    visited[start] = true

        // Dequeue from the front.
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // Micropattern: a FIFO queue drives the level-by-level order.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue and mark the start as visited when enqueued.
    queue := []int{start}
    visited[start] = true

    for len(queue) > 0 {
        // Dequeue from the front.
        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 {
    // Micropattern: a FIFO queue drives the level-by-level order.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue and mark the start as visited when enqueued.
    queue := []int{start}
    visited[start] = true

    for len(queue) > 0 {
        // Dequeue from the front.
        node := queue[0]
        queue = queue[1:]
        order = append(order, node)

        // Enqueue unvisited neighbors, marking them now to avoid duplicates.
    }
}
// graph is an adjacency list: graph[node] = list of neighbors.
func bfs(graph [][]int, start int) []int {
    // Micropattern: a FIFO queue drives the level-by-level order.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue and mark the start as visited when enqueued.
    queue := []int{start}
    visited[start] = true

    for len(queue) > 0 {
        // Dequeue from the front.
        node := queue[0]
        queue = queue[1:]
        order = append(order, node)

        // Enqueue unvisited neighbors, marking them now to avoid duplicates.
        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 {
    // Micropattern: a FIFO queue drives the level-by-level order.
    visited := make([]bool, len(graph))
    order := []int{}

    // Seed the queue and mark the start as visited when enqueued.
    queue := []int{start}
    visited[start] = true

    for len(queue) > 0 {
        // Dequeue from the front.
        node := queue[0]
        queue = queue[1:]
        order = append(order, node)

        // Enqueue unvisited neighbors, marking them now to avoid duplicates.
        for _, next := range graph[node] {
            if !visited[next] {
                visited[next] = true
                queue = append(queue, 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.