41
1 Chapter 22: Elementary Graph Algorithms II

1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

Embed Size (px)

Citation preview

Page 1: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

1

Chapter 22: Elementary Graph Algorithms II

Page 2: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

2

About this lecture

• Depth First Search• DFS Tree and DFS Forest

• Properties of DFS• Parenthesis theorem (very

important)• White-path theorem (very useful)

Page 3: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

3

Depth First Search (DFS)

• An alternative algorithm to find all vertices reachable from a particular source vertex s

• Idea: Explore a branch as far as

possible before exploring another branch

• Easily done by recursion or stack

Page 4: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

4

The DFS AlgorithmDFS(u) { Mark u as discovered ;

while (u has unvisited neighbor v)DFS(v);

Mark u as finished ;}

The while-loop explores a branch as far as possible before the next branch

Page 5: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

5

Example (s = source)

v

r s

w x

t u

y v

r s

w x

t u

y

v

r s

w x

t u

y

finished

discovered

direction of edge when new node is discovered

Page 6: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

6

Example (s = source)

v

r s

w x

t u

y v

r s

w x

t u

y

v

r s

w x

t u

y

finished

discovered

direction of edge when new node is discovered

Page 7: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

7

Example (s = source)

v

r s

w x

t u

y v

r s

w x

t u

y

v

r s

w x

t u

y

finished

discovered

direction of edge when new node is discovered

Page 8: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

8

Example (s = source)

v

r s

w x

t u

y v

r s

w x

t u

y

v

r s

w x

t u

y

finished

discovered

direction of edge when new node is discovered

Page 9: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

9

Example (s = source)

v

r s

w x

t u

y v

r s

w x

t u

y

v

r s

w x

t u

y

finished

discovered

direction of edge when new node is discovered

Page 10: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

10

Example (s = source)

Done when s is discovered

The directed edges form a tree that

contains all nodes reachable from s

Called DFS tree of sv

r s

w x

t u

y

v

r s

w x

t u

y

Page 11: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

11

Generalization• Just like BFS, DFS may not visit all the

vertices of the input graph G, because :• G may be disconnected• G may be directed, and there is no

directed path from s to some vertex

• In most application of DFS (as a subroutine)

, once DFS tree of s is obtained, we will continue to apply DFS algorithm on any unvisited vertices …

Page 12: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

12

Generalization (Example)

v

r s

w x

t u

y

Suppose the input graph is directed

Page 13: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

13

Generalization (Example)

v

r s

w x

t u

y

1. After applying DFS on s

Page 14: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

14

Generalization (Example)

v

r s

w x

t u

y

2. Then, after applying DFS on t

Page 15: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

15

Generalization (Example)

v

r s

w x

t u

y

3. Then, after applying DFS on y

Page 16: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

16

Generalization (Example)

v

r s

w x

t u

y

4. Then, after applying DFS on r

Page 17: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

17

Generalization (Example)

v

r s

w x

t u

y

5. Then, after applying DFS on v

Page 18: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

18

Generalization (Example)

Result : a collection of rooted trees called DFS forest

v

r s

w x

t u

y

Page 19: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

19

Performance

• Since no vertex is discovered twice, and each edge is visited at most twice (why?)

Total time: O(|V|+|E|)

• As mentioned, apart from recursion, we can also perform DFS using a LIFO stack(Do you know how?)

Page 20: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

20

Who will be in the same tree ?

• Because we can only explore branches in an unvisited node DFS(u) may not contain all nodes

reachable by u in its DFS tree

v

r s

w x

t u

y

E.g, in the previous run, v can reach r, s, w, x but v ’s tree does not contain any of them

Can we determine who will be in the same tree ?

Page 21: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

21

Who will be in the same tree ?

• Yes, we will soon show that by white-path theorem, we can determine who will be in the same tree as v at the time when DFS is performed on v

• Before that, we will define the discovery time and finishing time for each node, and show interesting properties of them

Page 22: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

22

Discovery and Finishing Times

• When the DFS algorithm is run, let us consider a global time such that the time increases one unit :• when a node is discovered, or • when a node is finished

(i.e., finished exploring all unvisited neighbors)

• Each node u records :d(u) = the time when u is discovered, and f(u) = the time when u is finished

Page 23: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

23

Discovery and Finishing Times

In our first example (undirected graph)

v

r s

w x

t u

y

1/1612/15

13/14 2/11

4/9 5/8

3/10 6/7

Page 24: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

24

Discovery and Finishing Times

v

r s

w x

t u

y

1/613/14

15/16 2/5

7/10 8/9

3/4 11/12

