Docsity

Breadth-First Search Algorithm and Graph Traversal, Slides of Data Structures and Algorithms

Partial preview of the text

Download Breadth-First Search Algorithm and Graph Traversal and more Slides Data Structures and Algorithms in PDF only on Docsity!

Breadth-First Search

Problem of the Day

Present correct and efficient algorithms to convert between the following graph data structures, for an undirected graph G with n vertices and m edges. You must give the time complexity of each algorithm.

  1. Convert from an adjacency matrix to adjacency lists.

Traversing a Graph

One of the most fundamental graph problems is to traverse every edge and vertex in a graph. For efficiency , we must make sure we visit each edge at most twice. For correctness , we must do the traversal in a systematic way so that we don’t miss anything. Since a maze is just a graph, such an algorithm must be powerful enough to enable us to get out of an arbitrary maze.

Marking Vertices

The key idea is that we must mark each vertex when we first visit it, and keep track of what have not yet completely explored. Each vertex will always be in one of the following three states:

  • undiscovered – the vertex in its initial, virgin state.
  • discovered – the vertex after we have encountered it, but before we have checked out all its incident edges.
  • processed – the vertex after we have visited all its incident edges.

To Do List

We must also maintain a structure containing all the vertices we have discovered but not yet completely explored. Initially, only a single start vertex is considered to be discovered. To completely explore a vertex, we look at each edge going out of it. For each edge which goes to an undiscovered vertex, we mark it discovered and add it to the list of work to do. Note that regardless of what order we fetch the next vertex to explore, each edge is considered exactly twice, when each of its endpoints are explored.

Correctness of Graph Traversal

Every edge and vertex in the connected component is eventually visited. Suppose not, ie. there exists a vertex which was unvisited whose neighbor was visited. This neighbor will eventually be explored so we would visit it:

Data Structures for BFS

We use two Boolean arrays to maintain our knowledge about each vertex in the graph. A vertex is discovered the first time we visit it. A vertex is considered processed after we have traversed all outgoing edges from it. Once a vertex is discovered, it is placed on a FIFO queue. Thus the oldest vertices / closest to the root are expanded first.

bool processed[MAXV]; bool discovered[MAXV]; int parent[MAXV];

Initializing BFS

initialize search(graph *g) { int i; for (i=1; i<=g− >nvertices; i++) { processed[i] = discovered[i] = FALSE; parent[i] = -1; } }

enqueue(&q,y); discovered[y] = TRUE; parent[y] = v; } p = p− >next; } process vertex late(v); } }

BFS Example

6

1 2 5 3 4

1

(^23)

4 5 6

Finding Paths

The parent array set within bfs() is very useful for finding interesting paths through a graph. The vertex which discovered vertex i is defined as parent[i]. The parent relation defines a tree of discovery with the initial search node as the root of the tree.

Shortest Paths and BFS

In BFS vertices are discovered in order of increasing distance from the root, so this tree has a very important property. The unique tree path from the root to any node x ∈ V uses the smallest number of edges (or equivalently, intermediate nodes) possible on any root-to-x path in the graph.

Connected Components

The connected components of an undirected graph are the separate “pieces” of the graph such that there is no connection between the pieces. Many seemingly complicated problems reduce to finding or counting connected components. For example, testing whether a puzzle such as Rubik’s cube or the 15-puzzle can be solved from any position is really asking whether the graph of legal configurations is connected. Anything we discover during a BFS must be part of the same connected component. We then repeat the search from any undiscovered vertex (if one exists) to define the next component, until all vertices have been found:

Implementation

connected components(graph *g) { int c; int i; initialize search(g); c = 0; for (i=1; i<=g− >nvertices; i++) if (discovered[i] == FALSE) { c = c+1; printf(”Component %d:”,c); bfs(g,i); } }