Breadth First Search. Another graph search algorithm is Breadth First Search (bfs). In addition to finding connected components, the BFS approach enables us to calculate the distance from the start to every reachable vertex in its connected component.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Breadth First Search
Another graph search algorithm is Breadth First Search (bfs).
In addition to finding connected components, the BFS approach enables us to calculate the distance from the start to every reachable vertex in its connected component.
BFS uses a queue to hold vertices that have been visited but have not yet been used as a current vertex.
When searching from a vertex u, we look at every vertex adjacent to u,
one after another; for each of these vertices, if it is unvisited, we mark it
as visited and place it on the queue of pending vertices.
After inspecting all the vertices adjacent to u, we remove the next vertex
from the queue and explore from there.
We start by marking the start vertex as visited, in this algorithm by setting
its distance to 0; then we append it to the queue.
Then: while the queue is not empty:
Serve a vertex from the queue to be the current vertex;
for each vertex w adjacent from current:
if w has not been visited (distance is NIL):
mark w as visited;
make current the predecessor of w;
append w to the queue of pending vertices;
set the distance of w to be 1 + the distance of current;
We illustrate the BFS algorithm execution with the following graph
We assume that we process the adjacent vertices of our current vertex in
alphabetical order. Our initial call will be bfs(a,…).
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
a
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
a
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
e
a
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
f e
a
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
f
e
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
d c b f
e
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
d c b
f
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
g d c b
f
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
g d c
b
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
g d
c
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
g
d
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
h g
d
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
h
g
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
h
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
j i
h
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
j
i
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
j
back
front
black vertices have distance -1
arrow points to the current vertex
teal vertices have been
completely explored and distance
is valid.
Queue of pending vertices
current
j
back
front
If the theorem is true for all connected graphs, then it is clearly true for arbitrary graphs, so we assume G = (V,E) is connected.
Choose a vertex u of G and define V0 = { v V | dist(u,v) is even } and V1 = { v V | dist(u,v) is odd}. Clearly V = V0 V1 and V0 V1 = .
Thus V0 ,V1 is a partition of V.
Claim: no edge of G connects vertices in the same partition set.
Suppose not and let v,w be adjacent vertices in G whose distances from u are either both odd or both even. Let Qv be a shortest path from u to v and Qw a shortest path to w from u.
Let x be the last vertex of Qw encountered when traversing Qv.
The length of the segments of the two paths from u to x must be the same as otherwise we could replace the longer by the shorter and get a shorter path to its target.
Pv
Pw
Let Pv and Pw be the segments of Qv and Qw from x to their targets.
Then the lengths of Pv and Pw are either both odd or both even, so the sum of their lengths is even. But this means that Pv Pw {v,w} is an odd cycle.
Run BFS with the following modifications:
Assign color WHITE to all vertices of G
Assign color RED to the start vertex s and put s in the queue
While the queue is not empty:u dequeued element of the queue
for each vertex w adjacent to u:if color(w) == color(u)return FALSE
if color(w) == WHITE if color(u) = RED set color of w to BLUE else set color of w to RED
Enqueue w
return TRUE