SlideShare a Scribd company logo
Graph Algorithms Using Depth
First Search
Prepared by
John Reif, Ph.D.
Distinguished Professor of Computer Science
Duke University
Graph Algorithms Using Depth First
Search
a) Graph Definitions
b) DFS of Graphs
c) Biconnected Components
d) DFS of Digraphs
e) Strongly Connected Components
Readings on Graph Algorithms
Using Depth First Search
• Reading Selection:
– CLR, Chapter 22
Graph Terminology
• Graph: G=(V,E)
• Vertex set: V
• Edge set: E pairs of vertices which
are adjacent
Directed and Undirected Graphs
• G directed
• G undirected
if edges ordered pairs (u,v)
· ·
u v
if edges unordered pairs {u,v}
· ·
u v
Proper Graphs and Subgraphs
• Proper graph:
– No loops
– No multi-edges
• Subgraph G’ of G
G’ = (V’, E’) where
V’ is a subset of V and E’ is a subset of E
between vertices of V’.
Paths in Graphs
• Path p
P is a sequence of vertices v0, v1, …, vk
where for i=1,…k, vi-1 is adjacent to vi
Equivalently, p is a sequence of edges
e1, …, ek where for i = 2,…k each
consecutive pair of edges ei-1, ei share a
vertex
V1
V2 Vk-1 Vk
ek
e2
e1
Vo
Simple Paths and Cycles
• Simple path
no edge or vertex repeated,
except possibly vo = vk
• Cycle
a path p with vo = vk where k > 1
V1
V2 Vk-1
Vk = Vo
A Connected Undirected Graph
G is if path between
each pair of vertices
connected $
Connected Components of an
Undirected Graph
else G has 2 :
maximal connected subgraphs
connected components
³
Biconnected Component:
sets of edges where:
or is a single edge
There are a least two disjoint paths
between each pair of vertices.
Size of a Graph
• Graph G = (V,E)
n = |V| = # vertices
m = |E| = # edges
size of G is n+m
4
2 3
1
Representing a Graph
by an Adjacency Matrix
2
A is size n n
1 2 3 4
1 0 1 1 0
1 (i,j) E 2 1 0 1 0
A(i,j)
0 else 3 1 1 0 1
4 0 0 1 0
space cost n -n
´
Î
ì
= í
î
4
2 3
1
Adjacency Matrix A
Adjacency List Representation
of a Graph
• Adjacency Lists Adj (1),…,Adj (n)
Adj(v) = list of vertices adjacent to v
space cost O(n+m)
1
2
3
4
2
3
1
1
3
3
2 4
4
2 3
1
Definition of an Undirected Tree
• Tree
T is graph with unique simple path
between every pair of vertices
n = # vertices
n-1 = # edges
• Forest
– set of trees
Definition of a Directed Tree
• Directed Tree
T is digraph with distinguished vertex root r
such that each vertex reachable from r by a
unique path
Family Relationships:
- ancestors
- descendants
- parent
- child
- siblings
r
leaves have no proper descendants
An Ordered Tree
• Ordered Tree
– is a directed tree with siblings ordered
B
A
C
D
E
F
H I
Preorder Tree Traversal
• Preorder: A,B,C,D,E,F,H,I
[1] root (order vertices as pushed on
stack)
[2] preorder left subtree
[3] preorder right subtree
B
A
C
D
E
F
H I
Postorder Tree Traversal
• Postorder: B,E,D,H,I,F,C,A
[1] postorder left subtree
[2] postorder right subtree
[3] root (order vertices as popped off
stack)
B
A
C
D
E
F
H I
Spanning Tree and Forest of a
Graph
• T is a spanning tree of graph G if
(1) T is a directed tree with the same vertex
set as G
(2) each edge of T is a directed version of an edge of
G
• Spanning Forest:
forest of spanning trees of connected components of
G
Example Spanning Tree of a Graph
1
6
2
4
3
5
7
8
root
tree edge back edge
9
10
11
12
No cross edge
Classification of Edges of G with
Spanning Tree T
• An edge (u,v) of T is tree edge
• An edge (u,v) of G-T is back edge if u is a descendent
or ancestor of v.
• Else (u,v) is a cross edge (do not exist in undirected
DFS)
Tarjan’s Depth First Search Algorithm
• We assume a Random Access Machine (RAM)
computational model
• Algorithm Depth First Search
graph G (V,E) represented by
adjacency lists Adj(v) for each v V
[0] N 0
[1] all v V (number (v) 0
children (v) ( ) )
[2] all v V do
Input
for do
od
for
=
Î
¬
Î ¬
¬
Î
number (v) 0 DFS(v)
[3] spanning forest defined by children
if then
output
=
Recursive DFS Procedure
DFS(v)
[1] N N + 1; number (v) N
[2] for each u Adj(v)
number (u) 0
(add u to children (v); DFS(u))
recursive procedure
do
if then
¬ ¬
Î
=
The preorder numbers give the order the vertices are first
visited in DFS.
Time Cost of Depth First Search
(DFS) Algorithm on a RAM
• Input size n = |V|, m = |E|
• Theorem Depth First Search takes linear time cost
O(n+m)
• Proof
Can associate with each edge and vertex
a constant number of operations.
Classification of Edges of G via DFS
Spanning Tree T
• Edge notation induced by
• Depth First Search Tree T
u v iff (u,v) is tree edge of T
u v iff u is an ancestor of v
u --- v iff (u,v) is backedge if (u,v) G-T
with either u v or v u

 



 
Classification of Edges of Graph G
via DFS Spanning Tree T
1
6
2
4
3
5
7
8
G
1
6
2
4
3
5
7
8
T
Preorder numbering vertices by
order visited in DFS
DFS Spanning Tree T:
Classification of Edges of G via DFS
Spanning Tree T (cont’d)
• Note DFS tree T of an undirected graph has no
cross edges (u,v) where u,v are unrelated in T
u u
Verifying Vertices are Descendants
via Preordering of Tree
• Suppose we preorder number a Tree T
Let Dv = # of descendants of v (found in DFS)
• Lemma
u is descendant of v
iff v < u < v + Dv
v
u
Dv
W
*
Testing for Proper Ancestors via
Preordering
• Lemma
If u is descendant of v
and (u,w) is back edge s.t. w < v
then w is a proper ancestor of v
Low Values
• For each vertex v,
define low(v) = min ( {v} ∪ {w | v→ - - - w} )
• Can prove by induction:
Lemma
Can be computed during DFS in postorder.
*
low(v) min ( {v} {low(w)|v w} {w|v - - - w} )
= È ® È ®
Graph with DFS Numbering of
vertices [Low Values in Brackets]
7[5]
1[1]
2[2]
4[2]
3[2]
5[1]
8[1]
6[5]
v[low(v)]
Biconnected Components
• G is Biconnected iff either
(1) G is a single edge, or
(2) for each triple of vertices u,v,w
w-avoiding path from u to v
(equivalently: two disjoint paths from u to v)
$
$
Example of Biconnected
Components of Graph G
• Maximal edge subgraphs
of G which are biconnected.
5
1
8
1
6
2
4
3
5
7
8
1
2
2
3
4
6
5
7
Biconnected
Components
G
Biconnected Components Meet at
Articulation Points
• The intersection of two biconnected components
consists of at most one vertex called an Articulation
Point.
• Example: 1,2,5 are articulation points
1
6
2
4
3
5
7
8
G
Discovery of Biconnected
Components via Articulation Points
=> If can find articulation points
then can compute biconnected components:
Algorithm:
• During DFS, use auxiliary stack to store visited
edges.
• Each time we complete the DFS of a tree child of an
articulation point, pop all stacked edges
• currently in stack
• These popped off edges form a biconnected
component
Characterization of an Articulation Point
• Theorem
a is an articulation point iff either
(1) a is root with ≥ 2 tree children
or
(2) a is not root but a has a tree child v with
low (v) ≥ a
(note easy to check given low computation)
Proof of Characterization of an
Articulation Point
• Proof
The conditions are sufficient since any
a-avoiding path from v remains in the
subtree Tv rooted at v, if v is a child of a
• To show condition necessary, assume a is
an articulation point.
Characterization of an Articulation
Point (cont’d)
• Case (1)
If a is a root and is articulation point,
a must have ≥ 2 tree edges to two distinct biconnected
components.
• Case(2)
If a is not root, consider graph G - {a}
which must have a connected component C consisting
of only descendants of a, and
with no backedge from C to an ancestor
of v. Hence a has a tree child v in C and
low (v) ≥ a
Proof of Characterization of an
Articulation Point (cont’d)
a
v
root
Articulation Point a
is not the root
subtree Tv
C
no
back
edges
• Case (2)
Computing Biconnected Components
• Theorem
The Biconnected Components of G = (V,E) can
be computed in time O(|V|+|E|) using a RAM
• Proof idea
• Use characterization of Bicoconnected
components via articulation points
• Identify these articulation points dynamically
during depth first search
• Use a secondary stack to store the edges of the
biconnected components as they are visited
• When an articulation point is discovered , pop the
edges of this stack off to output a biconnected
component
Biconnected Components Algorithm
[0] initialize a STACK to empty
During a DFS traversal do
[1] add visited edge to STACK
[2] compute low of visited vertex v using Lemma
[3] test if v is an articulation point
[4] if so, for each u ∈ children (v) in order
where low (u) > v
do Pop all edges in STACK
upto and including tree edge (v,u)
Output popped edges as a
biconnected component of G
od
Time Bounds of Biconnected
Components Algorithm
• Time Bounds:
Each edge and vertex can be associated
with 0(1) operations. So time O(|V|+|E|).
Depth First Search in a Directed Graph
Depth first search tree T of Directed Graph
cross cross
cross
cycle
forward
cycle
1
1
2
3
4
5
6
7
8
2
4
5
6
7
8
G = (V,E)
i = DFS number = order
discovered
v
j = postorder = order vertex
popped off DFS stack
edge set E partitioned:
3
forward
The preorder numbers give order
vertices are first visited in DFS:
The postorder numbers give order
vertices are last visited in DFS.
Classification of Directed Edge (u, v)
via DFS Spanning Tree T
• Tree edge (u,v) : (u parent of v in T)
• Cycle edge (u,v): (u is descendant of v in T)
• Forward edge (u,v): (u is descendant of v in T)
• Cross edge(u,v): (u,v not related in T)
*
if v u

