28
Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 22 Depth First Search (DFS) Traversal Novel application: computing biconnected components of a graph 1

Lecture-22-CS210-2012.pptx

Embed Size (px)

Citation preview

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

Data Structures and Algorithms(CS210/ESO207/ESO211)

Lecture 22• Depth First Search (DFS) Traversal• Novel application: computing biconnected components of a graph

1

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

Let us recall what we mean by Graph traversal

2

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

Graph traversalDefinition: A vertex y is said to be reachable from x if there is a path from x to y.

Graph traversal from vertex x: Starting from a given vertex x, the aim is to visit all vertices which are reachable from x.

3

x

y

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

Nontriviality of graph traversal

• Avoiding loop: How to avoid visiting a vertex multiple times ? (keeping track of vertices already visited)

• Finite number of steps : The traversal must stop in finite number of steps.

• Completeness : We must visit all vertices reachable from the start vertex x.

4

The following points will convince you about the non-triviality of a graph traversal algorithm

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

Depth First Search (DFS) traversal ?

5

A recursive algorithm for traversing graphs

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

DFS traversal – a milestone in the area of graph algorithm

Invented by Robert Endre Tarjan in 1972• One of the pioneer in the field of

data structures and algorithms.• Got the Turing award (equivalent to

Nobel prize) for his fundamental contribution to data structures and algorithms.

• DFS traversal has proved to be a very elegant and powerful tool for graph algorithms.

6

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

DFS traversal – a milestone in the area of graph algorithm

Applications:• Connected components of a graph.• Biconnected components of a graph. (Is the connectivity of a graph robust to failure of any node ?)• Finding bridges in a graph. (Is the connectivity of a graph robust to failure of any edge)• Planarity testing of a graph (Can a given graph be embedded on a plane so that no two edges intersect ?)• Strongly connected components of a directed graph. (the extension of connectivity in case of directed graphs)

7

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

DFS traversal of G

DFS(v) { Visited(v) true; For each neighbor w of v { if (Visited(w) = false) { DFS(w) ; } }}

DFS-traversal(G){ For each vertex vϵ V { Visited(v) false; } For each vertex v ϵ V { If (Visited(v ) = false) DFS(v); }}

8

……..;

……..;You will need to add only a few extra statements here to obtain efficient

algorithms for a variety of problems.

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

Insight into DFS through an example

DFS(v) begins v visits y DFS(y) begins y visits f DFS(f) begins f visits b DFS(b) begins all neighbors of b are already visited DFS(b) ends control returns to DFS(f) f visits h DFS(h) begins …. and so on …. After visiting z, control returns torcsuhfyv v visits w DFS(w) begins …. and so on …. 9

z

y

c

db

f

gh

u

w

v

r

s

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

Insight into DFS through an example

Observation1: (Recursive nature of DFS) If DFS(v) invokes DFS(w), then DFS(w) finishes before DFS(v).

10

z

y

c

db

f

gh

u

w

v

r

s

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

Insight into DFS through an example

Question : When DFS reaches a vertex u, what is the role of vertices already visited ?

11

z

y

c

db

f

gh

u

w

v

r

s

The traversal will not proceed along the vertices which are already visited. Hence the visited vertices act as a barrier for the

traversal from u.

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

Insight into DFS through an example

Observation 2: Let X be the set of vertices visited before DFS traversal reaches vertex u for the first time. The DFS(u) pursued now is like DFS(u) executed in graph G\X.

NOTE:G\X is the graph G after removal of all vertices X along with their edges.

12

z

c

dg

u

w

r

s

X

y

f

bh

v

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

Proving that DFS(v) traverses all vertices of the connected component of v

13

By induction on the size (number of vertices) of component of v

Can you figure out the inductive assertion now?Think over it. It is given on the following slide…

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

Inductive assertion A(i): If a connected component has size = i, then DFS from any of its vertices will visit all its vertices.

PROOF:Base case: i=1. The component is {v} and the first statement of DFS(v) marks it visited. So A(1) holds.

Induction hypothesis: If a connected component has size < i, then DFS from any of its vertices will visit all its vertices.

Induction step: We have to prove that A(i) holds. Consider any connected component of size i. Let V* be the set of its vertices. |V*|= i. Let v be any vertex in the connected component.

14

Watch the following slides very slowly and very carefully.

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

15

DFS(v)

