What is Breadth-first search?

A way of searching a graph or tree that visits vertices in order of distance from the start: first those one edge away, then those two edges away, and so on. It is implemented with a queue, and in a graph without edge weights it finds the smallest number of edges from the start to every vertex. It is abbreviated BFS.

Japanese name: 幅優先探索

Breadth-first search appears in 4 practice questions across 1 topic, including Data Structures and Algorithms.

Field: Data Structures and Algorithms

Try it: questions about Breadth-first search

Elements A, B and C are added to an empty queue in this order. Then one element is removed, D is added and one is removed, and E is added and one is removed. In what order are the elements removed?

  1. AA, B, C
  2. BC, D, E
  3. CC, B, A
Show answer and explanation

Answer: A. A, B, C

A queue is a first-in, first-out (FIFO) structure: items are removed in the order they were added. After A, B and C are added, the first removal takes A. Even though D and E are added at the back afterward, B and C at the front come out first, so the removal order is A, B, C, and D and E remain. C, D, E is the order you would get from a stack (LIFO), which removes the most recently added item each time. Queues are used for print jobs, for message queues that pass data between tasks, and for breadth-first search.

Which is an appropriate way to store in memory a sparse graph, one with few edges compared with its number of vertices, such as a road network with about 1 million vertices and 3 million edges?

  1. AAn adjacency matrix: it can check whether two vertices are connected in O(1) and also uses the least memory
  2. BAdjacency lists: memory is proportional to the number of vertices plus the number of edges, and they suit following the edges out of a vertex one by one
  3. CAn adjacency matrix: its memory is proportional only to the number of edges
Show answer and explanation

Answer: B. Adjacency lists: memory is proportional to the number of vertices plus the number of edges, and they suit following the edges out of a vertex one by one

An adjacency matrix uses a V × V table for V vertices, so 1 million vertices need 10¹² entries, about 125 GB even at 1 bit per entry. Adjacency lists store only the neighbors of each vertex, so memory is O(V + E), only a few million entries in this example. An adjacency matrix has the advantage of checking for an edge in O(1) and suits dense graphs or graphs with few vertices. For processing that follows the edges out of each vertex in turn, such as breadth-first search and Dijkstra's algorithm, adjacency lists are more efficient.

Breadth-first search in the explanations

Questions that mention Breadth-first search (4)

  1. Elements A, B and C are added to an empty queue in this order. Then one element is removed, D is added and one is removed, and E is added and one is removed. In what order are the elements removed?
  2. Which is an appropriate way to store in memory a sparse graph, one with few edges compared with its number of vertices, such as a road network with about 1 million vertices and 3 million edges?
  3. In a graph whose edges have no weights, you want to find the smallest number of edges from a starting vertex to every other vertex. Which pair of search method and data structure is suitable?
  4. In an undirected graph, the neighbors of each vertex are A: B, C / B: A, D, E / C: A, F / D: B / E: B, F / F: C, E. A recursive depth-first search starts from vertex A. The neighbors are examined in the order listed, and as soon as an unvisited vertex is found, the search moves to it. In what order are the vertices visited?

Books on Data Structures and Algorithms Ad

As an Amazon Associate I earn from qualifying purchases.

Semiconductor, Electronics and Computing Calculators

Related terms

Queue Stack Depth-first search Dijkstra's algorithm Recursion Binary heap Binary search Binary search tree Dynamic programming Greedy algorithm

Back to the glossary