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...

81
Lecture 15: Depth First Search Shang-Hua Teng

Post on 21-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Lecture 15:Depth First Search

Shang-Hua Teng

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

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

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>

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

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

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

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

Complete Graphs

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

A complete digraph has edges between any two vertices.

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

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

Color Scheme

• Vertices initially colored white

• Then colored gray when discovered

• Then black when finished

Time Stamps

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

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

• d[u] < f[u]

DFS Examplesourcevertex

DFS Example

1 | | |

| | |

| |

sourcevertex

d f

DFS Example

1 | | |

| | |

2 | |

sourcevertex

d f

DFS Example

1 | | |

| | 3 |

2 | |

sourcevertex

d f

DFS Example

1 | | |

| | 3 | 4

2 | |

sourcevertex

d f

DFS Example

1 | | |

| 5 | 3 | 4

2 | |

sourcevertex

d f

DFS Example

1 | | |

| 5 | 63 | 4

2 | |

sourcevertex

d f

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 |

sourcevertex

d f

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 |

sourcevertex

d f

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 9 |

sourcevertex

d f

DFS Example

1 | 8 | |

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

DFS Example

1 | 8 |11 |

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

DFS Example

1 |12 8 |11 |

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

DFS Example

1 |12 8 |11 13|

| 5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

DFS Example

1 |12 8 |11 13|

14| 5 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

DFS Example

1 |12 8 |11 13|

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

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)

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;

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)

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

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

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

DFS Example

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

Tree edges

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)

DFS Example

Tree edges Back edges

1 |12 8 |11 13|16

14|155 | 63 | 4

2 | 7 9 |10

sourcevertex

d f

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

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

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

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

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

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?

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?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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..

Directed Acyclic Graphs

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

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

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

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}

{ }

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

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