Top Banner
Lecture 15: Depth First Search Shang-Hua Teng
81

Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Dec 21, 2015

Download

Documents

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: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Lecture 15:Depth First Search

Shang-Hua Teng

Page 2: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Graphs• G= (V,E)

B

E

C

FD

A B

E

C

FD

A

• Directed Graph (digraph)– Degree: in/out

• Undirected Graph – Adjacency is symmetric

Page 3: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Graph Basics• The size of a graph is the number of its vertices• If two vertices are connected by an edge, they

are neighbors • The degree of a node is the number of edges it

has• For directed graphs,

– The in-degree of a node is the number of in-edges it has

– The out-degree of a node is the number of out-edges it has

Page 4: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Paths, Cycles• Path:

– simple: all vertices distinct• Cycle:

– Directed Graph: • <v0,v1,...,vk > forms cycle if v0=vk

and k>=1• simple cycle: v1,v2..,vk also distinct

– Undirected Graph: • <v0,v1,...,vk > forms (simple) cycle

if v0=vk and k>=3• simple cycle: v1,v2..,vk also distinct

B

E

C

FD

A path <A,B,F>path <A,B,F>

B

E

C

FD

A simple cycle simple cycle <E,B,F,E><E,B,F,E>

B

E

C

FD

Asimple cycle <A,B,C,A>= <B,C,A,B>simple cycle <A,B,C,A>= <B,C,A,B>

Page 5: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Connectivity

• Undirected Graph: connected– every pair of vertices is connected by a

path– one connected component– connected components:

• equivalence classes under “is reachable from” relation

• Directed Graph: strongly connected– every pair of vertices is reachable from

each other– one strongly connected component– strongly connected components:

• equivalence classes under “mutually reachable” relation

B

E

C

FD

Aconnectedconnected

B

E

C

FD

A2 connected 2 connected componentscomponents

strongly strongly connected connected componentcomponent

B

E

C

FD

A

not not strongly strongly connectedconnected

B

E

C

FD

A

Page 6: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Subgraphs• A subgraph S of a graph G

is a graph such that – The edges of S are a

subset of the edges of G– The edges of S are a

subset of the edges of G• A spanning subgraph of G

is a subgraph that contains all the vertices of G

Subgraph

Spanning subgraph

Page 7: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Trees and Forests• A (free) tree is an undirected

graph T such that

– T is connected

– T has no cycles

This definition of tree is different from the one of a rooted tree

• A forest is an undirected graph without cycles

• The connected components of a forest are trees

Tree

Forest

Page 8: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Spanning Trees and Forests• A spanning tree of a connected

graph is a spanning subgraph that is a tree

• A spanning tree is not unique unless the graph is a tree

• Spanning trees have applications to the design of communication networks

• A spanning forest of a graph is a spanning subgraph that is a forest

Graph

Spanning tree

Page 9: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Complete Graphs

A graph which has edge between all pair of vertex pair is complete.

A complete digraph has edges between any two vertices.

Page 10: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Graph Search (traversal)• How do we search a graph?

– At a particular vertices, where shall we go next?

• Two common framework: the breadth-first search (BFS) and the depth-first search (DFS)– In BFS, one explore a graph level by level away

(explore all neighbors first and then move on) – In DFS, go as far as possible along a single path until

reach a dead end (a vertex with no edge out or no neighbor unexplored) then backtrack

Page 11: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Depth-First Search• The basic idea behind this algorithm is

that it traverses the graph using recursion– Go as far as possible until you reach a

deadend– Backtrack to the previous path and try the

next branch– The algorithm in the book is overly

complicated, we will stick with the one on the right

– The graph below, started at node a, would be visited in the following order: a, b, c, g, h, i, e, d, f, j

dfs(g, current){ mark current as visited x = set of nodes adjacent

to current for(i gets next in x*) if(i has not been

visited yet) dfs(g, i)}

* - we will assume that adjacentnodes will be processed in numeric or alphabetical order

a b

h i

c

g

e

j

d

f

Page 12: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Color Scheme

• Vertices initially colored white

• Then colored gray when discovered

• Then black when finished

Page 13: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Time Stamps

• Discover time d[u]: when u is first discovered

• Finish time f[u]: when backtrack from u

• d[u] < f[u]

Page 14: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Examplesourcevertex

Page 15: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | | |

| | |

| |

sourcevertex

d f

Page 16: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | | |

| | |

2 | |

sourcevertex

d f

Page 17: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | | |

| | 3 |

2 | |

sourcevertex

d f

Page 18: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | | |

| | 3 | 4

2 | |

sourcevertex

d f

Page 19: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | | |

| 5 | 3 | 4

2 | |

sourcevertex

d f

Page 20: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | | |

| 5 | 63 | 4

2 | |

sourcevertex

d f

Page 21: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 |

sourcevertex

d f

Page 22: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 |

sourcevertex

d f

Page 23: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 9 |

sourcevertex

d f

Page 24: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 25: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 | 8 |11 |

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 26: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 |12 8 |11 |

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 27: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 |12 8 |11 13|

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 28: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 |12 8 |11 13|

14| 5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 29: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 |12 8 |11 13|

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 30: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 31: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

PseudocodeDFS(G)

1. For each v in V,

2. color[v]=white; [u]=NIL

3. time=0;

4. For each u in V

5. If (color[u]=white)

6. DFS-VISIT(u)

Page 32: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

1. DFS-VISIT(u)

2. color[u]=gray;

3. time = time + 1; d[u] = time;

4. For each v in Adj(u) do

5. If (color[v] = white)

6. [v] = u;

7. DFS-VISIT(v);

8. color[u] = black;

9. time = time + 1; f[u]= time;

Page 33: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Complexity Analysis

There is only one DFS-VISIT(u) for each vertex u.

Ignoring the recursion calls the complexity is O(deg(u)+1)

The recursive call on v is charged to DFS-VISIT(v)

Initialization complexity is O(V)

Overall complexity is O(V + E)

Page 34: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Forest

1. [v] = u then (u,v) is an edge

2. All descendant of u are reachable from u

3. defines a spanning forest

Page 35: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Parenthesis Lemma Relation between timestamps and ancestry

• u is an ancestor of v if and only if [d[u], f[u]] [d[v], f[v]]

• u is a descendent of v if and only if [d[u], f[u]][d[v], f[v]]

• u and v are not related if and only if [d[u], f[u]] and [d[v], f[v]] are disjoint

Page 36: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS: Tree (Forest) Edges

• DFS introduces an important distinction among edges in the original graph:– Tree edge: encounter new (white) vertex

• The tree edges form a spanning forest

Page 37: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Tree edges

Page 38: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS: Back Edges

• DFS introduces an important distinction among edges in the original graph:– Tree edge: encounter new (white) vertex – Back edge: from descendent to ancestor

• Encounter a grey vertex (grey to grey)

Page 39: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

Tree edges Back edges

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 40: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS: Forward Edges

• DFS introduces an important distinction among edges in the original graph:– Tree edge: encounter new (white) vertex – Back edge: from descendent to ancestor– Forward edge: from ancestor to descendent

• Not a tree edge, though

• From grey node to black node

Page 41: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

Tree edges Back edges Forward edges

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 42: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS: Cross Edges

• DFS introduces an important distinction among edges in the original graph:– Tree edge: encounter new (white) vertex – Back edge: from descendent to ancestor– Forward edge: from ancestor to descendent– Cross edge: between a tree or subtrees

• From a grey node to a black node

Page 43: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Example

Tree edges Back edges Forward edges Cross edges

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Page 44: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS: Types of edges

• DFS introduces an important distinction among edges in the original graph:– Tree edge: encounter new (white) vertex

– Back edge: from descendent to ancestor

– Forward edge: from ancestor to descendent

– Cross edge: between a tree or subtrees

• Note: tree & back edges are important; most algorithms don’t distinguish forward & cross

Page 45: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS: Undirected Graph

• Theorem: If G is undirected, a DFS produces only tree and back edges

• Proof by contradiction:– Assume there’s a forward edge

• But F? edge must actually be a back edge (why?)

sourceF?

Page 46: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS: Undirected Graph

• Theorem: If G is undirected, a DFS produces only tree and back edges

• Proof by contradiction:– Assume there’s a cross edge

• But C? edge cannot be cross:• must be explored from one of the

vertices it connects, becoming a treevertex, before other vertex is explored

• So in fact the picture is wrong…bothlower tree edges cannot in fact betree edges

source

C?

Page 47: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS Undirected Graph