if u v in T
®
otherwise
*
if (u,v) T
but u v in T


Topological Order of Acyclic
Directed Graph
• Digraph G = (V,E) is acyclic if it has
no cycles
1 n
i j
V {v ,...v } satisfies
(v ,v ) E i j
Topological Order
=
Î Þ <
Characterization of an Acyclic Digraph
• Proof
Case (1):
• Suppose (u,v) ∈ E is a cycle edge, so v → u.
• But let e1,…ek be the tree edges from v to u.
• Then (u,v), e1,…ek is a cycle.
*
G is acylic iff no cycle edge
$
Characterization of an Acyclic
Digraph (cont’d)
Case (2):
• Suppose G has no a cycle edge
• Then order vertices in postorder of DFS
spanning forest (i.e. in order vertices are
popped off DFS stack).
• This is a reverse topological order of G.
• So, G can have no cycles.
Note:
Gives an O(|V|+|E|) algorithm
for computing Topological Ordering
of an acyclic graph G = (V,E)
Strong Components of Directed
Graph
• Strong Component
• Collapsed Graph
G* derived by
collapsing each strong component
into a single vertex.
note: G* is acyclic.
maximum set vertices
S of V such that u,v S
cycle containing u,v
 

Directed Graph with Strong
Components Circled
Directed Graph G = (V,E)
3
1
2
4
7
5
6
8
Algorithm Strong Components
Input digraph G
[1] Perform DFS on G. Renumber vertices
by postorder of the DFS of G.
[2] Let G- be the reverse digraph derived
from G by reversing direction of each edge.
Algorithm Strong Components
(cont’d)
[3] Perform DFS on G-, starting at highest
(in postorder of G) numbered vertex.
Output resulting DFS tree of G- as a strongly
connected component of G.
[4] repeat [3], starting at highest numbered
vertex not so for visited (halt when all
vertices visited)
Time Bounds of Algorithm Strong
Components
• Time Bounds
each DFS costs time c(|V|+|E|)
So total time = 2*c(|V|+|E|)
≤ O(|V|+|E|)
Example of Step [1] of Strong
Components Algorithm
3
1
2
4
7
5
6
8
8
7
6
5
3
4
2
1
Example Input
Digraph G
(postorder numbering
of G given in gold)
Example of Step [2] of Strong
Components Algorithm
3
1
2
4
7
5
6
8
8
7
6
5
3
4
2
1
Reverse
Digraph G-
(postorder numbering
of G given in gold)
Proof of Strong Components Algorithm
• Theorem
The Algorithm outputs the
strong components of G.
• Proof
We must show these are exactly the vertices in each
DFS spanning forest of reverse graph G-
Proof of Strong Components
Algorithm (continued)
• Suppose:
• v,w in the same strong component and DFS search
in G- starts at vertex r and reaches v.
• Then w will also be reached in the DFS search in G- .
• So v,w are output together in the same spanning tree of
G-.
Proof of Strong Components
Algorithm (continued)
• Suppose:
• v,w output in same spanning tree of G-.
• Let r be the root of that spanning tree of G-.
• Then there exists paths in G- from r to each of v and w.
• So there exists paths in G to r from each of v and w.
Proof of Strong Components
Algorithm (continued)
• r is root of spanning tree of G- containing v and w.
• Suppose: no path in G to r from v.
• Then since r has a higher postorder than v, there is no
path in G from v to r, a contradiction.
• Hence, there exists path in G from r to v, and similar
argument gives path from r to w.
• So v and w are in a cycle of G and must be in the same
strong component, completing proof !

