CS 401: Computer Algorithm I DFS / Topological Ordering Xiaorui - - PowerPoint PPT Presentation

cs 401 computer algorithm i
SMART_READER_LITE
LIVE PREVIEW

CS 401: Computer Algorithm I DFS / Topological Ordering Xiaorui - - PowerPoint PPT Presentation

CS 401: Computer Algorithm I DFS / Topological Ordering Xiaorui Sun 1 BFS Application: Testing Bipartiteness Problem: Given a graph ! , is it bipartite? Many graph problems become: Easier/Tractable if the underlying graph is bipartite


slide-1
SLIDE 1

CS 401: Computer Algorithm I

DFS / Topological Ordering

Xiaorui Sun

1

slide-2
SLIDE 2

BFS Application: Testing Bipartiteness

Problem: Given a graph !, is it bipartite? Many graph problems become:

  • Easier/Tractable if the underlying graph is bipartite (matching)

Before attempting to design an algorithm, we need to understand structure of bipartite graphs.

2

v1 v2 v3 v6 v5 v4 v7 v2 v4 v5 v7 v1 v3 v6 a bipartite graph G another drawing of G

slide-3
SLIDE 3

A Characterization of Bipartite Graphs

Lemma: Let ! be a connected graph, and let "0, … , "& be the layers produced by BFS('). Exactly one of the following holds. (i) No edge of ! joins two nodes of the same layer, and ! is bipartite. (ii) An edge of ! joins two nodes of the same layer, and ! contains an odd-length cycle (and hence is not bipartite).

3

Case (i)

L1 L2 L3

Case (ii)

L1 L2 L3

slide-4
SLIDE 4

Obstruction to Bipartiteness

Corollary: A graph ! is bipartite if and only if it contains no

  • dd length cycles.

Furthermore, one can test bipartiteness using BFS.

Bipartiteness algorithm:

  • Run BFS on an arbitrary vertex
  • If there is a non-tree edge connecting two vertices at the

same level of the BFS tree, then the graph is not a bipartite graph, otherwise, the graph is a bipartite graph.

4

slide-5
SLIDE 5

BFS Summary

Breadth First Search (BFS): Explore vertices according to the order of the discovery of vertices Property:

  • BFS tree
  • Level = distance (length of shortest path) from the initial

vertex

  • Every edge connect two vertices at the same or adjacent

levels Applications of BFS:

  • Finding connected components of a graph
  • Testing bipartiteness

5

slide-6
SLIDE 6

Depth First Search

Follow the first path you find as far as you can go; back up to last unexplored edge when you reach a dead end, then go as far you can Naturally implemented using recursive calls or a stack

6

slide-7
SLIDE 7

DFS(s) – Recursive version

Initialization: mark all vertices undiscovered DFS(!) Mark ! discovered for each edge {!, $} if ($ is undiscovered) DFS($) Mark ! fully-explored

7

slide-8
SLIDE 8

8

DFS(A)

A,1 B J I H C G F D E K L M

Suppose edge lists at each vertex are sorted alphabetically Color code: undiscovered discovered fully-explored Call Stack (Edge list): A (B,J) st[] = {1}

slide-9
SLIDE 9

9

DFS(A)

A,1 B,2 J I H C G F D E K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) st[] = {1,2}

slide-10
SLIDE 10

10

DFS(A)

A,1 B,2 J I H C,3 G F D E K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) st[] = {1,2,3}

slide-11
SLIDE 11

11

DFS(A)

A,1 B,2 J I H C,3 G F D,4 E K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) st[] = {1,2,3,4}

slide-12
SLIDE 12

12

DFS(A)

A,1 B,2 J I H C,3 G F D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) st[] = {1,2,3,4,5}

slide-13
SLIDE 13

13

DFS(A)

A,1 B,2 J I H C,3 G F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) st[] = {1,2,3,4,5, 6}

slide-14
SLIDE 14

14

DFS(A)