• Theorem: An undirected graph is acyclic iff a DFS yields no back edges– If acyclic, no back edges (because a back edge implies

a cycle– If no back edges, acyclic

• No back edges implies only tree edges (Why?)• Only tree edges implies we have a tree or a forest• Which by definition is acyclic

• Thus, can run DFS to find whether a graph has a cycle

Page 48: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

G=(V,E)G=(V,E)Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

Page 49: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

Page 50: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

Page 51: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

Page 52: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TT

Page 53: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TT

Page 54: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBB

Page 55: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

Page 56: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

Page 57: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

Page 58: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB

Page 59: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

Page 60: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

Page 61: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TT

Page 62: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TT

Page 63: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TTBB

Page 64: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TTBB

Page 65: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TTBB

Page 66: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TTBB

Page 67: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TTBBTT

Page 68: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TTBBTT

Page 69: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TTBBTT

BB

Page 70: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TTBBTT

BB

Page 71: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TTBBTT

BB

Page 72: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

AA

BB

CC

DD

EEFF

GG

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

TT

TTBBTT

BB

BB TT

TTBBTT

BB

Page 73: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: DFS of Undirected Graph

Adjacency ListAdjacency List::A: B,C,D,FA: B,C,D,FB: A,C,DB: A,C,DC: A,B,D,E,FC: A,B,D,E,FD: A,B,C,GD: A,B,C,GE: C,GE: C,GF: A,CF: A,CG: D,EG: D,E

AA

BB

CC

DD

EEFF

GG

TT

TTBBTT

BB

BB TT

TTBBTT

BB

Page 74: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Example: (continued)

DFS of Undirected Graph

DFS TreeDFS Tree

AA

BB

CC

DD

EEFF

GG

TT

TTBBTT

BB

BB TT

TTBBTT

BB

AA

CC

DD

EE

FF

GG

BB

BB

TT

TT

TT

TT

TT

TT

BB

BB

BB

BB

Page 75: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Using DFS & BFS

A directed graph G is acyclic if and only if a Depth-A directed graph G is acyclic if and only if a Depth-First Search of G yields no back edges.First Search of G yields no back edges.

• Using DFS to Detect Cycles:

• Using BFS for Shortest Paths:A Breadth-First Search of G yields shortest path information: A Breadth-First Search of G yields shortest path information:

For each Breadth-First Search tree, the path from its root u For each Breadth-First Search tree, the path from its root u to a vertex v yields the shortest path from u to v in Gto a vertex v yields the shortest path from u to v in G..

Page 76: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Directed Acyclic Graphs

• A directed acyclic graph or DAG is a directed graph with no directed cycles:

Page 77: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Directed Acyclic Graphs (DAGs)

• DAG - digraph with no cycles

• compare: tree, DAG, digraph with cycle

D E

CB

A

D E

CB

A

D E

CB

A

Page 78: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Where DAGs are used

• Syntactic structure of arithmetic expressions with common sub-expressions

e.g. ((a+b)*c + ((a+b)+e)*(e+f)) * ((a+b)*c)

*+

**

+ ++

a b

c

e f

Page 79: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Where DAGs are used?

• To represent partial orders

• A partial order R on a set S is a binary relation such that– for all a in S a R a is false (irreflexive)

– for all a, b, c in S if a R b and b R c then a R c (transitive)

• examples: “less than” (<) and proper containment on sets

• S ={1, 2, 3}

• P(S) - power set of S

(set of all subsets)

{1, 2, 3}

{1, 2} {1, 3} {2, 3}

{1} {2} {3}

{ }

Page 80: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DAGs in use

• To model course prerequisites or dependent tasks

Year 1 Year 2 Year 3 Year 4

Compilerconstruction

Prog.Languages

DS&APUMA

Data &Prog.

DiscreteMath

DataComm 1

DataComm 2

OpSystems

Real timesystems

DistributedSystems

Page 81: Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

DFS and DAGs

• Theorem: a directed graph G is acyclic iff a DFS of G yields no back edges:– => if G is acyclic, will be no back edges

• Trivial: a back edge implies a cycle

– <= if no back edges, G is acyclic• Proof by contradiction: G has a cycle a back edge

– Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle

– When v discovered, whole cycle is white– Must visit everything reachable from v before returning from

DFS-Visit()– So path from uv is greygrey, thus (u, v) is a back edge