More Related Content

Similar to ALG5.1.ppt

Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
ChristianKapsales1
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
Aakash deep Singhal
 
Ppt 1
Ppt 1Ppt 1
Topological Sort
Topological SortTopological Sort
Topological Sort
Dr Sandeep Kumar Poonia
 
8-Graph.ppt
8-Graph.ppt8-Graph.ppt
8-Graph.ppt
ssuser55cbdb
 
Graph
GraphGraph
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
SSE_AndyLi
 
Lecture13
Lecture13Lecture13
Lecture13
vaishali_singh
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
Hector Zenil
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
AJAL A J
 
Graphs
GraphsGraphs
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
ARVIND SARDAR
 
Graphs
GraphsGraphs
Graphs
Dwight Sabio
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
ssuser034ce1
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
ssuser034ce1
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
Edhole.com
 
Graph
GraphGraph
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
whittemorelucilla
 

Similar to ALG5.1.ppt (20)

Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
8-Graph.ppt
8-Graph.ppt8-Graph.ppt
8-Graph.ppt
 
Graph
GraphGraph
Graph
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
 
Lecture13
Lecture13Lecture13
Lecture13
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 
Graphs
GraphsGraphs
Graphs
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Graphs
GraphsGraphs
Graphs
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Graph
GraphGraph
Graph
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 