A,1 B,2 J I H C,3 G,7 F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) G(C,F) st[] = {1,2,3,4,5, 6,7}

slide-15
SLIDE 15

15

DFS(A)

A,1 B,2 J I H C,3 G,7 F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) G(C,F) st[] = {1,2,3,4,5, 6,7}

slide-16
SLIDE 16

16

DFS(A)

A,1 B,2 J I H C,3 G,7 F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) st[] = {1,2,3,4,5, 6}

slide-17
SLIDE 17

17

DFS(A)

A,1 B,2 J I H C,3 G,7 F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) st[] = {1,2,3,4,5}

slide-18
SLIDE 18

18

DFS(A)

A,1 B,2 J I H C,3 G,7 F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) st[] = {1,2,3,4}

slide-19
SLIDE 19

19

DFS(A)

A,1 B,2 J I H C,3 G,7 F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) st[] = {1,2,3}

slide-20
SLIDE 20

20

DFS(A)

A,1 B,2 J I H,8 C,3 G,7 F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) st[] = {1,2,3,8}

slide-21
SLIDE 21

21

DFS(A)

A,1 B,2 J I,9 H,8 C,3 G,7 F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) I (H) st[] = {1,2,3,8,9}

slide-22
SLIDE 22

22

DFS(A)

A,1 B,2 J I,9 H,8 C,3 G,7 F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) st[] = {1,2,3,8}

slide-23
SLIDE 23

23

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) st[] = {1,2,3,8, 10}

slide-24
SLIDE 24

24

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) st[] = {1,2,3,8,10 ,11}

slide-25
SLIDE 25

25

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) st[] = {1,2,3,8,10 ,11,12}

slide-26
SLIDE 26

26

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) M(L) st[] = {1,2,3,8,10 ,11,12,13}

slide-27
SLIDE 27

27

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) st[] = {1,2,3,8,10 ,11,12}

slide-28
SLIDE 28

28

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) st[] = {1,2,3,8,10 ,11}

slide-29
SLIDE 29

29

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) st[] = {1,2,3,8, 10}

slide-30
SLIDE 30

30

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) st[] = {1,2,3,8, 10}

slide-31
SLIDE 31

31

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) st[] = {1,2,3,8}

slide-32
SLIDE 32

32

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) st[] = {1,2,3}

slide-33
SLIDE 33

33

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) st[] = {1,2}

slide-34
SLIDE 34

34

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) B (A,C,J) st[] = {1,2}

slide-35
SLIDE 35

35

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) st[] = {1}

slide-36
SLIDE 36

36

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) A (B,J) st[] = {1}

slide-37
SLIDE 37

37

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Color code: undiscovered discovered fully-explored Call Stack: (Edge list) TA-DA!! st[] = {}

slide-38
SLIDE 38

38

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Edge code: Tree edge Back edge

slide-39
SLIDE 39

39

DFS(A)

A,1 B,2 J,10 I,9 H,8 C,3 G,7 F,6 D,4 E,5 K,11 L,12 M,13

Edge code: Tree edge Back edge No Cross Edges!

slide-40
SLIDE 40

Properties of (undirected) DFS

Like BFS(!):

  • DFS(!) visits " iff there is a path in G from ! to "

So, we can use DFS to find connected components

  • Edges into then-undiscovered vertices define a tree –

the "depth first spanning tree" of G Unlike the BFS tree:

  • The DF spanning tree isn't minimum depth
  • Its levels don't reflect min distance from the root
  • Non-tree edges never join vertices on the same or

adjacent levels

40

slide-41
SLIDE 41

Non-Tree Edges in DFS

BFS tree ≠ DFS tree, but, as with BFS, DFS has found a tree in the graph s.t. non-tree edges are "simple" in some way. All non-tree edges join a vertex and one of its descendents/ancestors in the DFS tree

41

slide-42
SLIDE 42

Non-Tree Edges in DFS

