Introduction To Graph Theory and Algorithms: Jean-Yves L'Excellent and Bora U Car

Download as pdf or txt
Download as pdf or txt
You are on page 1of 124

Outline

Introduction to
graph theory and algorithms
Jean-Yves LExcellent and Bora U car
GRAAL, LIP, ENS Lyon, France
CR-07: Sparse Matrix Computations, September 2010
http://graal.ens-lyon.fr/
~
bucar/CR07/
1/124 CR09
Outline
Outline
1
Denitions and some problems
2
Basic algorithms
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
3
Questions
2/124 CR09
Denitions and some problems
Basic algorithms
Questions
Graph notations and denitions
A graph G = (V, E) consists of a nite set V, called the vertex set and a
nite, binary relation E on V, called the edge set.
Three standard graph models
Undirected graph: The edges are unordered pair of vertices, i.e.,
{u, v} E for some u, v V.
Directed graph: The edges are ordered pair of vertices, that is, (u, v) and
(v, u) are two dierent edges.
Bipartite graph: G = (U V, E) consists of two disjoint vertex sets U
and V such that for each edge (u, v) E, u U and v V.
An ordering or labelling of G = (V, E) having n vertices, i.e., |V| = n, is
a mapping of V onto 1, 2, . . . , n.
3/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matrices and graphs: Rectangular matrices
The rows/columns and nonzeros of a given sparse matrix correspond (with
natural labelling) to the vertices and edges, respectively, of a graph.
Rectangular matrices
A =
0
@
1 2 3 4
1
2
3
1
A
Bipartite graph
1
r
2
r
3
1
c
2
c
3
c
4
c
r
The set of rows corresponds to one of the vertex set R, the set of columns
corresponds to the other vertex set C such that for each a
ij
= 0, (r
i
, c
j
) is an
edge.
4/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matrices and graphs: Square unsymmetric pattern
The rows/columns and nonzeros of a given sparse matrix correspond (with
natural labelling) to the vertices and edges, respectively, of a graph.
Square unsymmetric
pattern matrices
A =
0
@
1 2 3
1
2
3
1
A
Graph models
Bipartite graph as before.
Directed graph
v
2 3
v
1
v
The set of rows/cols corresponds the vertex set V such that for each a
ij
= 0,
(v
i
, v
j
) is an edge. Transposed view possible too, i.e., the edge (v
i
, v
j
) directed
from column i to row j . Usually self-loops are omitted.
5/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matrices and graphs: Square unsymmetric pattern
A special subclass
Directed acyclic graphs (DAG):
A directed graphs with no loops
(maybe except for self-loops).
DAGs
We can sort the vertices such that
if (u, v) is an edge, then u appears
before v in the ordering.
Question: What kind of matrices have a DAG?
6/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matrices and graphs: Symmetric pattern
The rows/columns and nonzeros of a given sparse matrix correspond (with
natural labelling) to the vertices and edges, respectively, of a graph.
Square symmetric
pattern matrices
A =
0
@
1 2 3
1
2
3
1
A
Graph models
Bipartite and directed graphs as before.
Undirected graph
v
2 3
v
1
v
The set of rows/cols corresponds the vertex set V such that for each
a
ij
, a
ji
= 0, {v
i
, v
j
} is an edge. No self-loops; usually the main diagonal is
assumed to be zero-free.
7/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Edges, degrees, and paths
Many denitions for directed and undirected graphs are the same. We
will use (u, v) to refer to an edge of an undirected or directed graph to
avoid repeated denitions.
An edge (u, v) is said to incident on the vertices u and v.
For any vertex u, the set of vertices in adj(u) = {v : (u, v) E} are
called the neighbors of u. The vertices in adj(u) are said to be
adjacent to u.
The degree of a vertex is the number of edges incident on it.
A path p of length k is a sequence of vertices v
0
, v
1
, . . . , v
k
where
(v
i 1
, v
i
) E for i = 1, . . . , k. The two end points v
0
and v
k
are
said to be connected by the path p, and the vertex v
k
is said to be
reachable from v
0
.
8/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Components
An undirected graph is said to be connected if every pair of vertices
is connected by a path.
The connected components of an undirected graph are the
equivalence classes of vertices under the is reachable from
relation.
A directed graph is said to be strongly connected if every pair of
vertices are reachable from each other.
The strongly connected components of a directed graph are the
equivalence classes of vertices under the are mutually reachable
relation.
9/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Trees and spanning trees
A tree is a connected, acyclic, undirected graph. If an undirected graph is
acyclic but disconnected, then it is a forest.
Properties of trees
Any two vertices are connected by a unique path.
|E| = |V| 1
A rooted tree is a tree with a distinguished vertex r , called the root.
There is a unique path from the root r to every other vertex v. Any
vertex y in that path is called an ancestor of v. If y is an ancestor of v,
then v is a descendant of y.
The subtree rooted at v is the tree induced by the descendants of v,
rooted at v.
A spanning tree of a connected graph G = (V, E) is a tree T = (V, F),
such that F E.
10/124 CR09
Denitions and some problems
Basic algorithms
Questions
Ordering of the vertices of a rooted tree
A topological ordering of a rooted tree is an ordering that numbers
children vertices before their parent.
A postorder is a topological ordering which numbers the vertices in
any subtree consecutively.
u w
x
y
z
v
with topological ordering
1
3
2
4
5
6
Rooted spanning tree
w
y z
x u
v
Connected graph G
u w
x
y
z
v
1
6
5 4
3
2
Rooted spanning tree
with postordering
11/124 CR09
Denitions and some problems
Basic algorithms
Questions
Postordering the vertices of a rooted tree I
The following recursive algorithm will do the job:
[porder ]=PostOrder(T, r )
for each child c of r do
porder [porder , PostOrder(T, c)]
porder [porder , r ]
We need to run the algorithm for each root r when T is a forest.
Usually recursive algorithms are avoided, as for a tree with large number
of vertices can cause stack overow.
12/124 CR09
Denitions and some problems
Basic algorithms
Questions
Postordering the vertices of a rooted tree II
[porder ]=PostOrder(T, r )
porder []
seen(v) False for all v T
seen(r ) True
Push(S, r )
while NotEmpty(S) do
v Pop(S)
if a child c of v with seen(c) = False then
seen(c) True
Push(S, c)
else
porder [porder , v]
Again, have to run for each root, if T is a forest.
Both algorithms run in O(n) time for a tree with n nodes.
13/124 CR09
Denitions and some problems
Basic algorithms
Questions
Permutation matrices
A permutation matrix is a square (0, 1)-matrix where each row and
column has a single 1.
If P is a permutation matrix, PP
T
= I , i.e., it is an orthogonal matrix.
Let,
A =