In our second example (directed

graph)

Page 25: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

25

Nice PropertiesLemma: For any node u, d(u) f(u)

Theorem (Parenthesis Theorem): Let u and v be two nodes with d(u) d(v) .Then, either 1. d(u) d(v) f(v) f(u) [contain], or2. d(u) f(u) d(v) f(v) [disjoint]

Lemma: For nodes u and v, d(u), d(v), f(u), f(v) are all distinct

Page 26: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

26

Proof of Parenthesis Theorem

• Consider the time when v is discovered• Since u is discovered before v, there are

two cases concerning the status of u :

• Case 1: (u is not finished)This implies v is a descendant of u f(v) f(u) (why?)

• Case 2: (u is finished) f(u) d(v)

Page 27: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

27

CorollaryCorollary:

v is a (proper) descendant of u if and only if

d(u) d(v) f(v) f(u)

Proof: v is a (proper) descendant of u d(u) d(v) and f(v) f(u)

d(u) d(v) f(v) f(u)

Page 28: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

28

White-Path TheoremTheorem: In a DF forest of a graph G = (V, E),

vertex v is a descendant of vertex u if and only if at the time d(u) that the search discovers u, there is a path from u to v consisting entirely of white nodes only

E.g.,If we perform DFS(w)

now, will the descendant of w

always be the same set of nodes?v

r s

w x

t u

y

Page 29: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

29

Proof (Part 1)• => Suppose that v is a descendant of u

Let P = (u, w1, w2, …, wk, v) be the directed path from u to v in DFS tree of u

Then, apart from u, each node on P must be discovered after u They are all unvisited by the time we perform DFS on u Thus, at this time, there exists a path from u to v with unvisited nodes only

Page 30: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

30

Proof (Part 2)• So, every descendant of u is reachable

from u with unvisited nodes only

• To complete the proof, it remains to show the converse :

Any node reachable from u with unvisited nodes only becomes u ’s

descendant

is also true(We shall prove this by contradiction)

Page 31: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

31

Proof (Part 2)• Suppose on contrary the converse is

false• Then, there exists some v, reachable

from u with unvisited nodes only, does not become u ’s descendant• If more than one choice of v, let v be

one such vertex closest to u

d(u) f(u) d(v) f(v) … EQ.1

Page 32: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

32

Proof (Part 2)• Let P = (u, w1, w2, …, wk, v) be any path

from u to v using unvisited nodes only

• By our choice of v (closest one), all w1, w2, …, wk become u ’s descendants

• This implies: d(u) d(wk) f(wk) f(u)

Handle special case: when u = wk

• Combining with EQ.1, we haved(wk) f(wk) d(v) f(v)

Page 33: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

33

Proof (Part 2)• However, since there is an edge (no

matter undirected or directed) from wk to v,

if d(wk) d(v) , then we must have

d(v) f(wk) … (why??)

• Consequently, it contradicts with : d(wk) f(wk) d(v) f(v)

Proof completes

Page 34: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

34

Classification of Tree Edges

• After a DFS process, we can classify the edges of a graph into four types :

1. Tree : Edges in the DFS forest 2. Back : From descendant to ancestor

when explored (include self loop)

3. Forward : From ancestor to descendant when explored (exclude tree edge)

4. Cross : Others (no ancestor-descendant relation)

Page 35: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

35

Example

v

r s

w x

t u

y

Suppose the input graph is directed

Page 36: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

36

Example

v

r s

w x

t u

y

Suppose this is the DFS forest obtained

Can you classify the type of each edge ?

B F CCC

Page 37: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

37

Example

v

r s

w x

t u

y

Suppose the DFS forest is different now …

Can you classify the type of each edge ?

B

C

CCC

Page 38: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

38

Example

v

r s

w x

t u

y

Suppose the input graph is undirected

Page 39: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

39

Example

v

r s

w x

t u

y

Suppose this is the DFS forest obtained

Can you classify the type of each edge ?

B B

Page 40: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

40

Edges in Undirected GraphTheorem:

After DFS of an undirected graph, every edge is either a tree edge or a back edge

Proof: Let (u,v) be an edge. Suppose u is discovered first. Then, v will become u’s descendent (white-path) so that f(v) f(u)

• If u discovers v (u,v) is tree edge• Else, (u,v) is explored after v discovered

Then, (u,v) must be explored from v because f(v) f(u) (u,v) is back edge

Page 41: 1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very

41

Homework

• Exercise: 22.3-2, 22.3-3 (Due Dec. 18)

• Practice at home:. 22.3-8, 22.3-9, 22.3-11