Lemma: For every edge {", $}, if {", $} is not in DFS tree, then

  • ne of " or $ is an ancestor of the other in the tree.

Proof: Suppose that " is visited first. Therefore DFS(") was called before DFS($) Since {", $} is not in DFS tree, $ was visited when the edge {", $} was examined during DFS(") Therefore $ was visited during the call to DFS(") so $ is a descendant of ".

42

slide-43
SLIDE 43

Directed Graph and Topological Ordering

slide-44
SLIDE 44

Directed Graphs

44

1 2 10 9 8 3 4 5 6 7 11 12 13

Multi edge self loop

slide-45
SLIDE 45

Node = intersection, edge = one-way street

Directed Graphs

45

slide-46
SLIDE 46

Precedence Constraints

In a directed graph, an edge (", $) means task " must occur before task $. Applications

  • Course prerequisite:

course " must be taken before $

  • Compilation:

must compile module " before $

  • Computing overflow:
  • utput of job " is part of input to job $
  • Manufacturing or assembly:

sand it before paint it

46

slide-47
SLIDE 47

Directed Acyclic Graphs (DAG)

Def: A DAG is a directed acyclic graph, i.e.,

  • ne that contains no directed cycles.

Def: A topological order of a directed graph G = (V, E) is an

  • rdering of its nodes as !", !$, … , !& so that for every edge

(!(, !)) we have + < -.

47

a DAG

2 3 6 5 4 7 1

a topological ordering of that DAG– all edges left-to-right

1 2 3 4 5 6 7

slide-48
SLIDE 48

DAGs: A Sufficient Condition

Lemma: If ! has a topological order, then ! is a DAG.

  • Proof. (by contradiction)

Suppose that ! has a topological order 1,2, … , & and that ! also has a directed cycle '. Let ( be the lowest-indexed node in ', and let ) be the node just before (; thus (), () is an (directed) edge. By our choice of (, we have ( < ). On the other hand, since (), () is an edge and 1, … , & is a topological order, we must have ) < (, a contradiction

48

1

i

j

n

the directed cycle C

the supposed topological order: 1,2,…,n

slide-49
SLIDE 49

DAGs: A Sufficient Condition

49

G has a topological order G is a DAG

?

slide-50
SLIDE 50

Every DAG has a source node

Lemma: If ! is a DAG, then ! has a node with no incoming edges (i.e., a source).

  • Proof. (by contradiction)

Suppose that ! is a DAG and it has no source Pick any node ", and begin following edges backward from ". Since " has at least one incoming edge ($, ") we can walk backward to $. Then, since $ has at least one incoming edge (', $), we can walk backward to '. Repeat until we visit a node, say w, twice. Let C be the sequence of nodes encountered between successive visits to w. C is a cycle.

50

w x u v

C

w x u v

slide-51
SLIDE 51

DAG => Topological Order

Lemma: If ! is a DAG, then ! has a topological order

  • Proof. (by induction on n)

Base case: true if " = 1. Hypothesis: Every DAG with " − 1 vertices has a topological ordering. Inductive Step: Given DAG with " > 1 nodes, find a source node '. ! − { ' } is a DAG, since deleting ' cannot create cycles. By hypothesis, ! − { ' } has a topological ordering. Place ' first in topological ordering; then append nodes of ! − {'} in topological order. This is valid since ' has no incoming edges.

51

Reminder: Always remove vertices/edges to use hypothesis

slide-52
SLIDE 52

A Characterization of DAGs

52

G has a topological order G is a DAG

slide-53
SLIDE 53

53

Topological Order Algorithm 1: Example

2 3 6 5 4 7 1

slide-54
SLIDE 54

54

Topological order: 1, 2, 3, 4, 5, 6, 7

Topological Order Algorithm 1: Example

2 3 6 5 4 7 1 1 2 3 4 5 6 7

Running time: O(n+m)

  • Adjacency list
  • Maintain # outgoing edge for each node