1 2 3
1
2
3

and suppose we want to permute columns as [2, 1, 3]. Dene p


2,1
= 1,
p
1,2
= 1, p
3,3
= 1, and B = AP (if column j to be at position i , set
p
ji
= 1)
B =

2 1 3
1
2
3

1 2 3
1
2
3

1 2 3
1 1
2 1
3 1

14/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matching in bipartite graphs and permutations
A matching in a graph is a set of edges no two of which share a common
vertex. We will be mostly dealing with matchings in bipartite graphs.
In matrix terms, a matching in the bipartite graph of a matrix
corresponds to a set of nonzero entries no two of which are in the same
row or column.
A vertex is said to be matched if there is an edge in the matching
incident on the vertex, and to be unmatched otherwise. In a perfect
matching, all vertices are matched.
The cardinality of a matching is the number of edges in it. A maximum
cardinality matching or a maximum matching is a matching of maximum
cardinality. Solvable in polynomial time.
15/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matching in bipartite graphs and permutations
Given a square matrix whose bipartite graph has a perfect matching, such
a matching can be used to permute the matrix such that the matching
entries are along the main diagonal.
1
r
2
r
3 3
c
2
c
1
c r

1 2 3
1
2
3

2 1 3
1
2
3

1 2 3
1
2
3

1 2 3
1 1
2 1
3 1

16/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Reducibility
Reducible matrix: An n n square matrix is reducible if there exists an
n n permutation matrix P such that
PAP
T
=

A
11
A
12
O A
22

,
where A
11
is an r r submatrix, A
22
is an (n r ) (n r ) submatrix,
where 1 r < n.
Irreducible matrix: There is no such a permutation matrix.
Theorem: An n n square matrix is irreducible i its directed graph is
strongly connected.
Proof: Follows by denition.
17/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Fully indecomposability
Fully indecomposable matrix: There is no permutation matrices P and Q
such that
PAQ =

A
11
A
12
O A
22

