Lecture Graphs | Graph Theory | Matrix (Mathematics)

# Outline

Introduction to
graph theory and algorithms
Jean-Yves L’Excellent 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
Deﬁnitions and some problems
2
Basic algorithms
Depth-ﬁrst search
Topological sort
Strongly connected components
3
Questions
2/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Graph notations and deﬁnitions
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 diﬀerent 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
Deﬁnitions 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
Deﬁnitions 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
Deﬁnitions 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
Deﬁnitions 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
Deﬁnitions and some problems
Basic algorithms
Questions
Deﬁnitions: Edges, degrees, and paths
Many deﬁnitions 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 deﬁnitions.
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
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
Deﬁnitions and some problems
Basic algorithms
Questions
Deﬁnitions: 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
Deﬁnitions and some problems
Basic algorithms
Questions
Deﬁnitions: 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
Deﬁnitions 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
Deﬁnitions 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 overﬂow.
12/124 CR09
Deﬁnitions 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
Deﬁnitions 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]. Deﬁne 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
Deﬁnitions 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
Deﬁnitions 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
Deﬁnitions and some problems
Basic algorithms
Questions
Deﬁnitions: 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 deﬁnition.
17/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Deﬁnitions: 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
Deﬁnitions 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

Q

, 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
19/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Deﬁnitions: 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
Deﬁnitions and some problems
Basic algorithms
Questions
Deﬁnitions: 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
Deﬁnitions and some problems
Basic algorithms
Questions
Deﬁnitions: 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, deﬁne 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
Deﬁnitions and some problems
Basic algorithms
Questions
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.
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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
2
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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
3
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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
4
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
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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
5
Sample Graph:

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

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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
7

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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
8

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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
9

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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
10

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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
11

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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
12

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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
13

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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
14

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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
15
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University
Computer Engineering Department
26

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
Deﬁnitions and some problems
Basic algorithms
Questions
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
· 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
Deﬁnitions and some problems
Basic algorithms
Questions
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
41/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
-\$'color|,| \$ "#\$%& .+(/
#|,| " !
!"#012#234G5',6
color|!|" -.*/0
I|!|" %\$'& " %\$'& %1
44/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 8
Depth-First Search: Example
! " #
\$
%
& '
(
46/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 9
Depth-First Search: Example
! " #
\$
%
& '
(
!
47/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 10
Depth-First Search: Example
! " #
\$
%
& '
(
!
"
48/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 11
Depth-First Search: Example
! " #
\$
%
& '
(
!
"
#
49/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 12
Depth-First Search: Example
! " #
\$
%
& '
(
!
"
#
50/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 13
Depth-First Search: Example
! " #
\$
%
& '
(
!
"
# \$
51/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 14
Depth-First Search: Example
! " #
\$
%
& '
(
!
"
# \$ %
52/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 15
Depth-First Search: Example
! " #
\$
%
& '
(
!
"
# \$ %
53/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 16
Depth-First Search: Example
! " #
\$
%
& '
(
!
"
# \$ % &
54/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 17
Depth-First Search: Example
! " #
\$
%
& '
(
!
"
# \$ % &
'
55/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 18
Depth-First Search: Example
! " #
\$
%
& '
(
!
"
# \$ % &
'
(
56/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Example
CS 473 Lecture 14 19
Depth-First Search: Example
! " #
\$
%
& '
(
!
"
# \$ % &
'
(
57/124 CR09
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation
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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation
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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Edge classiﬁcation 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
Deﬁnitions and some problems
Basic algorithms
Questions
Depth-ﬁrst search
Topological sort
Strongly connected components
Depth-ﬁrst search: Undirected graphs
Edge classiﬁcation
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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 deﬁned 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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions and some problems
Basic algorithms
Questions
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
Deﬁnitions 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
Deﬁnitions and some problems
Basic algorithms
Questions
Could not get enough of it: Questions
Can you deﬁne:
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

Outline

Outline

1

Deﬁnitions and some problems Basic algorithms Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Questions

2

3

2/124

CR09

Deﬁnitions and some problems Basic algorithms Questions

Graph notations and deﬁnitions
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 diﬀerent 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

respectively. 4/124 CR09 . the set of columns corresponds to the other vertex set C such that for each aij = 0. Rectangular matrices 1 1 × A = 2 @× 3 0 2 3 × × 4 1 ×A × r2 c3 r3 c4 Bipartite graph c1 r1 c2 The set of rows corresponds to one of the vertex set R. of a graph. (ri . cj ) is an edge.Deﬁnitions 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.

vj ) is an edge. respectively.e.. Usually self-loops are omitted. Directed graph v1 v2 v3 The set of rows/cols corresponds the vertex set V such that for each aij = 0. of a graph. the edge (vi . i. 5/124 CR09 . Square unsymmetric pattern matrices 1 1 × A = 2 @× 3 0 2 × 3 1 ×A × Graph models Bipartite graph as before. Transposed view possible too. vj ) directed from column i to row j. (vi .Deﬁnitions 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.

Question: What kind of matrices have a DAG? 6/124 CR09 . then u appears before v in the ordering. DAGs We can sort the vertices such that if (u.Deﬁnitions 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). v ) is an edge.

aji = 0. usually the main diagonal is assumed to be zero-free. No self-loops. {vi . Square symmetric pattern matrices 1 1 A = 2 @× 3 0 2 × × × 3 1 ×A × Graph models Bipartite and directed graphs as before.Deﬁnitions 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. Undirected graph v1 v2 v3 The set of rows/cols corresponds the vertex set V such that for each aij . respectively. of a graph. vj } is an edge. 7/124 CR09 .

. For any vertex u. . k. We will use (u. v ) ∈ E } are called the neighbors of u. 8/124 CR09 . vk where (vi−1 . v ) is said to incident on the vertices u and v . . A path p of length k is a sequence of vertices v0 . and paths Many deﬁnitions for directed and undirected graphs are the same. . degrees. v ) to refer to an edge of an undirected or directed graph to avoid repeated deﬁnitions. and the vertex vk is said to be reachable from v0 . The degree of a vertex is the number of edges incident on it. the set of vertices in adj(u) = {v : (u. . . v1 . vi ) ∈ E for i = 1. The vertices in adj(u) are said to be adjacent to u. The two end points v0 and vk are said to be connected by the path p. .Deﬁnitions and some problems Basic algorithms Questions Deﬁnitions: Edges. . An edge (u.

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.Deﬁnitions and some problems Basic algorithms Questions Deﬁnitions: Components An undirected graph is said to be connected if every pair of vertices is connected by a path. 9/124 CR09 .

A spanning tree of a connected graph G = (V . acyclic. Properties of trees Any two vertices are connected by a unique path. then it is a forest. If y is an ancestor of v . such that F ⊆ E . undirected graph. There is a unique path from the root r to every other vertex v . rooted at v . 10/124 CR09 . E ) is a tree T = (V . If an undirected graph is acyclic but disconnected. Any vertex y in that path is called an ancestor of v .Deﬁnitions and some problems Basic algorithms Questions Deﬁnitions: Trees and spanning trees A tree is a connected. The subtree rooted at v is the tree induced by the descendants of v . |E | = |V | − 1 A rooted tree is a tree with a distinguished vertex r . called the root. F ). then v is a descendant of y .

v z u y x w Connected graph G z 5 y 4 x 1 6 2 3 z v 4 y 3 x 6 5 v u w 1 u w 2 Rooted spanning tree with topological ordering Rooted spanning tree with postordering 11/124 CR09 . A postorder is a topological ordering which numbers the vertices in any subtree consecutively.Deﬁnitions 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.

PostOrder(T . r ) for each child c of r do porder ← [porder .Deﬁnitions 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 . Usually recursive algorithms are avoided. 12/124 CR09 . as for a tree with large number of vertices can cause stack overﬂow. r ] We need to run the algorithm for each root r when T is a forest. c)] porder ← [porder .

if T is a forest. Both algorithms run in O(n) time for a tree with n nodes. r ) porder ← [·] seen(v ) ← False for all v ∈ T seen(r ) ← True Push(S. c) else porder ← [porder . 13/124 CR09 . r ) while NotEmpty(S) do v ←Pop(S) if ∃ a child c of v with seen(c) = False then seen(c) ← True Push(S. have to run for each root. v ] Again.Deﬁnitions and some problems Basic algorithms Questions Postordering the vertices of a rooted tree – II [porder ]=PostOrder(T .

If P is a permutation matrix. Let. i. 3].e. p1.Deﬁnitions and some problems Basic algorithms Questions Permutation matrices A permutation matrix is a square (0. 1.3 = 1.1 = 1.2 = 1. p3. 1 2 3   1 × × × A = 2× 3 × and suppose we want to permute columns as [2. 1)-matrix where each row and column has a single 1.. it is an orthogonal matrix. PP T = I . Deﬁne p2. set pji = 1) 2 1 3 1    1 × × 1 × × × = 2× B = 2 3 × 3 14/124 CR09 2 × 1 2 3    1 1  × 21 × 3 1 3 . and B = AP (if column j to be at position i.

and to be unmatched otherwise. all vertices are matched.Deﬁnitions 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. Solvable in polynomial time. We will be mostly dealing with matchings in bipartite graphs. The cardinality of a matching is the number of edges in it. In a perfect matching. 15/124 CR09 . 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. A maximum cardinality matching or a maximum matching is a matching of maximum cardinality. In matrix terms.

r1 r2 r3 c1 c2 c3 1 1 × 2× 3  2 × 3  × × 2  1 × 2 3 1 3 1   × 1 × × × = 2× × 3 16/124 2 × 1 2 3    1 1  × 21 × 3 1 3 CR09 . such a matching can be used to permute the matrix such that the matching entries are along the main diagonal.Deﬁnitions and some problems Basic algorithms Questions Matching in bipartite graphs and permutations Given a square matrix whose bipartite graph has a perfect matching.

where 1 ≤ r < n. where A11 is an r × r submatrix. Irreducible matrix: There is no such a permutation matrix. Theorem: An n × n square matrix is irreducible iﬀ its directed graph is strongly connected. A22 is an (n − r ) × (n − r ) submatrix. Proof: Follows by deﬁnition. 17/124 CR09 .Deﬁnitions and some problems Basic algorithms Questions Deﬁnitions: Reducibility Reducible matrix: An n × n square matrix is reducible if there exists an n × n permutation matrix P such that PAP T = A11 O A12 A22 .

Therefore we may assume that A is irreducible and has a zero-free diagonal. B is fully indecomposable iﬀ A is (why?).Deﬁnitions and some problems Basic algorithms Questions Deﬁnitions: Fully indecomposability Fully indecomposable matrix: There is no permutation matrices P and Q such that A11 A12 PAQ = . for the sake of contradiction. Suppose. 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. A is not fully indecomposable. 18/124 CR09 . O A22 with the same condition on the blocks and their sizes as above. Only if part (by contradiction): Let B = PA be an irreducible matrix with zero-free main diagonal.

Hence. with the same condition on the blocks and their sizes as above. 2 19/124 CR09 . Proof cont.: Let P1 AQ1 be of the form above with A11 of size r × r . and A is reducible: contradiction. and Q = P1 Q1 is a permutation matrix which has to permute (why?) the ﬁrst r columns among themselves. A is in the above form. and similarly the last n − r columns among themselves. We may write P1 AQ1 = A Q . where A = P1 AP1 T with zero-free diagonal (why?).Deﬁnitions and some problems Basic algorithms Questions Fully indecomposable matrices Fully indecomposable matrix There is no permutation matrices P and Q such that PAQ = A11 O A12 A22 .

a set of vertices S ⊆ V is a clique if for all s. we have (s. or diagonal. In a symmetric matrix A. Maximal clique: A clique is a maximal clique. t) ∈ E . if it is not contained in another clique. R) is either zero. R) is full. Can we ﬁnd the largest one in polynomial time? In a symmetric matrix A. t ∈ S. a clique corresponds to a subset of rows R and the corresponding columns such that the matrix A(R. E ). Maximum clique: A clique of maximum cardinality (ﬁnding a maximum clique in an undirected graph is NP-complete). Independent set A set of vertices is an independent set if none of the vertices are adjacent to each other. 20/124 CR09 .Deﬁnitions and some problems Basic algorithms Questions Deﬁnitions: Cliques and independent sets Clique In an undirected graph G = (V . an independent set corresponds to a subset of rows R and the corresponding columns such that the matrix A(R.

Deﬁnitions and some problems Basic algorithms Questions

Deﬁnitions: 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

Deﬁnitions and some problems Basic algorithms Questions

Deﬁnitions: 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, deﬁne a hypergraph whose vertices correspond to the rows and whose nets correspond to the columns such that vertex vi is in net nj iﬀ aij = 0 (the column-net model). A sample matrix 1 0 1 × 2 @× 3 × 2 3 × ×
n4 v2 v3 n3 v2 n3 n2 v3 n4
22/124 CR09

The column-net hypergraph model 4 1 × ×A
n1 v1 v1 n2 n1

×

Deﬁnitions 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

directed or undirected with adjacency list repres.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Breadth-ﬁrst search: Idea Graph Searching: Breadth-First Search Graph G =(V.Bilkent University Computer Engineering Department 24/124 CR09 2 . 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 CS473 – Lecture 14 Cevdet Aykanat . E).

Bilkent University Computer Engineering Department 25/124 CR09 3 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Breadth-ﬁrst search: Key components 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 CS473 – Lecture 14 Cevdet Aykanat .

Bilkent University Computer Engineering Department 26/124 CR09 4 . d [u] # % color[s] # GRAY \$[s] # NIL. v) DEQUEUE(Q) color[u] # BLACK CS473 – Lecture 14 Cevdet Aykanat .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Breadth-ﬁrst search: Algorithm Breadth-First Search Algorithm BFS(G. 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. s) for each u ! V" {s} do color[u] # WHITE \$[u] # NIL.

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

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

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

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

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

Deﬁnitions and some problems Basic algorithms Questions

Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components

s
0 3 1

a
1

d
2

FIFO queue Q

just after processing vertex a b c f e

c b
2

e f i
3

g
3

h
3

!a" !a,b,c" !a,b,c,f" !a,b,c,f,e" !a,b,c,f,e,g,h" !a,b,c,f,e,g,h,d,i"

all distances are filled in after processing e
CS473 – Lecture 14 Cevdet Aykanat - Bilkent University Computer Engineering Department
32/124 CR09

10

Deﬁnitions and some problems Basic algorithms Questions

Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components

s
0 3 1

a
1

d
2

FIFO queue Q

just after processing vertex a b c f g

c b
2

e f i
3

g
3

h
3

!a" !a,b,c" !a,b,c,f" !a,b,c,f,e" !a,b,c,f,e,g,h" !a,b,c,f,e,g,h,d,i"

CS473 – Lecture 14

Cevdet Aykanat - Bilkent University Computer Engineering Department
33/124 CR09

11

Deﬁnitions and some problems Basic algorithms Questions

Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components

s
0 3 1

a
1

d
2

FIFO queue Q

just after processing vertex a b c f h

c b
2

e f i
3

g
3

h
3

!a" !a,b,c" !a,b,c,f" !a,b,c,f,e" !a,b,c,f,e,g,h" !a,b,c,f,e,g,h,d,i"

CS473 – Lecture 14

Cevdet Aykanat - Bilkent University Computer Engineering Department
34/124 CR09

12

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

d.b.c.g.e.b.c.f" !a.f.f.b.h" !a.i" algorithm terminates: all vertices are processed CS473 – Lecture 14 Cevdet Aykanat .Bilkent University Computer Engineering Department 36/124 CR09 14 .f.h.c.b.c" !a.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Breadth-ﬁrst search: Example Breadth-First Search s 0 3 1 a 1 d 2 FIFO queue Q just after processing vertex a b c f i c b 2 e f i 3 g 3 h 3 !a" !a.c.g.e" !a.b.e.

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Breadth-ﬁrst search: Analysis Algorithm Breadth-First Search 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 u#V ! | Adj[u ] |= !( E ) CS473 – Lecture 14 Cevdet Aykanat .Bilkent University Computer Engineering Department 37/124 CR09 15 .

v) in G PRINT-PATH(G.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! . s) in G! constitutes a sp(s.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Breadth-First Tree Generated by root Breadth-ﬁrst search: The paths to the BFS LEMMA 4: predecessor subgraph G!=(V!. unique path p(v. s. ![v] ) print v CS473 – Lecture 14 Prints out vertices on a s!v shortest path Cevdet Aykanat . s. E!) generated by BFS(G.Bilkent University Computer Engineering Department 38/124 CR09 25 . s) . v) if v = s then print s else if ![v] = NIL then print no “s!v path” else PRINT-PATH(G. where V! ={v " V: ![v] # NIL}\${s} and E! ={(![v].

Bilkent University Computer Engineering Department 39/124 CR09 26 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Breadth-ﬁrst search: The BFS tree Breadth-First Tree Generated by BFS BFS(G.a) s 0 3 1 s a 0 1 3 a 1 d 2 d 2 c 1 c e 2 b f g 3 b 2 e f i 3 i 3 h 3 g 3 h 3 CS473 – Lecture 14 Cevdet Aykanat .a) terminated BFT generated by BFS(G.

(#(H(.C#(>I( 7 J>(6E#5(6.#0?554LD( M N54@I#6#'J3O#P+(+(#9"*6)QR#3J3O#P+(+(#+5(>#4@#S3"= !"#\$%& '()*+.()*(>#?.#+@>4. 40/124 CR09 .#K1(@(H(.<=#>4.(#-\$ .()*(> 7 A>B6)(@)C#D45*#.(0.(5(@*6*4?@ 7 8?6DE#"C5*(F6*4)6DDC#(G0D?.601#8!9:.)1 7 8.5*#"(6.)1#>((0(.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Idea /(0*1234.*(G#6@># (H(.C#H(.

38340(< 6 '3?(#@2"A#.3(:<4#.5:#.4 *0(#9(.# 4*5*(4B#C5)0#9(.(<A# E J:5)?(8(<#H0(8#.*(D#34 E F83*35::G#H03*(A# E I.4*#"(5.*3)(4#*=#38<3)5*(#*0(3.(#-\$ 41/124 CR09 & .()=.=.#(5)0#!!> 6 '3?(#@2"A#)=:=.<4#<34)=9(.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Key components .G =.)0 6 7538*5384#4(9(.(/*0123.#5#H03*(#! <+.5G(<#H0(8#<34)=9(.38I# 4)58838I#K<LM"N#JG#"M!N# " !"#\$%& '()*+.

((4#E.22H#34#)>D/>4(=#>C# =34Q>38*#=(/*01C3.#?.2"#C>.(# G!"JE!K!L<!HM#!#F#58= !K!L\$ NO'P 6 @!" =(/*01C3.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Key components .(4*#E.(4* 6 @!"EF<G!H#I0(.(#-\$ \$ 42/124 CR09 .2"<#/.2R4H !"#\$%& '()*+.(=()(44>.4*#*.>=+)(=#AB# .)0 6 7893:(#.4*#C>.D4#4/58838?#C>.(/*0123.5/0#@! /.4*#"(5.

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Key components /(0*1234.)1 7 /3"#6859#*4:(5*6:05#(6)1#.(B*#C9.#(6)1#.#9C#6#.5*#"(6.6E(> 7 C?!@A#.5*#>45)9.(>#6B>#D.(.*(<#=4*1#*=9#*4:(5*6:05 7 >?!@A#.(.(.(B*#6B>#C4B4514BD# (.(#-! >?!@#" C?!@! HIJI K4:(#6<45#C9.()9.(.#*1(#)989.*(< >?!@ '()*+.(#45#9B8E#9B(#>45)9.>5#=1(B#! 45#C4B451(>#6B>#F86)G(B(> 7 "4B)(#*1(.>5#=1(B#! 45#C4.(.E#(.()9.H !"#\$%& C?!@ HIJI . 43/124 CR09 .(#-\$ .*(<#=(#16.

5*#"(6.% -\$').(#-\$ 44/124 CR09 .% -\$').+(/ #=. !"#789 \$%&'()*+ !!:#..*/0 E=!>" %\$'& " %\$'& %.! CBD=!>#.=!>" ()*+ B=!>" %\$'& " %\$'& %\$%&'()*+ ..>#" ! !"#012#23485'.<.=.+(/ !"#012#23485'!6 !"#\$%& '()*+.<.<.% ).=!> \$ "#\$%& ..)1 !"#012#23485'!6 ).<.6 ).Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Algorithm /(0*1234.> \$ "#\$%& .=!>" "#\$%& #=!>#" ?@' %\$'& " A \$%&'()*+ !!:#..=!>" -.<..

(#-\$ 45/124 CR09 % .(/*0123.#!<=">? 6 @83*35A3B5*3C8#ACC/#38#!"# .4*#"(5.*3)(4#58H J !"#\$%&#&')M*("+ 3::(H35*(AI#)CAC.#ACC/#CG#!"#\$%&#&')M*("+ 34#(E()+*(H#NOHPQ"RN#*3:( 6 "38)(#\$ NOHPQ"RN#% >S#*C*5A#)C4*#CG#(E()+*389#ACC/#CG# !"#\$%&#&'(34#!<>? !"#\$%& '()*+.5I 6 2C.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Analysis .4#+#54#9.)0 6 7+88389#*3:(.#(5)0#!#=#438)( J !"#\$%&#&'(34#38FCK(H#C8AI#C8#L03*(#F(.#!<=?#(E)A+43F(#CG#*3:(#*C#(E()+*(# )5AA4#*C#!"#\$%&#&'( 6 !"#\$%&#&'(34#)5AA(H#(E5)*AI#C8)(#GC.#!<=? 6 D538#ACC/#38#!"#.

(#-\$ 46/124 CR09 ( .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! " /(0*1234.5*#"(6. .( # \$ % & !"#\$%& ' '()*+.)17#896:0.

.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ! /(0*1234.(#-\$ 47/124 CR09 ( .5*#"(6.( " # \$ % & !"#\$%& ' '()*+.)17#896:0.

.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ! /(0*1234.(#-\$ 48/124 CR09 ( -.5*#"(6.)17#896:0.( " # \$ " % & !"#\$%& ' '()*+.

)06#7859/:( " # \$ " % # & !"#\$%& ' '()*+.(#-\$ 49/124 CR09 ( -- .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ! .4*#"(5.(/*0123.

)17#896:0.5*#"(6.( " # \$ " % # & !"#\$%& ' '()*+.(#-\$ 50/124 CR09 ( -. .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ! /(0*1234.

(/*0123.)06#7859/:( ! ! " # \$ " % # \$ & !"#\$%& ' '()*+.4*#"(5.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example .(#-\$ 51/124 CR09 ( -& .

)06#7859/:( " # \$ " % # \$ % & !"#\$%& ' '()*+.4*#"(5.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ! .(/*0123.(#-\$ 52/124 CR09 ( -\$ .

.(#-\$ 53/124 CR09 ( -.5*#"(6.( " # \$ " % # \$ % & !"#\$%& ' '()*+.)17#896:0.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ! /(0*1234.

(#-\$ 54/124 CR09 ( -.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example /(0*1234.( ! ! " # \$ " % # \$ % & & !"#\$%& ' '()*+.5*#"(6.)17#896:0. .

)06#7859/:( ! ! " # \$ " ' % # \$ % & & !"#\$%& ' '()*+.(/*0123.4*#"(5.(#-\$ 55/124 CR09 ( -% .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example .

( ! ! ( " # \$ " ' % # \$ % & & !"#\$%& ' '()*+.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example /(0*1234. .5*#"(6.)17#896:0.(#-\$ 56/124 CR09 ( -.

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example /(0*1234.(#-\$ 57/124 CR09 ( -.)17#896:0.5*#"(6.( ! ! ( " # \$ " ' % # \$ % & & !"#\$%& ' '()*+. .

/ .(#-\$ 58/124 CR09 ( .1<( " # \$ " ' ) % # \$ % & & !"#\$%& ' '()*+.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ! ( 0(1*2345.6*#"(7.)28#9:7.

- .5*#"(6.)17#896:0.( " # \$ " ' ) % # \$ % & & !"#\$%& ' '()*+.(#-\$ 59/124 CR09 ( .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ! ( /(0*1234.

5*#"(6.( ! ! ( " # \$ " ' % ) !* # \$ % & & !"#\$%& ' '()*+. .(#-\$ 60/124 CR09 ( .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example /(0*1234..)17#896:0.

( ! ! " * !! # \$ " ' % ( !) # \$ % & & !"#\$%& ' '()*+.& .5*#"(6.(#-\$ 61/124 CR09 ( .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example /(0*1234.)17#896:0.

5*#"(6.\$ .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example /(0*1234.( ! ( (! " * (( # \$ ! & % ' () " # \$ % & !"#\$%& ' '()*+.)17#896:0.(#-\$ 62/124 CR09 ( .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example 0(1*2345./ .1<( ! ( (! " * (( (" # \$ ! & % ' () " # \$ % & !"#\$%& ' '()*+.6*#"(7.(#-\$ 63/124 CR09 ( .)28#9:7.

1<( " * (( (" # \$ ! & % ' () " # \$ % & !"#\$%& ' '()*+./ .)28#9:7.6*#"(7.(#-\$ 64/124 CR09 ( .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ( (! 0(1*2345.

5*#"(6.% .)17#896:0.( ! ( (! " * (( (" # \$ ! & % ' () " # \$ % & !"#\$%& ' '()*+.(#-\$ 65/124 CR09 ( .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example /(0*1234.

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example 0(1*2345.6*#"(7.(#-\$ 66/124 CR09 ( .1<( ! ( (! " * (( (" # \$ ! & % ' () " # \$ % (# & !"#\$%& ' '()*+.)28#9:7./ .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ( (! 0(1*2345.1<( " * (( (" # \$ ! & % ' () " # \$ % (# & !"#\$%& ' '()*+.(#-\$ 67/124 CR09 ( ./ .6*#"(7.)28#9:7.

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example ! ( (! /(0*1234. .5*#"(6.)17#896:0.(#-\$ 68/124 CR09 ( &.( " * (( (" # \$ ! & % ' () " # \$ % (# (\$ & !"#\$%& ' '()*+.

4*#"(5.)06#7859/:( ! ( (! " * (( # (" (% \$ ! & % ' () " # \$ % (# (\$ & !"#\$%& ' '()*+.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Example .(#-\$ 69/124 CR09 ( &- .(/*0123.

5*#AB. 70/124 CR09 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: DFT and DFF /3"<=>#*(.(5*#</33> ! ( (! " * (( # (" (% ! ( (! " * (( # (" (% \$ ! & % ' () \$ ! & % ' () " # \$ % (# (\$ " # \$ % (# (\$ & ' ( & ' ( !"#\$%& '()*+.)17#896:0.( /(0*12A4.(#-\$ &.:4?6*(@ /(0*1234.5*#"(6.

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 CS473 – Lecture 16 Cevdet Aykanat .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Parenthesis theorem DFS: Parenthesis Theorem Thm: In any DFS of G=(V.Bilkent University Computer Engineering Department 2 71/124 CR09 .E). let int[v] = [d[v].

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Parenthesis theorem Parenthesis Theorem 2 x 1 12 y 8 11 z 13 16 s 7 t 9 10 3 4 5 6 14 15 w 1 2 3 4 5 6 7 8 9 1 0 1 1 v 1 2 1 3 1 4 1 5 u 1 6 x z s w 1 2 3 4 5 y v 6 7 8 9 u t 1 0 1 1 1 2 1 3 1 4 1 5 1 6 (x (s (w w ) (v v ) s ) 72/124 (y (t CR09 t) y ) x ) (z (u u ) z) .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation 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 CS473 – Lecture 16 Cevdet Aykanat .Bilkent University Computer Engineering Department 6 73/124 CR09 .

v) is a cross edge if d[u] > d[v] CS473 – Lecture 16 Cevdet Aykanat .v) is a forward edge if d[u] < d[v] – (u.Bilkent University Computer Engineering Department 74/124 CR09 7 . which are cross Let BLACK vertex v !Adj[u] is encountered while processing GRAY vertex u – (u.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation Edge Classification in a DFF • How to decide which GRAY to BLACK edges are forward.

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 y z s t w CS473 – Lecture 16 v Cevdet Aykanat .Bilkent University Computer Engineering Department 75/124 CR09 u 8 .

Bilkent University Computer Engineering Department 76/124 CR09 u 9 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 y z s 2 T t w CS473 – Lecture 16 v Cevdet Aykanat .

Bilkent University Computer Engineering Department 77/124 CR09 u 10 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 y z s 2 T t T 3 w CS473 – Lecture 16 v Cevdet Aykanat .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 y z s 2 T B t T 3 w CS473 – Lecture 16 v Cevdet Aykanat .Bilkent University Computer Engineering Department 78/124 CR09 u 11 .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x T s 2 1 y z t B T 3 4 w CS473 – Lecture 16 v Cevdet Aykanat .Bilkent University Computer Engineering Department 79/124 CR09 u 12 .

Bilkent University Computer Engineering Department 80/124 CR09 u 13 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 y z s 2 T t B T T 3 4 5 w CS473 – Lecture 16 v Cevdet Aykanat .

Bilkent University Computer Engineering Department 81/124 CR09 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 y z s 2 T t B T T 3 4 5 w CS473 – Lecture 16 C v u 14 Cevdet Aykanat .

Deﬁnitions and some problems Basic algorithms Questions

Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components

Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example
x
1

y

z

s
2

T B T T
3 4

t

C

5 6

w
CS473 – Lecture 16

v
Cevdet Aykanat - Bilkent University Computer Engineering Department
82/124 CR09

u
15

Deﬁnitions and some problems Basic algorithms Questions

Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components

Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example
x
1

y

z

s
2 7

T B T
3 4

t

T w
CS473 – Lecture 16

C

5 6

v

u
16

Cevdet Aykanat - Bilkent University Computer Engineering Department
83/124 CR09

Deﬁnitions and some problems Basic algorithms Questions

Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components

Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example
x
1

y T
8

z

s
2 7

T t B T
3 4

T w
CS473 – Lecture 16

C

5 6

v

u
17

Cevdet Aykanat - Bilkent University Computer Engineering Department
84/124 CR09

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 y T 8 z s 2 7 T t B T T 3 4 C w CS473 – Lecture 16 C 5 6 v u 18 Cevdet Aykanat .Bilkent University Computer Engineering Department 85/124 CR09 .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 y T 8 z T s 2 7 T B T 3 4 t 9 C T C 5 6 w CS473 – Lecture 16 v Cevdet Aykanat .Bilkent University Computer Engineering Department 86/124 CR09 u 19 .

Bilkent University Computer Engineering Department 87/124 CR09 u 20 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 y T 8 z T s 2 7 T B T 3 4 t 9 C T C 5 6 C w CS473 – Lecture 16 v Cevdet Aykanat .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 y T 8 z T t s 2 7 T B T T 3 4 C C 5 6 9 10 C w CS473 – Lecture 16 v Cevdet Aykanat .Bilkent University Computer Engineering Department 88/124 CR09 u 21 .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x T s 2 7 1 y T F T 8 11 z T t C C 5 6 9 10 B T 3 4 w CS473 – Lecture 16 C v u 22 Cevdet Aykanat .Bilkent University Computer Engineering Department 89/124 CR09 .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 12 y T F T 8 11 z T s 2 7 T B C C 5 6 t 9 10 T 3 4 C w CS473 – Lecture 16 v Cevdet Aykanat .Bilkent University Computer Engineering Department 90/124 CR09 u 23 .

Bilkent University Computer Engineering Department 91/124 CR09 u 24 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x T 1 12 8 11 13 y T C T C C 5 6 z s 2 7 T B F t 9 10 T 3 4 w CS473 – Lecture 16 v Cevdet Aykanat .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 12 y T F T 8 11 z C T t 13 s 2 7 T B C 9 10 C 5 6 T 3 4 C w CS473 – Lecture 16 v Cevdet Aykanat .Bilkent University Computer Engineering Department 92/124 CR09 u 25 .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x T 1 12 y T F C T C 5 6 8 11 z C T t C 13 s 2 7 B 9 10 T 3 4 w CS473 – Lecture 16 C v u 26 Cevdet Aykanat .Bilkent University Computer Engineering Department 93/124 CR09 .

Bilkent University Computer Engineering Department 94/124 CR09 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 12 y T F 8 11 z C T t C 13 s 2 7 T B C T C 9 10 T T 3 4 5 6 14 w CS473 – Lecture 16 C v u 27 Cevdet Aykanat .

Bilkent University Computer Engineering Department 95/124 CR09 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 12 y T F B T C C C 5 6 8 11 z C T t C 9 10 13 s 2 7 T T T 3 4 w CS473 – Lecture 16 v C 14 u 28 Cevdet Aykanat .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Edge classiﬁcation example Depth-First Search: Example x 1 12 y T F B C T C 5 6 8 11 z C T t C T 13 16 s 2 7 T 9 10 T 3 4 w CS473 – Lecture 16 C v C 14 15 u 29 Cevdet Aykanat .Bilkent University Computer Engineering Department 96/124 CR09 .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Undirected graphs Edge classiﬁcation Any DFS on an undirected graph produces only Tree and Back edges. 6/7 a 5/8 c g i j 4/11 h 3/12 13/14 d f 9/10 e b h d j 1/20 g c f i 2/19 e 15/18 b 16/17 97/124 CR09 a .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Depth-ﬁrst search: Non-recursive algorithm [π. 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 . f ]=DFS(G . d.

v ) is a directed edge. Ordering is not necessarily unique. 99/124 CR09 . then u appears before v in the ordering.Deﬁnitions 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.

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Topological sort: Example 11/16 under short socks 17/18 13/14 watch 9/10 12/15 pants shoes shirt 1/8 belt 6/7 jacket 3/4 tie 2/5 socks under short pants shoes watch shirt belt tie jacket 17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4 100/124 CR09 .

f [u] is going to be computed. 101/124 CR09 . hence f [u] > f [v ]. where u is Gray. If v is Black. v ). v ) ∈ E . If v is White. The algorithm: Correctness if (u.Deﬁnitions 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. hence f [u] > f [v ]. then u is an ancestor of v . 2 v cannot be Gray (otherwise a Back edge in an acyclic graph !!!). f [v ] is computed already. then f [u] > f [v ] Proof: Consider the color of v during exploring the edge (u.

v ) : (v . Constructing G T from G takes O(V + E ) time with adjacency list (like the CSR or CSC storage format for sparse matrices) representation.Deﬁnitions 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. Notice that G and G T have the same SCCs. u) ∈ E }. where E T = {(u. the transpose is deﬁned as G T = (V . 102/124 CR09 . E ). E T ). For a graph G = (V .

Bilkent University Computer Engineering Department 3 103/124 CR09 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Algorithm Algorithm Strongly Connected Components (1) Run DFS(G) to compute finishing times for all u!V (2) Compute GT (3) Call DFS(GT) 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 CS473 – Lecture 16 Cevdet Aykanat .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Analysis Strongly Connected Components Lemma 1: no path between a pair of vertices in the same SCC.Bilkent University Computer Engineering Department 104/124 CR09 w QED 4 . 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 u SCC v CS473 – Lecture 16 Cevdet Aykanat .

Bilkent University Computer Engineering Department 105/124 CR09 .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example b c a d e CS473 – Lecture 16 f g h 5 Cevdet Aykanat .

Bilkent University Computer Engineering Department .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example b 1 (1) Run DFS(G) to compute finishing times for all u!V 1 a c d e CS473 – Lecture 16 f 106/124 CR09 g h 6 Cevdet Aykanat .

Bilkent University Computer Engineering Department .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example b c 1 10 (1) Run DFS(G) to compute finishing times for all u!V 1 a d 8 9 3 4 2 7 5 6 e CS473 – Lecture 16 f 107/124 CR09 g h 7 Cevdet Aykanat .

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example 2 (1) Run DFS(G) to compute finishing times for all u!V a b 11 c 1 10 d 8 9 3 4 2 7 5 6 e CS473 – Lecture 16 f 108/124 CR09 g h 8 Cevdet Aykanat .Bilkent University Computer Engineering Department .

d. c. g. a.Bilkent University Computer Engineering Department 109/124 CR09 9 . h. f " CS473 – Lecture 16 Cevdet Aykanat .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example 2 1 a 13 14 b 11 16 c 1 10 d 8 9 12 15 3 4 2 7 5 6 e f g h Vertices sorted according to the finishing times: !b. e.

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example b c (2) Compute GT a d e CS473 – Lecture 16 f g h 10 Cevdet Aykanat .Bilkent University Computer Engineering Department 110/124 CR09 .

e. c. g. a.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: !b. f " a b c d e CS473 – Lecture 16 f 111/124 g CR09 h 11 Cevdet Aykanat . h. d.Bilkent University Computer Engineering Department .

d. h.Bilkent University Computer Engineering Department . e. g. c.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: !b. f " a r1= b c d e CS473 – Lecture 16 f 112/124 CR09 g h 12 Cevdet Aykanat . a.

d. e. a. g. h. c.Bilkent University Computer Engineering Department .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: !b. f " a r1= b c d e CS473 – Lecture 16 f 113/124 CR09 g h 13 Cevdet Aykanat .

Bilkent University Computer Engineering Department . a. h. g. e.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: !b. d. f " a r1= b r2= c d e CS473 – Lecture 16 f 114/124 CR09 g h 14 Cevdet Aykanat . c.

c.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: !b. h. g.Bilkent University Computer Engineering Department . f " a r1= b r2= c d e CS473 – Lecture 16 f 115/124 CR09 g h 15 Cevdet Aykanat . d. e. a.

Bilkent University Computer Engineering Department . e.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: !b. c. g. d. f " a r1= b r2= c d e CS473 – Lecture 16 f 116/124 r3= g CR09 h 16 Cevdet Aykanat . h. a.

e.Bilkent University Computer Engineering Department . f " a r1= b r2= c d e CS473 – Lecture 16 f 117/124 r3= g CR09 h 17 Cevdet Aykanat . g. h.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: !b. a. d. c.

h. f " a r1= b r2= c d e CS473 – Lecture 16 f 118/124 r3= g CR09 r4=h 18 Cevdet Aykanat .Bilkent University Computer Engineering Department . g. a. d. e.Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: !b. c.

Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example r2= c (4) Output vertices of each DFT in DFF as a separate SCC a r1= b d Cc={c.e} CS473 – Lecture 16 Cg={g.Bilkent University Computer Engineering Department 119/124 CR09 Ch={h} 19 .d} e f r3= g r4=h Cb={b.f} Cevdet Aykanat .a.

Bilkent University Computer Engineering Department .Deﬁnitions and some problems Basic algorithms Questions Breadth-ﬁrst search Depth-ﬁrst search Topological sort Strongly connected components Strongly connected components: Example SCC: Example a b c d e f Acyclic component graph g c.b.g h 20 Cevdet Aykanat .e Cb CS473 – Lecture 16 Cg 120/124 f.d Cc Ch CR09 h a.

Any vertex that is reachable from r in G T should be in the SCC of r (why?) 121/124 CR09 . It is a root of a DFT.Deﬁnitions 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 ). Consider the vertex r with the largest ﬁnishing time. the last vertex ﬁnished in an SCC is the ﬁrst vertex discovered in the SCC. all vertices in the same SCC are placed in the same DFT. In the DFS(G ) step of the algorithm.

where A11 is an r × r submatrix.Deﬁnitions 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 = A11 O A12 A22 . 122/124 CR09 . can be in that form (how many SCCs?). 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 A11 . too. A22 is an (n − r ) × (n − r ) submatrix.

the structure of Ak . 123/124 CR09 . where b = Ax for a given sparse matrix A. for k > 1. the structure of PAQ for a given square sparse matrix A and two permutation matrices P and Q. the structure of AAT . the structure of the vector b.Deﬁnitions 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. and a sparse vector x.

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 .Deﬁnitions and some problems Basic algorithms Questions Could not get enough of it: Questions Can you deﬁne: the row-net hypergraph model of a matrix. a matching in a hypergraph (is it a hard problem?).