Introduction To Graph Theory and Algorithms: Jean-Yves L'Excellent and Bora U Car
Introduction To Graph Theory and Algorithms: Jean-Yves L'Excellent and Bora U Car
Introduction To Graph Theory and Algorithms: Jean-Yves L'Excellent and Bora U Car
Introduction to
graph theory and algorithms
Jean-Yves LExcellent and Bora U car
GRAAL, LIP, ENS Lyon, France
CR-07: Sparse Matrix Computations, September 2010
http://graal.ens-lyon.fr/
~
bucar/CR07/
1/124 CR09
Outline
Outline
1
Denitions and some problems
2
Basic algorithms
Breadth-rst search
Depth-rst search
Topological sort
Strongly connected components
3
Questions
2/124 CR09
Denitions and some problems
Basic algorithms
Questions
Graph notations and denitions
A graph G = (V, E) consists of a nite set V, called the vertex set and a
nite, binary relation E on V, called the edge set.
Three standard graph models
Undirected graph: The edges are unordered pair of vertices, i.e.,
{u, v} E for some u, v V.
Directed graph: The edges are ordered pair of vertices, that is, (u, v) and
(v, u) are two dierent edges.
Bipartite graph: G = (U V, E) consists of two disjoint vertex sets U
and V such that for each edge (u, v) E, u U and v V.
An ordering or labelling of G = (V, E) having n vertices, i.e., |V| = n, is
a mapping of V onto 1, 2, . . . , n.
3/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matrices and graphs: Rectangular matrices
The rows/columns and nonzeros of a given sparse matrix correspond (with
natural labelling) to the vertices and edges, respectively, of a graph.
Rectangular matrices
A =
0
@
1 2 3 4
1
2
3
1
A
Bipartite graph
1
r
2
r
3
1
c
2
c
3
c
4
c
r
The set of rows corresponds to one of the vertex set R, the set of columns
corresponds to the other vertex set C such that for each a
ij
= 0, (r
i
, c
j
) is an
edge.
4/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matrices and graphs: Square unsymmetric pattern
The rows/columns and nonzeros of a given sparse matrix correspond (with
natural labelling) to the vertices and edges, respectively, of a graph.
Square unsymmetric
pattern matrices
A =
0
@
1 2 3
1
2
3
1
A
Graph models
Bipartite graph as before.
Directed graph
v
2 3
v
1
v
The set of rows/cols corresponds the vertex set V such that for each a
ij
= 0,
(v
i
, v
j
) is an edge. Transposed view possible too, i.e., the edge (v
i
, v
j
) directed
from column i to row j . Usually self-loops are omitted.
5/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matrices and graphs: Square unsymmetric pattern
A special subclass
Directed acyclic graphs (DAG):
A directed graphs with no loops
(maybe except for self-loops).
DAGs
We can sort the vertices such that
if (u, v) is an edge, then u appears
before v in the ordering.
Question: What kind of matrices have a DAG?
6/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matrices and graphs: Symmetric pattern
The rows/columns and nonzeros of a given sparse matrix correspond (with
natural labelling) to the vertices and edges, respectively, of a graph.
Square symmetric
pattern matrices
A =
0
@
1 2 3
1
2
3
1
A
Graph models
Bipartite and directed graphs as before.
Undirected graph
v
2 3
v
1
v
The set of rows/cols corresponds the vertex set V such that for each
a
ij
, a
ji
= 0, {v
i
, v
j
} is an edge. No self-loops; usually the main diagonal is
assumed to be zero-free.
7/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Edges, degrees, and paths
Many denitions for directed and undirected graphs are the same. We
will use (u, v) to refer to an edge of an undirected or directed graph to
avoid repeated denitions.
An edge (u, v) is said to incident on the vertices u and v.
For any vertex u, the set of vertices in adj(u) = {v : (u, v) E} are
called the neighbors of u. The vertices in adj(u) are said to be
adjacent to u.
The degree of a vertex is the number of edges incident on it.
A path p of length k is a sequence of vertices v
0
, v
1
, . . . , v
k
where
(v
i 1
, v
i
) E for i = 1, . . . , k. The two end points v
0
and v
k
are
said to be connected by the path p, and the vertex v
k
is said to be
reachable from v
0
.
8/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Components
An undirected graph is said to be connected if every pair of vertices
is connected by a path.
The connected components of an undirected graph are the
equivalence classes of vertices under the is reachable from
relation.
A directed graph is said to be strongly connected if every pair of
vertices are reachable from each other.
The strongly connected components of a directed graph are the
equivalence classes of vertices under the are mutually reachable
relation.
9/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Trees and spanning trees
A tree is a connected, acyclic, undirected graph. If an undirected graph is
acyclic but disconnected, then it is a forest.
Properties of trees
Any two vertices are connected by a unique path.
|E| = |V| 1
A rooted tree is a tree with a distinguished vertex r , called the root.
There is a unique path from the root r to every other vertex v. Any
vertex y in that path is called an ancestor of v. If y is an ancestor of v,
then v is a descendant of y.
The subtree rooted at v is the tree induced by the descendants of v,
rooted at v.
A spanning tree of a connected graph G = (V, E) is a tree T = (V, F),
such that F E.
10/124 CR09
Denitions and some problems
Basic algorithms
Questions
Ordering of the vertices of a rooted tree
A topological ordering of a rooted tree is an ordering that numbers
children vertices before their parent.
A postorder is a topological ordering which numbers the vertices in
any subtree consecutively.
u w
x
y
z
v
with topological ordering
1
3
2
4
5
6
Rooted spanning tree
w
y z
x u
v
Connected graph G
u w
x
y
z
v
1
6
5 4
3
2
Rooted spanning tree
with postordering
11/124 CR09
Denitions and some problems
Basic algorithms
Questions
Postordering the vertices of a rooted tree I
The following recursive algorithm will do the job:
[porder ]=PostOrder(T, r )
for each child c of r do
porder [porder , PostOrder(T, c)]
porder [porder , r ]
We need to run the algorithm for each root r when T is a forest.
Usually recursive algorithms are avoided, as for a tree with large number
of vertices can cause stack overow.
12/124 CR09
Denitions and some problems
Basic algorithms
Questions
Postordering the vertices of a rooted tree II
[porder ]=PostOrder(T, r )
porder []
seen(v) False for all v T
seen(r ) True
Push(S, r )
while NotEmpty(S) do
v Pop(S)
if a child c of v with seen(c) = False then
seen(c) True
Push(S, c)
else
porder [porder , v]
Again, have to run for each root, if T is a forest.
Both algorithms run in O(n) time for a tree with n nodes.
13/124 CR09
Denitions and some problems
Basic algorithms
Questions
Permutation matrices
A permutation matrix is a square (0, 1)-matrix where each row and
column has a single 1.
If P is a permutation matrix, PP
T
= I , i.e., it is an orthogonal matrix.
Let,
A =
1 2 3
1
2
3
2 1 3
1
2
3
1 2 3
1
2
3
1 2 3
1 1
2 1
3 1
14/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matching in bipartite graphs and permutations
A matching in a graph is a set of edges no two of which share a common
vertex. We will be mostly dealing with matchings in bipartite graphs.
In matrix terms, a matching in the bipartite graph of a matrix
corresponds to a set of nonzero entries no two of which are in the same
row or column.
A vertex is said to be matched if there is an edge in the matching
incident on the vertex, and to be unmatched otherwise. In a perfect
matching, all vertices are matched.
The cardinality of a matching is the number of edges in it. A maximum
cardinality matching or a maximum matching is a matching of maximum
cardinality. Solvable in polynomial time.
15/124 CR09
Denitions and some problems
Basic algorithms
Questions
Matching in bipartite graphs and permutations
Given a square matrix whose bipartite graph has a perfect matching, such
a matching can be used to permute the matrix such that the matching
entries are along the main diagonal.
1
r
2
r
3 3
c
2
c
1
c r
1 2 3
1
2
3
2 1 3
1
2
3
1 2 3
1
2
3
1 2 3
1 1
2 1
3 1
16/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Reducibility
Reducible matrix: An n n square matrix is reducible if there exists an
n n permutation matrix P such that
PAP
T
=
A
11
A
12
O A
22
,
where A
11
is an r r submatrix, A
22
is an (n r ) (n r ) submatrix,
where 1 r < n.
Irreducible matrix: There is no such a permutation matrix.
Theorem: An n n square matrix is irreducible i its directed graph is
strongly connected.
Proof: Follows by denition.
17/124 CR09
Denitions and some problems
Basic algorithms
Questions
Denitions: Fully indecomposability
Fully indecomposable matrix: There is no permutation matrices P and Q
such that
PAQ =
A
11
A
12
O A
22
,
with the same condition on the blocks and their sizes as above.
Theorem: An n n square matrix A is fully indecomposable i for some
permutation matrix P, the matrix PA is irreducible and has a zero-free
main diagonal.
Proof: We will come later in the semester to the if part.
Only if part (by contradiction): Let B = PA be an irreducible matrix with
zero-free main diagonal. B is fully indecomposable i A is (why?).
Therefore we may assume that A is irreducible and has a zero-free
diagonal. Suppose, for the sake of contradiction, A is not fully
indecomposable.
18/124 CR09
Denitions and some problems
Basic algorithms
Questions
Fully indecomposable matrices
Fully indecomposable matrix
There is no permutation matrices P and Q such that
PAQ =
A
11
A
12
O A
22
,
with the same condition on the blocks and their sizes as above.
Proof cont.: Let P
1
AQ
1
be of the form above with A
11
of size r r . We
may write P
1
AQ
1
= A
, where A
= P
1
AP
1
T
with zero-free diagonal
(why?), and Q
= P
1
Q
1
is a permutation matrix which has to permute
(why?) the rst r columns among themselves, and similarly the last n r
columns among themselves. Hence, A
A
11
A
12
O A
22
,
where A
11
is an r r submatrix, A
22
is an (n r ) (n r ) submatrix,
where 1 r < n:
run SCC on the directed graph of A to identify each strongly connected
component as an irreducible block (more than one SCC?). Hence A
11
,
too, can be in that form (how many SCCs?).
122/124 CR09
Denitions and some problems
Basic algorithms
Questions
Could not get enough of it: Questions
How would you describe the following in the language of graphs
the structure of PAP
T
for a given square sparse matrix A and a
permutation matrix P,
the structure of PAQ for a given square sparse matrix A and two
permutation matrices P and Q,
the structure of A
k
, for k > 1,
the structure of AA
T
,
the structure of the vector b, where b = Ax for a given sparse
matrix A, and a sparse vector x.
123/124 CR09
Denitions and some problems
Basic algorithms
Questions
Could not get enough of it: Questions
Can you dene:
the row-net hypergraph model of a matrix.
a matching in a hypergraph (is it a hard problem?).
Can you relate:
the DFS or BFS on a tree to a topological ordering? postordering?
Find an algorithm
how do you transpose a matrix in CSR or CSC format?
124/124 CR09