,
with the same condition on the blocks and their sizes as above.
Theorem: An n n square matrix A is fully indecomposable i for some
permutation matrix P, the matrix PA is irreducible and has a zero-free
main diagonal.
Proof: We will come later in the semester to the if part.
Only if part (by contradiction): Let B = PA be an irreducible matrix with
zero-free main diagonal. B is fully indecomposable i A is (why?).
Therefore we may assume that A is irreducible and has a zero-free
diagonal. Suppose, for the sake of contradiction, A is not fully
indecomposable.
18/124 CR09
Denitions and some problems
Basic algorithms
Questions
Fully indecomposable matrices
Fully indecomposable matrix
There is no permutation matrices P and Q such that
PAQ =

A
11
A
12
O A
22

,
with the same condition on the blocks and their sizes as above.
Proof cont.: Let P
1
AQ
1
be of the form above with A
11
of size r r . We
may write P
1
AQ
1
= A

, where A

= P
1
AP
1
T
with zero-free diagonal
(why?), and Q

= P
1
Q
1
is a permutation matrix which has to permute
(why?) the rst r columns among themselves, and similarly the last n r
columns among themselves. Hence, A

is in the above form, and A is


reducible: contradiction. 2
19/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Cliques and independent sets
Clique
In an undirected graph G = (V, E), a set of vertices S V is a clique if for all
s, t S, we have (s, t) E.
Maximum clique: A clique of maximum cardinality (nding a maximum clique
in an undirected graph is NP-complete).
Maximal clique: A clique is a maximal clique, if it is not contained in another
clique.
In a symmetric matrix A, a clique corresponds to a subset of rows R and the
corresponding columns such that the matrix A(R, R) is full.
Independent set
A set of vertices is an independent set if none of the vertices are adjacent to
each other. Can we nd the largest one in polynomial time?
In a symmetric matrix A, an independent set corresponds to a subset of rows R
and the corresponding columns such that the matrix A(R, R) is either zero, or
diagonal.
20/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: More on cliques
Clique: In an undirected graph G = (V, E), a set of vertices S V is a
clique if for all s, t S, we have (s, t) E.
In a symmetric matrix A corresponds to a subset of rows R and the
corresponding columns such that the matrix A(R, R) is full.
Cliques in bipartite graphs: Bi-cliques
In a bipartite graph G = (U V, E), a pair of sets R, C where R U
and C V is a bi-clique if for all a R and b C, we have (a, b) E.
In a matrix A, corresponds to a subset of rows R and a subset of columns
C such that the matrix A(R, C) is full.
The maximum node bi-clique problem asks for a bi-clique of maximum
size (e.g., |R| +|C|), and it is polynomial time solvable, whereas
maximum edge bi-clique problem (e.g., asks for a maximum |R| |C|) is
NP-complete.
21/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Hypergraphs
Hypergraph: A hypergraph H = (V, N) consists of a nite set V called the
vertex set and a set of non-empty subsets of vertices N called the hyperedge
set or the net set. A generalization of graphs.
For a matrix A, dene a hypergraph whose vertices correspond to the rows and
whose nets correspond to the columns such that vertex v
i
is in net n
j
i a
ij
= 0
(the column-net model).
A sample matrix
0
@
1 2 3 4
1
2
3
1
A
The column-net hypergraph model
4
3
n
1
n
1
v
2
v
3
v
n
n
2
v
2
n
4
n
3
n
1
n
1
2
3
v
v
22/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Basic graph algorithms
Searching a graph: Systematically following the edges of the graph so as
to visit all the vertices.
Breadth-rst search,
Depth-rst search.
Topological sort (of a directed acyclic graph): It is a linear ordering of all
the vertices such that if (u, v) directed is an edge, then u appears before
v in the ordering.
Strongly connected components (of a directed graph; why?): Recall that
a strongly connected component is a maximal set of vertices for which
every pair its vertices are reachable. We want to nd them all.
We will use some of the course notes by Cevdet Aykanat
(http://www.cs.bilkent.edu.tr/
~
aykanat/teaching.html)
23/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Idea
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
2
Graph Searching: Breadth-First Search
Graph G =(V, E), directed or undirected with adjacency list repres.
GOAL: Systematically explores edges of G to
discover every vertex reachable from the source vertex s
compute the shortest path distance of every vertex
from the source vertex s
produce a breadth-first tree (BFT) G
!
with root s
" BFT contains all vertices reachable from s
" the unique path from any vertex v to s in G
!
constitutes a shortest path from s to v in G
IDEA: Expanding frontier across the breadth -greedy-
propagate a wave 1 edge-distance at a time
using a FIFO queue: O(1) time to update pointers to both ends
24/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Key components
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
3
Breadth-First Search Algorithm
Maintains the following fields for each u ! V
color[u]: color of u
" WHITE : not discovered yet
" GRAY : discovered and to be or being processed
" BLACK: discovered and processed
#[u]: parent of u (NIL of u = s or u is not discovered yet)
d[u]: distance of u from s
Processing a vertex = scanning its adjacency list
25/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Algorithm
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
4
Breadth-First Search Algorithm
BFS(G, s)
for each u ! V" {s} do
color[u] #WHITE
$[u] #NIL; d [u] #%
color[s] #GRAY
$[s] #NIL; d [s] #0
Q #{s}
while Q & ' do
u #head[Q]
for each v in Adj[u] do
if color[v] = WHITE then
color[v] #GRAY
$[v] #u
d [v] #d [u] + 1
ENQUEUE(Q, v)
DEQUEUE(Q)
color[u] #BLACK
26/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Example
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
5
Breadth-First Search
Sample Graph:

