33
Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 21 BFS traversal (proof of correctness) BFS tree An important application of BFS traversal 1

Lecture-21-CS210-2012.pptx

Embed Size (px)

Citation preview

Page 1: Lecture-21-CS210-2012.pptx

Data Structures and Algorithms(CS210/ESO207/ESO211)

Lecture 21• BFS traversal (proof of correctness)• BFS tree• An important application of BFS traversal

1

Page 2: Lecture-21-CS210-2012.pptx

Breadth First Search traversal

2

Page 3: Lecture-21-CS210-2012.pptx

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;

Page 4: Lecture-21-CS210-2012.pptx

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

Page 5: Lecture-21-CS210-2012.pptx

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

Page 6: Lecture-21-CS210-2012.pptx

1𝑽 𝟏

0𝑽 𝟎

The key idea underlying proof of correctness

Partition the vertices according to their distance from x.

6

x

2𝑽 𝟐

i-1𝑽 𝒊−𝟏

i𝑽 𝒊

i+1𝑽 𝒊+𝟏

w

w can not have any neighbor from level i-2 or higher.

Page 7: Lecture-21-CS210-2012.pptx

Correctness of BFS(x) traversalPart 1

7

All vertices reachable from x get visited

Page 8: Lecture-21-CS210-2012.pptx

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

Page 9: Lecture-21-CS210-2012.pptx

Induction step: To prove that w ϵ is visited during BFS(x)

Let v ϵ 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

Page 10: Lecture-21-CS210-2012.pptx

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

Page 11: Lecture-21-CS210-2012.pptx

Correctness of BFS(x) traversalPart 2

11

Vertices are visited in the non-decreasing order of distance from x

Page 12: Lecture-21-CS210-2012.pptx

Proof of Part 2

Theorem: Vertices are visited in non-decreasing order of distance from x duringBFS(G,x).Proof: (By induction on distance from x)Inductive Assertion A(i) : Every vertex w ϵ gets visited only after every v ϵ has been visited.

Base case: i = 0. = {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

Page 13: Lecture-21-CS210-2012.pptx

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𝑽 𝟏

0𝑽 𝟎x

i-1𝑽 𝒊−𝟏

i𝑽 𝒊w

i-2𝑽 𝒊−𝟐

v

u

y

FIFO property of queue

Page 14: Lecture-21-CS210-2012.pptx

Correctness of BFS(x) traversalPart 3

14

Distance(v) stores distance of v from x

Page 15: Lecture-21-CS210-2012.pptx

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

Page 16: Lecture-21-CS210-2012.pptx

Induction step: To prove that for w ϵ , Distance(w) = i at the end of BFS(G,x)

Among all neighbors of w, let v ϵ 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 = IThis 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

Page 17: Lecture-21-CS210-2012.pptx

BFS tree

17

Page 18: Lecture-21-CS210-2012.pptx

1𝑽 𝟏

0𝑽 𝟎

BFS traversal gives a tree

Perform BFS traversal from x.

18

x

2𝑽 𝟐

i-1𝑽 𝒊−𝟏

i𝑽 𝒊

i+1𝑽 𝒊+𝟏

w

v

Page 19: Lecture-21-CS210-2012.pptx

A nontrivial application of BFS traversal

19

Determining if a graph is bipartite

Page 20: Lecture-21-CS210-2012.pptx

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

Page 21: Lecture-21-CS210-2012.pptx

Nontriviality in determining whether a graph is bipartite

21

A B

bipartite bipartite

A

BA

A

B

B

Both are same graph but drawn in different ways. Can you see it ?

Is this graph bipartite ?

Page 22: Lecture-21-CS210-2012.pptx

Bipartite graph

Question: Is a path bipartite ?

Answer: Yes

22

A B A B A B A

Page 23: Lecture-21-CS210-2012.pptx

Bipartite graph

Question: Is a cycle bipartite ?

23

A

B

B

A

B

B

A

non-bipartite bipartite

Page 24: Lecture-21-CS210-2012.pptx

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

AB

B

B

B

A

Page 25: Lecture-21-CS210-2012.pptx

Bipartite graph

Question: Is a tree bipartite ?

Answer: YesEven level vertices: AOdd level vertices: B

25

0

1

2

3

4

Page 26: Lecture-21-CS210-2012.pptx

Subgraph

A subgraph of a graph G=(V,E) is a graph G’=(V’,E’) such that • V’ ⊆ V • E’ ⊆ EQuestion: If G has a subgraph which is an odd cycle, is G bipartite ?Answer: No.

26

∩ (V’ ⨯ V’)

Page 27: Lecture-21-CS210-2012.pptx

An algorithm for determining if a given graph is bipartite

27

Assume the graph is a single connected component

Page 28: Lecture-21-CS210-2012.pptx

1𝑽 𝟏

0𝑽 𝟎

Compute a BFS tree at any vertex x.

28

x

2𝑽 𝟐

i-1𝑽 𝒊−𝟏

i𝑽 𝒊

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

Page 29: Lecture-21-CS210-2012.pptx

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

Page 30: Lecture-21-CS210-2012.pptx

1𝑽 𝟏

0𝑽 𝟎

What if there is an edge with both end points at same level ?

30

x

2𝑽 𝟐

i-1𝑽 𝒊−𝟏

i𝑽 𝒊

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 ?

Page 31: Lecture-21-CS210-2012.pptx

1𝑽 𝟏

0𝑽 𝟎

31

x

2𝑽 𝟐

i-1𝑽 𝒊−𝟏

i𝑽 𝒊

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

Page 32: Lecture-21-CS210-2012.pptx

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

Page 33: Lecture-21-CS210-2012.pptx

Theorem: There is an O(n+m) time algorithm to determine if a given graph is bipartite.

33