Data Structures and Algorithms: (CS210/ESO207/ESO211)
Data Structures and Algorithms: (CS210/ESO207/ESO211)
(CS210/ESO207/ESO211)
Lecture 21
BFS traversal (proof of correctness)
BFS tree
An important application of BFS traversal
1
Breadth First Search traversal
2
BFS traversal of G from a vertex x
BFS(G, x) //Initially for each v, Distance(v)
{ CreateEmptyQueue(Q);
Distance(x) 0;
Enqueue(x,Q);
While(Not IsEmptyQueue(Q))
{ v Dequeue(Q);
For each neighbor w of v
{ if (Distance(w) = )
{ Distance(w) Distance(v) +1 ;
Enqueue(w, Q);
}
}
}
3
, and Visited(v) false.
Visited(x) true;
Visited(w) true;
Observations about BFS(x)
Observations:
Any vertex v enters the queue at most once.
Before entering the queue, Distance(v) is updated.
When a vertex v is dequeued, it processes all its neighbors: Each of its unvisited
neighbors is marked visited, its distance is updated, and is enqueued.
A vertex v in the queue is surely removed from the queue during the algorithm.
4
Correctness of BFS traversal
Question: What do we mean by correctness of BFS traversal from vertex x ?
Answer:
All vertices reachable from x get visited.
Vertices get visited in the non-decreasing order of their distances from x.
At the end of the algorithm, Distance(v) is the distance of vertex v from x.
5
1
The key idea underlying proof of correctness
Partition the vertices according to their distance from x.
6
x
2
i-1
i+1
+
w
w can not have any neighbor
from level i-2 or higher.
Correctness of BFS(x) traversal
Part 1
7
All vertices reachable from x get visited
Proof of Part 1
Theorem: Each vertex v reachable from x gets visited during BFS(G,x).
Proof:
(By induction on distance from x)
Inductive Assertion A(i) :
Every vertex v at distance i from x get visited.
Base case: i = 0.
x is the only vertex at distance 0 from x.
Right in the beginning of the algorithm Visited(x) true;
Hence the assertion A(0) is true.
Induction Hypothesis: A(j) is true holds for all j < i.
Induction step: To prove that A(i) is true.
Let w
.
8
Induction step:
To prove that w
be any neighbor of w.
By induction hypothesis, v gets visited during
BFS(x).
So v gets Enqueued.
Hence v gets dequeued.
Focus on the moment when v is dequeued,
v scans all its neighbors and marks all its
unvisited neighbors as visited.
Hence w gets visited too if not visited earlier.
This proves the induction step.
Hence by the principle of mathematical
induction, A(i) holds for each i.
This completes the proof of part 1.
9
w
x
0
1
i
i-1
2
v
We shall use this observation in proving Part 2.
10
It follows from the proof that a vertex at level i is marked
visited during the algorithm by some vertex at level i -1
Correctness of BFS(x) traversal
Part 2
11
Vertices are visited in the non-decreasing order
of distance from x
Proof of Part 2
Theorem: Vertices are visited in non-decreasing order of distance from x during
BFS(G,x).
Proof:
(By induction on distance from x)
Inductive Assertion A(i) :
Every vertex w
= {x}.
x is visited first during BFS(G,x).
Hence the assertion A(0) is true.
Induction Hypothesis: A(j) is true holds for all j < i.
Induction step: To prove that A(i) is true.
Let w
. Let v
12
Induction step:
To prove that w
is visited after v
during BFS(x)
Using Observation mentioned 2 slides back,
v is marked visited by some vertex u at level i-2.
w is marked visited by some vertex y at level i-1.
Using induction hypothesis y gets visited after u.
So y enters the queue after u.
So y leaves the queue after u.
When u leaves the queue and processed, v is marked
visited.
At this moment, y must either be in the queue or has
not even entered the queue. Since w is marked
visited only when y is dequeued and processed, it
follows that when v is marked visited, w was
unvisited.
This proves the induction step.
Hence by the principle of mathematical induction
A(i) holds for each i.
13
1
x
i-1
w
i-2
v
u
y
FIFO property of
queue
Correctness of BFS(x) traversal
Part 3
14
Distance(v) stores distance of v from x
Proof of Part 3
Theorem: For each w in the connected component of x, Distance(w) stores true
distance of w from x at the end BFS(G,x).
Proof:
(By induction on distance from x)
Inductive Assertion A(i) :
For every vertex w
, Distance(w) = i.
Base case: i = 0.
x is the only vertex at distance 0 from x.
Right in the beginning of the algorithm Distance(x) 0;
Hence the assertion A(0) is true.
Induction Hypothesis: A(j) is true holds for all j < i.
Induction step: To prove that A(i) is true.
Let w
.
15
Induction step:
To prove that for w
be the
neighbor of w which is visited first.
By induction hypothesis, Distance(v) = i-1.
Focus on the moment when v is dequeued,
v scans all its neighbors, updates Distance()
for all its unvisited neighbors.
w was unvisited at this stage.
Hence Distance(w) Distance(v) +1
= i-1 + 1
= I
This proves the induction step.
Hence by the principle of mathematical
induction, A(i) holds for each i.
This completes the proof of part 3.
16
w
x
0
1
i
i-1
2
v
BFS tree
17
1
BFS traversal gives a tree
Perform BFS traversal from x.
18
x
2
i-1
i+1
+
w
v
A nontrivial application of BFS traversal
19
Determining if a graph is bipartite
Bipartite graph
Definition: A graph G=(V,E) is said to be bipartite if its vertices can be
partitioned into two sets A and B such that every edge in E has one endpoint
in A and another in B.
20
A B
Is this graph
bipartite ?
YES
Nontriviality in determining whether a graph is
bipartite
21
A B
bipartite
bipartite
A
B
A
A
B
B
Both are same graph
but drawn in different
ways. Can you see it ?
Is this graph
bipartite ?
Bipartite graph
Question: Is a path bipartite ?
Answer: Yes
22
A B A B A B A
Bipartite graph
Question: Is a cycle bipartite ?
23
A
B
B
A
B
B
A
non-bipartite bipartite
Bipartite graph
Question: Is a cycle bipartite ?
24
A
B
B A
A B
B
A
non-bipartite
bipartite
Odd length cycle
Even length cycle
B
B
B
B
A
A
A
A
A
A
A
A B
B
B
B
A
Bipartite graph
Question: Is a tree bipartite ?
Answer: Yes
Even level vertices: A
Odd level vertices: B
25
0
1
2
3
4
Subgraph
A subgraph of a graph G=(V,E) is a graph G=(V,E) such that
V V
E E
Question: If G has a subgraph which is an odd cycle, is G bipartite ?
Answer: No.
26
(V V)
An algorithm for determining if a given graph
is bipartite
27
Assume the graph is a single
connected component
1
Compute a BFS tree at any vertex x.
28
x
2
i-1
i+1
+
w
A
B
A
B
A
B
The BFS tree is
bipartite. Now place the
non tree edges
If every nontree edge
goes between two
consecutive levels, what
can we say ?
The graph is bipartite
Observation:
If every non-tree edge goes between two consecutive levels of
BFS tree, then the graph is bipartite.
Question:
What if there is an edge with both end points at same level ?
29
1
What if there is an edge with both end points at same
level ?
30
x
2
i-1
i+1
+
w
A
B
A
B
A
B
u
Keep following parent pointer from u
and w simultaneously until we reach a
common ancestor. What do we get ?
1
31
x
2
i-1
i+1
+
w
A
B
A
B
A
B
u
Keep following parent pointer from u
and w simultaneously until we reach a
common ancestor. What do we get ?
An odd cycle
containing u and w
Observation:
If there is even a single non-tree edge whose both endpoints are
at the same level, then the graph has an odd length cycle. Hence
the graph is not bipartite.
32
Theorem: There is an O(n+m) time algorithm to determine if a
given graph is bipartite.
33