a
b
g
c
f
d
e
h
i
0
s
FIFO just after
queue Q processing vertex
!a" -
27/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Example
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
6
Breadth-First Search

a
b
g
c
f
d
e
h
i
0
1
1
s
FIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
28/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Example
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
7
Breadth-First Search

a
b
g
c
f
d
e
h
i
0
1
1
2
s
FIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
!a,b,c,f" b
29/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Example
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
8
Breadth-First Search

a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
FIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
!a,b,c,f" b
!a,b,c,f,e" c
30/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Example
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
9
Breadth-First Search

a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
3 3
FIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
!a,b,c,f" b
!a,b,c,f,e" c
!a,b,c,f,e,g,h" f
31/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Example
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
10
Breadth-First Search

a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
3 3
3
3
FIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
!a,b,c,f" b
!a,b,c,f,e" c
!a,b,c,f,e,g,h" f
!a,b,c,f,e,g,h,d,i" e
all distances are filled in after processing e
32/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Example
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
11
Breadth-First Search

a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
3 3
3
3
FIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
!a,b,c,f" b
!a,b,c,f,e" c
!a,b,c,f,e,g,h" f
!a,b,c,f,e,g,h,d,i" g
33/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Example
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
12
Breadth-First Search

a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
3 3
3
3
FIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
!a,b,c,f" b
!a,b,c,f,e" c
!a,b,c,f,e,g,h" f
!a,b,c,f,e,g,h,d,i" h
34/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Example
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
13
Breadth-First Search

a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
3 3
3
3
FIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
!a,b,c,f" b
!a,b,c,f,e" c
!a,b,c,f,e,g,h" f
!a,b,c,f,e,g,h,d,i" d
35/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Example
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
14
Breadth-First Search

