Top Banner
1 CS2351 Data Structures Lecture 10: Graph and Tree Traversals I
37

CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

May 26, 2018

Download

Documents

doque
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

1

CS2351Data Structures

Lecture 10:Graph and Tree Traversals I

Page 2: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

2

About this lecture•We introduce two popular algorithms to

traverse a graph1. Breadth First Search (BFS)2. Depth First Search (DFS)

•DFS Tree and DFS Forest•Parenthesis theorem

Page 3: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

3

Breadth First Search

Page 4: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

4

Lost in a Desert•After an unfortunate accident, we

survived, but are lost in a desert•To keep surviving, we need to find water•How to find the closest water source?

where is water?

Page 5: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

5

Breadth First Search (BFS)•A simple algorithm to find all vertices

reachable from a particular vertex s• s is called source vertex

•Idea: Explore vertices in rounds•At Round k, visit all vertices whose

shortest distance (#edges) from s is k-1•Also, discover all vertices whose

shortest distance from s is k

Page 6: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

6

The BFS Algorithm1. Mark s as discovered in Round 02. For Round k = 1, 2, 3, …,

For (each u discovered in Round k-1){ Mark u as visited ;

Visit each neighbor v of u ;If (v not visited and not discovered)

Mark v as discovered in Round k ;}Stop if no vertices werediscovered in Round k-1

Page 7: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

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

visited(? = discover time)

discovered(? = discover time)

?

?

0 0

direction of edge whennew node is discovered

1

1 1

01

11

Page 8: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

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

visited(? = discover time)

discovered(? = discover time)

?

?

0

direction of edge whennew node is discovered

0 2

1

0

1

1

2 1

1

1 12

2

1

1

2

Page 9: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

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

visited(? = discover time)

discovered(? = discover time)

?

?

0

direction of edge whennew node is discovered

0

3

1

0

1

1 1

1 1

1

1

2

2

2

1 42

2 3

3

4

2

Page 10: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

10

Example (s = source)

v

r s

w x

t u

y

0

1 1

1

2

3

4

2Done when no newnode is discovered

v

r s

w x

t u

y

0

1 1

1

2

3

4

2 The directed edges forma tree that contains allnodes reachable from s

Called BFS tree of s

Page 11: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

11

Correctness

•The correctness of BFS follows from thefollowing theorem :

Theorem: A vertex v is discovered inRound k if and only if shortestdistance of v from source s is k

Proof: By induction

Page 12: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

12

Performance•BFS algorithm is easily done if we use

•an O(|V|)-size array to storediscovered/visited information

•a separate list for each round to storethe vertices discovered in that round

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

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

Page 13: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

13

Performance (2)•Instead of using a separate list for each

round, we can use a common queue•When a vertex is discovered, we put it

at the end of the queue•To pick a vertex to visit in Step 2, we

pick the one at the front of the queue•Done when no vertex is in the queue

No improvement in time/space … But algorithm is simplifiedQuestion: Can you prove the correctness of using queue?

Page 14: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

14

Depth First Search

Page 15: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

15

Depth First Search (DFS)•An alternative algorithm to find all

vertices reachable from a particularsource vertex s

•Idea:Explore a branch as far as possiblebefore exploring another branch

•Easily done by recursion or stack

Page 16: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

16

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

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

Mark u as finished ;}

The while-loop explores abranch as far as possiblebefore the next branch

Page 17: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

17

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 whennew node is discovered

Page 18: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

18

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 whennew node is discovered

Page 19: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

19

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 whennew node is discovered

Page 20: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

20

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 whennew node is discovered

Page 21: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

21

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 whennew node is discovered

Page 22: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

22

Example (s = source)

Done when s isdiscovered

The directed edges forma tree that contains allnodes reachable from s

Called DFS tree of s

v

r s

w x

t u

y

v

r s

w x

t u

y

Page 23: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

23

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 willcontinue to apply DFS algorithm on anyunvisited vertices …

Page 24: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

24

Generalization (Example)

v

r s

w x

t u

y

Suppose the input graph is directed

Page 25: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

25

Generalization (Example)

v

r s

w x

t u

y

1. After applying DFS on s

Page 26: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

26

Generalization (Example)

v

r s

w x

t u

y

2. Then, after applying DFS on t

Page 27: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

27

Generalization (Example)

v

r s

w x

t u

y

3. Then, after applying DFS on y

Page 28: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

28

Generalization (Example)

v

r s

w x

t u

y

4. Then, after applying DFS on r

Page 29: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

29

Generalization (Example)

v

r s

w x

t u

y

5. Then, after applying DFS on v

Page 30: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

30

Generalization (Example)

Result : a collection of rooted treescalled DFS forest

v

r s

w x

t u

y

Page 31: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

31

Performance

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

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

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

Page 32: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

32

Discovery and Finishing Times•When the DFS algorithm is run, let us

consider a global time such that the timeincreases 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, andf(u) = the time when u is finished

Page 33: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

33

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 34: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

34

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 35: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

35

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, either1. 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 36: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

36

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 37: CS2351 Data Structures - National Tsing Hua Universitywkhon/ds/ds11/lecture/lecture10.pdf2 About this lecture •We introduce two popular algorithms to traverse a graph 1. Breadth

37

CorollaryCorollary:

v is a (proper) descendant of uif 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)