Let y be the first neighbor visited by v.A= the set of vertices such that every path from y to them passes through v.B= V*\A.

A= {v, g, w, d}B= {y, b, f, h, u, s, c, r, z}Question: What is DFS(y) like ?Answer: DFS(y) in G\{v}.

Question: What is the connected component of y in G\{v} ?

z

c

dg

u

w

r

s

y

f

bh

v

|A|< i since y A

|B|< i since v B

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

16

DFS(v)

dg

w

z

c

u r

s

y

f

bh

vLet y be the first neighbor visited by v.A= the set of vertices such that every path from y to them passes through v.B= V*\A.

A= {v, g, w, d}B= {y, b, f, h, u, s, c, r, z}Question: What is DFS(y) like ?Answer: DFS(y) in G\{v}.

Question: What is the connected component of y in G\{v} ?Answer: B.|B|< i, so by I.H., DFS(y) visits entire set B & we return to v.Question: What is DFS(v) like when DFS(y) finishes ?Answer: DFS(v) in G\B.

Question: What is the connected component of v in G\B ?

|A|< i since y A

|B|< i since v B

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

17

DFS(v)

dg

w

vLet y be the first neighbor visited by v.A= the set of vertices such that every path from y to them passes through v.B= V*\A.

A= {v, g, w, d}B= {y, b, f, h, u, s, c, r, z}Question: What is DFS(y) like ?Answer: DFS(y) in G\{v}.

Question: What is the connected component of y in G\{v} ?Answer: B.|B|< i, so by I.H., DFS(y) visits entire set B & we return to v.Question: What is DFS(v) like when DFS(y) finishes ?Answer: DFS(v) in G\B.

Question: What is the connected component of v in G\B ?Answer: A.|A|< i, so by I.H., DFS(v) pursued after finishing DFS(y) visits entire set A.

|A|< i since y A

|B|< i since v B

Hence entire component of v gets visited

z

c

u r

s

y

f

bh

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

Theorem: DFS(v) visits all vertices of the component of v.

Exercise: Use DFS traversal to compute all connected components of a given G in time O(m+n).

18

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

DFS tree

19

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

DFS(v) computes a tree rooted at v

Observation:For each vertex x in the connected component of v, there will be a neighboring vertex which visits it for the first time during DFS(v). So directing the edge from that neighbor to x gives us a tree rooted at v.

20

z

y

c

db

f

gh

u

w

r

s

v

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

z

y

c

db

f

gh

u

w

r

s

DFS(v) computes a tree rooted at v

NOTE: There will be a path in the DFS tree from v to every vertex of the component of v.

The edges of the graph which appear in the DFS tree are called tree edges. The remaining ones are called non-tree edges.

21

v

A DFS tree rooted at v

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

Note that the during DFS traversal, we may visit neighbor of a vertex in any arbitrary manner. Hence DFS traversal is not unique. There can be many DFS trees possible rooted at a vertex v. Question: Is there anything unique about a DFS tree?Answer: Yes.

Theorem: Let T be any DFS tree and (x,y) is any non-tree edge. Then either• x is ancestor of y in T or• y is ancestor of x in T

Go to the following slide to get a better understanding of this Theorem.

22

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

Exercise: Make sincere attempts to prove the theorem. Its short proof will be discussed in the next class.

23

u

xy

It can never happen

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

A novel application of DFS traversal

24

Determining if a graph G is biconnected

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

Definition: A connected graph is said to be biconnected if there does not exit any vertex whose removal disconnects the graph.

Motivation: To design robust networks (immune to any single node failure).

25

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

No.The removal of any of {v,f,u} can destroy connectivity.

v,f,u are called the articulation points of G.

26

z

y

c

db

f

gh

u

w

v

r

s

Is this graph biconnected ?

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

A formal definition of articulaton point

Definition: A vertex x is said to be articulation point if there exist two distinct vertices u and v such that every path between u and v passes through x.

Observation: A graph is biconnected if none of its vertices is an articulation point.

AIM: Design an algorithm to compute all articulation points in a given graph.

27

vu x

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

Algorithms for articulaton points in a graph

• Trivial algorithm: For each vertex v, determine if G\{v} is connected (One may use either BFS or DFS traversal here)

Time complexity of the trivial algorithm : O(mn)

• Novel algorithm: O(m+n) time

28This novel algorithm will be discussed in the next class