a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
3 3
3
3
FIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
!a,b,c,f" b
!a,b,c,f,e" c
!a,b,c,f,e,g,h" f
!a,b,c,f,e,g,h,d,i" i
algorithm terminates: all vertices are processed
36/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: Analysis
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
15
Breadth-First Search Algorithm
Running time: O(V+E) = considered linear time in graphs
initialization: !(V)
queue operations: O(V)
" each vertex enqueued and dequeued at most once
" both enqueue and dequeue operations take O(1) time
processing gray vertices: O(E)
" each vertex is processed at most once and
!
#
! =
V u
E u Adj ) ( | ] [ |
37/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: The paths to the root
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
25
Breadth-First Tree Generated by BFS
LEMMA 4: predecessor subgraph G
!
=(V
!
, E
!
) generated by
BFS(G, s) , where V
!
={v " V: ![v] # NIL}${s} and
E
!
={(![v],v) " E: v " V
!
%{s}}
is a breadth-first tree such that
% V
!
consists of all vertices in V that are reachable from s
% &v " V
!
, unique path p(v, s) in G
!
constitutes a sp(s, v) in G
PRINT-PATH(G, s, v)
if v = s then print s
else if ![v] = NIL then
print no s!v path
else
PRINT-PATH(G, s, ![v] )
print v
Prints out vertices on a
s!v shortest path
38/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Breadth-rst search: The BFS tree
CS473 Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
26
Breadth-First Tree Generated by BFS

a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
3 3
3
3

a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
3 3
3
3
BFS(G,a) terminated BFT generated by BFS(G,a)
39/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Idea
CS 473 Lecture 14 2
Depth-First Search
Graph G!(V,E) directed or undirected
Adjacency list representation
Goal: Systematically explore every vertex and
every edge
Idea: search deeper whenever possible
Using a LIFO queue (Stack; FIFO queue used in BFS)
40/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Key components
CS 473 Lecture 14 3
Depth-First Search
Maintains several Iields Ior each !!V
Like BFS, colors the vertices to indicate their
states. Each vertex is
Initially white,
grayed when discovered,
blackened when Iinished
Like BFS, records discovery oI a white ! during
scanning Adj|"| by "|!|#"
41/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Key components
CS 473 Lecture 14 4
Depth-First Search
Unlike BFS, predecessor graph G
!
produced by
DFS Iorms spanning Iorest
G
!
"(V,E
!
) where
E
!
"(!|!|,!): !#V and !|!|$ NIL}
G
!
" depth-Iirst Iorest (DFF) is composed oI
disjoint depth-Iirst trees (DFTs)
42/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Key components
CS 473 Lecture 14 5
Depth-First Search
DFS also timestamps each vertex with two timestamps
d|!|: records when ! is Iirst discovered and grayed
I|!|: records when ! is Iinished and blackened
Since there is only one discovery event and Iinishing
event Ior each vertex we have 1! d|!| " I|!|! 2,V,
1 2
d|!| I|!| 2,V,
Time axis Ior the color oI a vertex
43/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Algorithm
CS 473 Lecture 14 6
Depth-First Search
!"#(G)
$%&'()*+ !!V ,%
color|!|" "#$%&
#|!| " NIL
%$'& " 0
$%&'()*+ !!V ,%
-$'color|!| $ "#$%& .+(/
!"#012#234G5'!6
!"#012#234G5'!6
color|!|" ()*+
d|!|" %$'& " %$'& %1
$%&'()*+ ,! Adj|!| ,%
-$'color|,| $ "#$%& .+(/
#|,| " !
!"#012#234G5',6
color|!|" -.*/0
I|!|" %$'& " %$'& %1
44/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Analysis
CS 473 Lecture 14 7
Depth-First Search
Running time: !(V"E)
Initialization loop in !"# : !(V)
Main loop in !"#: !(V) exclusive oI time to execute
calls to !"#$%&#&'(
!"#$%&#&'(is called exactly once Ior each !#V since
!"#$%&#&'(is invoked only on white vertices and
!"#$%&#&')G*("+ immediately colors u as gray
For loop oI !"#$%&#&')G*("+ is executed ,Adj|"|, time
Since $ ,Adj|"|, % E, total cost oI executing loop oI
!"#$%&#&'(is !(E)
45/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 8
Depth-First Search: Example
! " #
$
%
& '
(
46/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 9
Depth-First Search: Example
! " #
$
%
& '
(
!
47/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 10
Depth-First Search: Example
! " #
$
%
& '
(
!
"
48/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 11
Depth-First Search: Example
! " #
$
%
& '
(
!
"
#
49/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 12
Depth-First Search: Example
! " #
$
%
& '
(
!
"
#
50/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 13
Depth-First Search: Example
! " #
$
%
& '
(
!
"
# $
51/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 14
Depth-First Search: Example
! " #
$
%
& '
(
!
"
# $ %
52/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 15
Depth-First Search: Example
! " #
$
%
& '
(
!
"
# $ %
53/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 16
Depth-First Search: Example
! " #
$
%
& '
(
!
"
# $ % &
54/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 17
Depth-First Search: Example
! " #
$
%
& '
(
!
"
# $ % &
'
55/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 18
Depth-First Search: Example
! " #
$
%
& '
(
!
"
# $ % &
'
(
56/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 19
Depth-First Search: Example
! " #
$
%
& '
(
!
"
# $ % &
'
(
57/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 20
Depth-First Search: Example
! " #
$
%
& '
(
1
2
3 4 5 6
7
8
9
58/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 21
Depth-First Search: Example
! " #
$
%
& '
(
1
2
3 4 5 6
7
8
9
59/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 22
Depth-First Search: Example
! " #
$
%
& '
(
1
2
3 4 5 6
7
8
9 10
60/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 23
Depth-First Search: Example
! " #
$
%
& '
(
1
2
3 4 5 6
7 9 10
8 11
61/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 24
Depth-First Search: Example
! " #
$
%
& '
(
2
3 4 5 6
7 9 10
8 11 1 12
62/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 25
Depth-First Search: Example
! " #
$
%
& '
(
2
3 4 5 6
7 9 10
8 11 1 12 13
63/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 26
Depth-First Search: Example
! " #
$
%
& '
(
2
3 4 5 6
7 9 10
8 11 1 12 13
64/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 27
Depth-First Search: Example
! " #
$
%
& '
(
2
3 4 5 6
7 9 10
8 11 1 12 13
65/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 28
Depth-First Search: Example
! " #
$
%
& '
(
2
3 4 5 6
7 9 10
8 11 1 12 13
14
66/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 29
Depth-First Search: Example
! " #
$
%
& '
(
2
3 4 5 6
7 9 10
8 11 1 12 13
14
67/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 30
Depth-First Search: Example
! " #
$
%
& '
(
2
3 4 5 6
7 9 10
8 11 1 12 13
14 15
68/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Example
CS 473 Lecture 14 31
Depth-First Search: Example
! " #
$
%
& '
(
2
3 4 5 6
7 9 10
8 11 1 12
14 15
13 16
69/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: DFT and DFF
CS 473 Lecture 14 32
Depth-First Search: Example
! " #
$
%
& '
(
2
3 4 5 6
7 9 10
8 11 1 12
14 15
13 16
! " #
$
%
& '
(
2
3 4 5 6
7 9 10
8 11 1 12
14 15
13 16
DFS(G) terminated Depth-Iirst Iorest (DFF)
70/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Parenthesis theorem
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
2
DFS: Parenthesis Theorem
Thm: In any DFS of G=(V,E), let int[v] = [d[v], f[v]]
then exactly one of the following holds
for any u and v !V
int[u] and int[v] are entirely disjoint
int[v] is entirely contained in int[u] and
v is a descendant of u in a DFT
int[u] is entirely contained in int[v] and
u is a descendant of v in a DFT
71/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Parenthesis theorem
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
5
Parenthesis
Theorem
x y z
s
t
w v
u
2
3 4 5 6
7 9 1 0
8 1 1 1 1 2
1 4 1 5
1 3 1 6
1 2 3 4 5 6 7 8 9
x z
s y u
w v t
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6
1 0 1 1 1 2 1 3 1 4 1 5 1 6
( x ( s ( w w ) ( v v ) s ) ( y ( t t ) y ) x ) ( z ( u u ) z )
72/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
6
Edge Classification in a DFF
Tree Edge: discover a new (WHITE) vertex
!GRAY to WHITE"
Back Edge: from a descendent to an ancestor in DFT
!GRAY to GRAY"
Forward Edge: from ancestor to descendent in DFT
!GRAY to BLACK"
Cross Edge: remaining edges (btwn trees and subtrees)
!GRAY to BLACK"
Note: ancestor/descendent is wrt Tree Edges
73/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
7
Edge Classification in a DFF
How to decide which GRAY to BLACK edges
are forward, which are cross
Let BLACK vertex v !Adj[u] is encountered
while processing GRAY vertex u
(u,v) is a forward edge if d[u] < << < d[v]
(u,v) is a cross edge if d[u] > >> > d[v]
74/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
8
Depth-First Search: Example
x y z
s
t
w v
u
1
75/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
9
Depth-First Search: Example
x y z
s
t
w v
u
1
2
T
76/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
10
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3
T
T
77/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
11
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3
T
T
B
78/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
12
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4
T
T
B
79/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
13
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4 5
T
T
T
B
80/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
14
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4 5
T
T
T
B
C
81/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
15
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4 5 6
T
T
T
B
C
82/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
16
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4 5 6
7
T
T
T
B
C
83/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
17
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4 5 6
7
8
T
T
T
T
B
C
84/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
18
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4 5 6
7
8
T
T
T
T
B
C
C
85/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
19
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4 5 6
7
8
9
T
T
T
T
T
B
C
C
86/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
20
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4 5 6
7
8
9
T
T
T
T
T
C
B
C
C
87/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
21
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4 5 6
7
8
9 10
T
T
T
T
T
B
C
C
C
88/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
22
Depth-First Search: Example
x y z
s
t
w v
u
1
2
3 4 5 6
7 9 10
8 11
T
T
T
T
T
B
C
C
C
F
89/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
23
Depth-First Search: Example
x y z
s
t
w v
u
2
3 4 5 6
7 9 10
8 11 1 12
T
T
T
T
T
B
F
C
C
C
90/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
24
Depth-First Search: Example
x y z
s
t
w v
u
2
3 4 5 6
7 9 10
8 11 1 12 13
T
T
T
T
T
B
C
F
C
C
91/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
25
Depth-First Search: Example
x y z
s
t
w v
u
2
3 4 5 6
7 9 10
8 11 1 12 13
T
T
T
T
T
C
B
F
C
C
C
92/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
26
Depth-First Search: Example
x y z
s
t
w v
u
2
3 4 5 6
7 9 10
8 11 1 12 13
T
T
T
T
T
B
C
F
C
C
C
C
93/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
27
Depth-First Search: Example
x y z
s
t
w v
u
2
3 4 5 6
7 9 10
8 11 1 12 13
14
T
T
T
T
T
B
F
C
C
C
C
C
T
94/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
28
Depth-First Search: Example
x y z
s
t
w v
u
2
3 4 5 6
7 9 10
8 11 1 12 13
14
C
T
T
T
T
T
T
B
F
C
C
C
C
C
95/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Edge classication example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
29
Depth-First Search: Example
x y z
s
t
w v
u
2
3 4 5 6
7 9 10
8 11 1 12
14 15
13 16
T
T
T
T
T
T
C
F
B
C
C
C
C
C
96/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Undirected graphs
Edge classication
Any DFS on an undirected graph produces only Tree and Back edges.
16/17
a c g
h f
d j
i e b
1/20
2/19
3/12
5/8 6/7
9/10
4/11
13/14
15/18
i
j
h
g
c f
e d
a
b
97/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Depth-rst search: Non-recursive algorithm
[, d, f ]=DFS(G, v)
top 1
stack(top) v
d(v) ctime 1
while top > 0 do
u stack(top)
if there is a vertex w Adj (u) where (w) is not set then
top top + 1
stack(top) w
(w) u
d(w) ctime ctime + 1
else
f (u) ctime ctime + 1
top top 1
98/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Topological sort
Topological sort (of a directed acyclic graph): It is a linear ordering of all
the vertices such that if (u, v) is a directed edge, then u appears before v
in the ordering.
Ordering is not necessarily unique.
99/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Topological sort: Example
3/4
short
socks watch
pants shoes shirt
tie belt jacket
pants
under
short
socks shoes watch shirt belt tie jacket
6/7 3/4
11/16 17/18 9/10
13/14
2/5
1/8 12/15
17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5
under
100/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Topological sort: Algorithm
The algorithm
run DFS(G)
when a vertex is nished, output it
vertices are output in the reverse topologically sorted order
Runs in O(V + E) time a linear time algorithm.
The algorithm: Correctness
if (u, v) E, then f [u] > f [v]
Proof: Consider the color of v during exploring the edge (u, v), where u
is Gray. 2
v cannot be Gray (otherwise a Back edge in an acyclic graph !!!).
If v is White, then u is an ancestor of v, hence f [u] > f [v].
If v is Black, f [v] is computed already, f [u] is going to be computed,
hence f [u] > f [v].
101/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components (SCC)
The strongly connected components of a directed graph are the
equivalence classes of vertices under the are mutually reachable
relation.
For a graph G = (V, E), the transpose is dened as G
T
= (V, E
T
),
where E
T
= {(u, v) : (v, u) E}.
Constructing G
T
from G takes O(V + E) time with adjacency list (like
the CSR or CSC storage format for sparse matrices) representation.
Notice that G and G
T
have the same SCCs.
102/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Algorithm
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
3
Strongly Connected Components
Algorithm
(1) Run DFS(G) to compute finishing times for all u!V
(2) Compute G
T
(3) Call DFS(G
T
) processing vertices in main loop in
decreasing f[u] computed in Step (1)
(4) Output vertices of each DFT in DFF of Step (3) as a
separate SCC
103/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Analysis
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
4
Strongly Connected Components
Lemma 1: no path between a pair of vertices in
the same SCC, ever leaves the SCC
Proof: let u and v be in the same SCC !u !v
let w be on some path u !w !v ! u !w
but v !u !! a path w !v !u ! w !u
therefore u and w are in the same SCC
QED
u
v
w
SCC
104/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
5
SCC: Example
a b c
e
d
f
g
h
105/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
6
SCC: Example
a b c
e
d
f
g
h
1
1
(1) Run DFS(G) to compute finishing times for all u!V
106/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
7
SCC: Example
a b c
e
d
f
g
h
1
1
10
2 7 3 4
5 6
8 9
(1) Run DFS(G) to compute finishing times for all u!V
107/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
8
SCC: Example
a b c
e
d
f
g
h
1
2
10
2 7 3 4
5 6
8 9 11
(1) Run DFS(G) to compute finishing times for all u!V
108/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
9
SCC: Example
a b c
e
d
f
g
h
1
2
10
2 7 3 4
5 6
8 9 16 11 14 13
15 12
1
Vertices sorted according to the finishing times:
! !! !b, e, a, c, d, g, h, f " "" "
109/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
10
SCC: Example
a b c
e
d
f
g
h
(2) Compute G
T
110/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
11
SCC: Example
a b c
e
d
f
g
h
(3) Call DFS(G
T
) processing vertices in main loop in
decreasing f[u] order: ! !! !b, e, a, c, d, g, h, f " "" "
111/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
12
SCC: Example
a b c
e
d
f
g
h
r
1
=
(3) Call DFS(G
T
) processing vertices in main loop in
decreasing f[u] order: ! !! !b, e, a, c, d, g, h, f " "" "
112/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
13
SCC: Example
a b c
e
d
f
g
h
r
1
=
(3) Call DFS(G
T
) processing vertices in main loop in
decreasing f[u] order: ! !! !b, e, a, c, d, g, h, f " "" "
113/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
14
SCC: Example
a b c
e
d
f
g
h
r
1
= r
2
=
(3) Call DFS(G
T
) processing vertices in main loop in
decreasing f[u] order: ! !! !b, e, a, c, d, g, h, f " "" "
114/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
15
SCC: Example
a b c
e
d
f
g
h
r
1
= r
2
=
(3) Call DFS(G
T
) processing vertices in main loop in
decreasing f[u] order: ! !! !b, e, a, c, d, g, h, f " "" "
115/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
16
SCC: Example
a b c
e
d
f
g
h
r
1
= r
2
=
r
3
=
(3) Call DFS(G
T
) processing vertices in main loop in
decreasing f[u] order: ! !! !b, e, a, c, d, g, h, f " "" "
116/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
17
SCC: Example
a b c
e
d
f
g
h
r
1
= r
2
=
r
3
=
(3) Call DFS(G
T
) processing vertices in main loop in
decreasing f[u] order: ! !! !b, e, a, c, d, g, h, f " "" "
117/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
18
SCC: Example
a b c
e
d
f
g
h
r
1
= r
2
=
r
3
= r
4
=
(3) Call DFS(G
T
) processing vertices in main loop in
decreasing f[u] order: ! !! !b, e, a, c, d, g, h, f " "" "
118/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
19
SCC: Example
a b c
e
d
f
g
h
r
1
= r
2
=
r
3
= r
4
=
(4) Output vertices of each DFT in DFF as a separate SCC
C
b
={b,a,e}
C
g
={g,f} C
h
={h}
C
c
={c,d}
119/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Example
CS473 Lecture 16 Cevdet Aykanat - Bilkent University
Computer Engineering Department
20
SCC: Example
a b c
e
d
f
g
h
h f,g
c,d
a,b,e
Acyclic component
graph
C
b
C
g
C
c
C
h
120/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
Strongly connected components: Observations
In any DFS(G), all vertices in the same SCC are placed in the same
DFT.
In the DFS(G) step of the algorithm, the last vertex nished in an
SCC is the rst vertex discovered in the SCC.
Consider the vertex r with the largest nishing time. It is a root of a
DFT. Any vertex that is reachable from r in G
T
should be in the
SCC of r (why?)
121/124 CR09
Denitions and some problems
Basic algorithms
Questions
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
SCC and reducibility
To detect if there exists a permutation matrix P such that
PAP
T
=

A
11
A
12
O A
22

,
where A
11
is an r r submatrix, A
22
is an (n r ) (n r ) submatrix,
where 1 r < n:
run SCC on the directed graph of A to identify each strongly connected
component as an irreducible block (more than one SCC?). Hence A
11
,
too, can be in that form (how many SCCs?).
122/124 CR09
Denitions and some problems
Basic algorithms
Questions
Could not get enough of it: Questions
How would you describe the following in the language of graphs
the structure of PAP
T
for a given square sparse matrix A and a
permutation matrix P,
the structure of PAQ for a given square sparse matrix A and two
permutation matrices P and Q,
the structure of A
k
, for k > 1,
the structure of AA
T
,
the structure of the vector b, where b = Ax for a given sparse
matrix A, and a sparse vector x.
123/124 CR09
Denitions and some problems
Basic algorithms
Questions
Could not get enough of it: Questions
Can you dene:
the row-net hypergraph model of a matrix.
a matching in a hypergraph (is it a hard problem?).
Can you relate:
the DFS or BFS on a tree to a topological ordering? postordering?
Find an algorithm
how do you transpose a matrix in CSR or CSC format?
124/124 CR09

You might also like