Recently uploaded

办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
xjq03c34
 
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
ysasp1
 
Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!
Toptal Tech
 
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
uehowe
 
HijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process HollowingHijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process Hollowing
Donato Onofri
 
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
uehowe
 
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
rtunex8r
 
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
uehowe
 
Discover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to IndiaDiscover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to India
davidjhones387
 
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
fovkoyb
 
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
k4ncd0z
 
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
3a0sd7z3
 
Bengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal BrandingBengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal Branding
Tarandeep Singh
 
Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?
Paul Walk
 
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmanuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
wolfsoftcompanyco
 
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
3a0sd7z3
 

Recently uploaded (16)

办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
 
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
 
Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!
 
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
 
HijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process HollowingHijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process Hollowing
 
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
 
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
 
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
 
Discover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to IndiaDiscover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to India
 
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
 
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
 
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
 
Bengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal BrandingBengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal Branding
 
Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?
 
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmanuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
 
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
 

ALG5.1.ppt

  • 1. Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University
  • 2. Graph Algorithms Using Depth First Search a) Graph Definitions b) DFS of Graphs c) Biconnected Components d) DFS of Digraphs e) Strongly Connected Components
  • 3. Readings on Graph Algorithms Using Depth First Search • Reading Selection: – CLR, Chapter 22
  • 4. Graph Terminology • Graph: G=(V,E) • Vertex set: V • Edge set: E pairs of vertices which are adjacent
  • 5. Directed and Undirected Graphs • G directed • G undirected if edges ordered pairs (u,v) · · u v if edges unordered pairs {u,v} · · u v
  • 6. Proper Graphs and Subgraphs • Proper graph: – No loops – No multi-edges • Subgraph G’ of G G’ = (V’, E’) where V’ is a subset of V and E’ is a subset of E between vertices of V’.
  • 7. Paths in Graphs • Path p P is a sequence of vertices v0, v1, …, vk where for i=1,…k, vi-1 is adjacent to vi Equivalently, p is a sequence of edges e1, …, ek where for i = 2,…k each consecutive pair of edges ei-1, ei share a vertex V1 V2 Vk-1 Vk ek e2 e1 Vo
  • 8. Simple Paths and Cycles • Simple path no edge or vertex repeated, except possibly vo = vk • Cycle a path p with vo = vk where k > 1 V1 V2 Vk-1 Vk = Vo
  • 9. A Connected Undirected Graph G is if path between each pair of vertices connected $
  • 10. Connected Components of an Undirected Graph else G has 2 : maximal connected subgraphs connected components ³
  • 11. Biconnected Component: sets of edges where: or is a single edge There are a least two disjoint paths between each pair of vertices.
  • 12. Size of a Graph • Graph G = (V,E) n = |V| = # vertices m = |E| = # edges size of G is n+m 4 2 3 1
  • 13. Representing a Graph by an Adjacency Matrix 2 A is size n n 1 2 3 4 1 0 1 1 0 1 (i,j) E 2 1 0 1 0 A(i,j) 0 else 3 1 1 0 1 4 0 0 1 0 space cost n -n ´ Î ì = í î 4 2 3 1 Adjacency Matrix A
  • 14. Adjacency List Representation of a Graph • Adjacency Lists Adj (1),…,Adj (n) Adj(v) = list of vertices adjacent to v space cost O(n+m) 1 2 3 4 2 3 1 1 3 3 2 4 4 2 3 1
  • 15. Definition of an Undirected Tree • Tree T is graph with unique simple path between every pair of vertices n = # vertices n-1 = # edges • Forest – set of trees
  • 16. Definition of a Directed Tree • Directed Tree T is digraph with distinguished vertex root r such that each vertex reachable from r by a unique path Family Relationships: - ancestors - descendants - parent - child - siblings r leaves have no proper descendants
  • 17. An Ordered Tree • Ordered Tree – is a directed tree with siblings ordered B A C D E F H I
  • 18. Preorder Tree Traversal • Preorder: A,B,C,D,E,F,H,I [1] root (order vertices as pushed on stack) [2] preorder left subtree [3] preorder right subtree B A C D E F H I
  • 19. Postorder Tree Traversal • Postorder: B,E,D,H,I,F,C,A [1] postorder left subtree [2] postorder right subtree [3] root (order vertices as popped off stack) B A C D E F H I
  • 20. Spanning Tree and Forest of a Graph • T is a spanning tree of graph G if (1) T is a directed tree with the same vertex set as G (2) each edge of T is a directed version of an edge of G • Spanning Forest: forest of spanning trees of connected components of G
  • 21. Example Spanning Tree of a Graph 1 6 2 4 3 5 7 8 root tree edge back edge 9 10 11 12 No cross edge
  • 22. Classification of Edges of G with Spanning Tree T • An edge (u,v) of T is tree edge • An edge (u,v) of G-T is back edge if u is a descendent or ancestor of v. • Else (u,v) is a cross edge (do not exist in undirected DFS)
  • 23. Tarjan’s Depth First Search Algorithm • We assume a Random Access Machine (RAM) computational model • Algorithm Depth First Search graph G (V,E) represented by adjacency lists Adj(v) for each v V [0] N 0 [1] all v V (number (v) 0 children (v) ( ) ) [2] all v V do Input for do od for = Î ¬ Î ¬ ¬ Î number (v) 0 DFS(v) [3] spanning forest defined by children if then output =
  • 24. Recursive DFS Procedure DFS(v) [1] N N + 1; number (v) N [2] for each u Adj(v) number (u) 0 (add u to children (v); DFS(u)) recursive procedure do if then ¬ ¬ Î = The preorder numbers give the order the vertices are first visited in DFS.
  • 25. Time Cost of Depth First Search (DFS) Algorithm on a RAM • Input size n = |V|, m = |E| • Theorem Depth First Search takes linear time cost O(n+m) • Proof Can associate with each edge and vertex a constant number of operations.
  • 26. Classification of Edges of G via DFS Spanning Tree T • Edge notation induced by • Depth First Search Tree T u v iff (u,v) is tree edge of T u v iff u is an ancestor of v u --- v iff (u,v) is backedge if (u,v) G-T with either u v or v u        
  • 27. Classification of Edges of Graph G via DFS Spanning Tree T 1 6 2 4 3 5 7 8 G 1 6 2 4 3 5 7 8 T Preorder numbering vertices by order visited in DFS DFS Spanning Tree T:
  • 28. Classification of Edges of G via DFS Spanning Tree T (cont’d) • Note DFS tree T of an undirected graph has no cross edges (u,v) where u,v are unrelated in T u u
  • 29. Verifying Vertices are Descendants via Preordering of Tree • Suppose we preorder number a Tree T Let Dv = # of descendants of v (found in DFS) • Lemma u is descendant of v iff v < u < v + Dv v u Dv W *
  • 30. Testing for Proper Ancestors via Preordering • Lemma If u is descendant of v and (u,w) is back edge s.t. w < v then w is a proper ancestor of v
  • 31. Low Values • For each vertex v, define low(v) = min ( {v} ∪ {w | v→ - - - w} ) • Can prove by induction: Lemma Can be computed during DFS in postorder. * low(v) min ( {v} {low(w)|v w} {w|v - - - w} ) = È ® È ®
  • 32. Graph with DFS Numbering of vertices [Low Values in Brackets] 7[5] 1[1] 2[2] 4[2] 3[2] 5[1] 8[1] 6[5] v[low(v)]
  • 33. Biconnected Components • G is Biconnected iff either (1) G is a single edge, or (2) for each triple of vertices u,v,w w-avoiding path from u to v (equivalently: two disjoint paths from u to v) $ $
  • 34. Example of Biconnected Components of Graph G • Maximal edge subgraphs of G which are biconnected. 5 1 8 1 6 2 4 3 5 7 8 1 2 2 3 4 6 5 7 Biconnected Components G
  • 35. Biconnected Components Meet at Articulation Points • The intersection of two biconnected components consists of at most one vertex called an Articulation Point. • Example: 1,2,5 are articulation points 1 6 2 4 3 5 7 8 G
  • 36. Discovery of Biconnected Components via Articulation Points => If can find articulation points then can compute biconnected components: Algorithm: • During DFS, use auxiliary stack to store visited edges. • Each time we complete the DFS of a tree child of an articulation point, pop all stacked edges • currently in stack • These popped off edges form a biconnected component
  • 37. Characterization of an Articulation Point • Theorem a is an articulation point iff either (1) a is root with ≥ 2 tree children or (2) a is not root but a has a tree child v with low (v) ≥ a (note easy to check given low computation)
  • 38. Proof of Characterization of an Articulation Point • Proof The conditions are sufficient since any a-avoiding path from v remains in the subtree Tv rooted at v, if v is a child of a • To show condition necessary, assume a is an articulation point.
  • 39. Characterization of an Articulation Point (cont’d) • Case (1) If a is a root and is articulation point, a must have ≥ 2 tree edges to two distinct biconnected components. • Case(2) If a is not root, consider graph G - {a} which must have a connected component C consisting of only descendants of a, and with no backedge from C to an ancestor of v. Hence a has a tree child v in C and low (v) ≥ a
  • 40. Proof of Characterization of an Articulation Point (cont’d) a v root Articulation Point a is not the root subtree Tv C no back edges • Case (2)
  • 41. Computing Biconnected Components • Theorem The Biconnected Components of G = (V,E) can be computed in time O(|V|+|E|) using a RAM • Proof idea • Use characterization of Bicoconnected components via articulation points • Identify these articulation points dynamically during depth first search • Use a secondary stack to store the edges of the biconnected components as they are visited • When an articulation point is discovered , pop the edges of this stack off to output a biconnected component
  • 42. Biconnected Components Algorithm [0] initialize a STACK to empty During a DFS traversal do [1] add visited edge to STACK [2] compute low of visited vertex v using Lemma [3] test if v is an articulation point [4] if so, for each u ∈ children (v) in order where low (u) > v do Pop all edges in STACK upto and including tree edge (v,u) Output popped edges as a biconnected component of G od
  • 43. Time Bounds of Biconnected Components Algorithm • Time Bounds: Each edge and vertex can be associated with 0(1) operations. So time O(|V|+|E|).
  • 44. Depth First Search in a Directed Graph Depth first search tree T of Directed Graph cross cross cross cycle forward cycle 1 1 2 3 4 5 6 7 8 2 4 5 6 7 8 G = (V,E) i = DFS number = order discovered v j = postorder = order vertex popped off DFS stack edge set E partitioned: 3 forward The preorder numbers give order vertices are first visited in DFS: The postorder numbers give order vertices are last visited in DFS.
  • 45. Classification of Directed Edge (u, v) via DFS Spanning Tree T • Tree edge (u,v) : (u parent of v in T) • Cycle edge (u,v): (u is descendant of v in T) • Forward edge (u,v): (u is descendant of v in T) • Cross edge(u,v): (u,v not related in T) * if v u  if u v in T ® otherwise * if (u,v) T but u v in T  
  • 46. Topological Order of Acyclic Directed Graph • Digraph G = (V,E) is acyclic if it has no cycles 1 n i j V {v ,...v } satisfies (v ,v ) E i j Topological Order = Î Þ <
  • 47. Characterization of an Acyclic Digraph • Proof Case (1): • Suppose (u,v) ∈ E is a cycle edge, so v → u. • But let e1,…ek be the tree edges from v to u. • Then (u,v), e1,…ek is a cycle. * G is acylic iff no cycle edge $
  • 48. Characterization of an Acyclic Digraph (cont’d) Case (2): • Suppose G has no a cycle edge • Then order vertices in postorder of DFS spanning forest (i.e. in order vertices are popped off DFS stack). • This is a reverse topological order of G. • So, G can have no cycles. Note: Gives an O(|V|+|E|) algorithm for computing Topological Ordering of an acyclic graph G = (V,E)
  • 49. Strong Components of Directed Graph • Strong Component • Collapsed Graph G* derived by collapsing each strong component into a single vertex. note: G* is acyclic. maximum set vertices S of V such that u,v S cycle containing u,v   
  • 50. Directed Graph with Strong Components Circled Directed Graph G = (V,E) 3 1 2 4 7 5 6 8
  • 51. Algorithm Strong Components Input digraph G [1] Perform DFS on G. Renumber vertices by postorder of the DFS of G. [2] Let G- be the reverse digraph derived from G by reversing direction of each edge.
  • 52. Algorithm Strong Components (cont’d) [3] Perform DFS on G-, starting at highest (in postorder of G) numbered vertex. Output resulting DFS tree of G- as a strongly connected component of G. [4] repeat [3], starting at highest numbered vertex not so for visited (halt when all vertices visited)
  • 53. Time Bounds of Algorithm Strong Components • Time Bounds each DFS costs time c(|V|+|E|) So total time = 2*c(|V|+|E|) ≤ O(|V|+|E|)
  • 54. Example of Step [1] of Strong Components Algorithm 3 1 2 4 7 5 6 8 8 7 6 5 3 4 2 1 Example Input Digraph G (postorder numbering of G given in gold)
  • 55. Example of Step [2] of Strong Components Algorithm 3 1 2 4 7 5 6 8 8 7 6 5 3 4 2 1 Reverse Digraph G- (postorder numbering of G given in gold)
  • 56. Proof of Strong Components Algorithm • Theorem The Algorithm outputs the strong components of G. • Proof We must show these are exactly the vertices in each DFS spanning forest of reverse graph G-
  • 57. Proof of Strong Components Algorithm (continued) • Suppose: • v,w in the same strong component and DFS search in G- starts at vertex r and reaches v. • Then w will also be reached in the DFS search in G- . • So v,w are output together in the same spanning tree of G-.
  • 58. Proof of Strong Components Algorithm (continued) • Suppose: • v,w output in same spanning tree of G-. • Let r be the root of that spanning tree of G-. • Then there exists paths in G- from r to each of v and w. • So there exists paths in G to r from each of v and w.
  • 59. Proof of Strong Components Algorithm (continued) • r is root of spanning tree of G- containing v and w. • Suppose: no path in G to r from v. • Then since r has a higher postorder than v, there is no path in G from v to r, a contradiction. • Hence, there exists path in G from r to v, and similar argument gives path from r to w. • So v and w are in a cycle of G and must be in the same strong component, completing proof !