Breadth-First Search

Breadth-first search animation

function BreadthFirstSearch(G,source){
  for each vertex u in (G.V - {source}) {
      u.color = white
      u.d = infinity
      u.phi = NaN
  }
  source.color = grey
  source.d = 0
  source.phi = NaN
  Q = new empty queue
  Enqueue(Q, source)
  //Step = 0
  while Q is not empty {
      u= Dequeue(Q)
      for each v in G.AdjacencyList[u] {
          if (v.color is white) {
              v.color = grey
              v.d = u.d + 1
              v.phi = u
              Enqueue(Q, v)
              //Step++;
          }
      }
      u.color = black
      //Step++
  }
}

Task 1 Step by step breadth-first search

Visualize the process of breadth-first search -- color and distance change of each vertex. The check points are marked as Step++ in the pseudocode.

The input graph G is shown below, and the source vertex is

Click on vertex to edit. Pay attention on the order in each adjacency list!


Task 2 Fill in the distance and predecessor table

Show the d and phi values that result from running breadth-first search on this undirected graph.

The input graph G is shown below. The source vertex is