lecture graphs
TRANSCRIPT
-
8/3/2019 Lecture Graphs
1/124
Outline
Introduction tograph theory and algorithms
Jean-Yves LExcellent and Bora Ucar
GRAAL, LIP, ENS Lyon, France
CR-07: Sparse Matrix Computations, September 2010http://graal.ens-lyon.fr/~bucar/CR07/
1/124 CR09
http://graal.ens-lyon.fr/~bucar/CR07/http://graal.ens-lyon.fr/~bucar/CR07/http://graal.ens-lyon.fr/~bucar/CR07/http://graal.ens-lyon.fr/~bucar/CR07/http://find/http://goback/ -
8/3/2019 Lecture Graphs
2/124
Outline
Outline
1 Definitions and some problems
2 Basic algorithmsBreadth-first searchDepth-first searchTopological sortStrongly connected components
3 Questions
2/124 CR09
http://find/http://goback/ -
8/3/2019 Lecture Graphs
3/124
Definitions and some problemsBasic algorithms
Questions
Graph notations and definitions
A graph G = (V,E) consists of a finite set V, called the vertex set and afinite, 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 different 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 ofG = (V,E) having n vertices, i.e., |V| = n, isa mapping ofV onto 1, 2, . . . , n.
3/124 CR09
http://find/http://goback/ -
8/3/2019 Lecture Graphs
4/124
Definitions and some problemsBasic algorithms
Questions
Matrices and graphs: Rectangular matrices
The rows/columns and nonzeros of a given sparse matrix correspond (withnatural labelling) to the vertices and edges, respectively, of a graph.
Rectangular matrices
A =
0@
1 2 3 41 2 3
1A
Bipartite graph
1
r2
r3
1c
2c
3c
4c
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 aij = 0, (ri, cj) is an
edge.
4/124 CR09
http://find/http://goback/ -
8/3/2019 Lecture Graphs
5/124
Definitions and some problemsBasic 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 unsymmetricpattern matrices
A =
0@
1 2 31 2 3
1A
Graph models
Bipartite graph as before.
Directed graph
v2 3v
1v
The set of rows/cols corresponds the vertex set V such that for each aij = 0,
(vi, vj) is an edge. Transposed view possible too, i.e., the edge (vi, vj) directed
from column i to row j. Usually self-loops are omitted.5/124 CR09
http://find/http://goback/ -
8/3/2019 Lecture Graphs
6/124
Definitions and some problemsBasic 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 thatif (u, v) is an edge, then u appearsbefore v in the ordering.
Question: What kind of matrices have a DAG?
6/124 CR09
http://find/http://goback/ -
8/3/2019 Lecture Graphs
7/124
Definitions and some problemsBasic 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 symmetricpattern matrices
A =
0@
1 2 31 2 3
1A
Graph models
Bipartite and directed graphs as before.
Undirected graph
v2 3v
1v
The set of rows/cols corresponds the vertex set V such that for each
aij, aji = 0, {vi, vj} is an edge. No self-loops; usually the main diagonal is
assumed to be zero-free.7/124 CR09
http://find/http://goback/ -
8/3/2019 Lecture Graphs
8/124
Definitions and some problemsBasic algorithms
Questions
Definitions: Edges, degrees, and paths
Many definitions for directed and undirected graphs are the same. Wewill use (u, v) to refer to an edge of an undirected or directed graph toavoid repeated definitions.
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} arecalled the neighbors ofu. The vertices in adj(u) are said to beadjacent 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 v0, v1, . . . , vk where(vi1, vi) E for i = 1, . . . , k. The two end points v0 and vk aresaid to be connected by the path p, and the vertex vk is said to bereachable from v0.
8/124 CR09
D fi iti d bl
http://find/http://goback/ -
8/3/2019 Lecture Graphs
9/124
Definitions and some problemsBasic algorithms
Questions
Definitions: Components
An undirected graph is said to be connected if every pair of verticesis connected by a path.
The connected components of an undirected graph are the
equivalence classes of vertices under the is reachable fromrelation.
A directed graph is said to be strongly connected if every pair ofvertices are reachable from each other.
The strongly connected components of a directed graph are theequivalence classes of vertices under the are mutually reachablerelation.
9/124 CR09
Definitions and some problems
http://find/http://goback/ -
8/3/2019 Lecture Graphs
10/124
Definitions and some problemsBasic algorithms
Questions
Definitions: Trees and spanning trees
A tree is a connected, acyclic, undirected graph. If an undirected graph isacyclic 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. Anyvertex y in that path is called an ancestor ofv. Ify is an ancestor ofv,
then v is a descendant ofy.The subtree rooted at v is the tree induced by the descendants ofv,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
Definitions and some problems
http://find/http://goback/ -
8/3/2019 Lecture Graphs
11/124
Definitions and some problemsBasic algorithms
Questions
Ordering of the vertices of a rooted tree
A topological ordering of a rooted tree is an ordering that numberschildren vertices before their parent.
A postorder is a topological ordering which numbers the vertices inany subtree consecutively.
u w
x
y
z
v
with topological ordering
1 3
2
4
5
6
Rooted spanning tree
w
yz
xu
v
Connected graph G
u w
x
y
z
v
1
6
54
3
2
Rooted spanning trewith postordering
11/124 CR09
Definitions and some problems
http://find/http://goback/ -
8/3/2019 Lecture Graphs
12/124
Definitions and some problemsBasic 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 doporder [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 numberof vertices can cause stack overflow.
12/124 CR09
Definitions and some problems
http://find/http://goback/ -
8/3/2019 Lecture Graphs
13/124
Definitions and some problemsBasic algorithms
Questions
Postordering the vertices of a rooted tree II
[porder]=PostOrder(T, r)
porder []seen(v) False for all v Tseen(r) TruePush(S, r)
while NotEmpty(S) dov Pop(S)if a child c of v with seen(c) = False thenseen(c) TruePush(S, c)
elseporder [porder, v]
Again, have to run for each root, ifT is a forest.
Both algorithms run in O(n) time for a tree with n nodes.
13/124 CR09
Definitions and some problems
http://find/http://goback/ -
8/3/2019 Lecture Graphs
14/124
pBasic algorithms
Questions
Permutation matrices
A permutation matrix is a square (0, 1)-matrix where each row and
column has a single 1.
IfP is a permutation matrix, PPT = 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]. Define p2,1 = 1,p1,2 = 1, p3,3 = 1, and B= AP (if column j to be at position i, set
pji = 1)
B=
2 1 3
1 2 3
=
1 2 3
1 2 3
1 2 3
1 12 13 1
14/124 CR09
Definitions and some problems
http://find/http://goback/ -
8/3/2019 Lecture Graphs
15/124
Basic algorithmsQuestions
Matching in bipartite graphs and permutations
A matching in a graph is a set of edges no two of which share a commonvertex. We will be mostly dealing with matchings in bipartite graphs.
In matrix terms, a matching in the bipartite graph of a matrixcorresponds to a set of nonzero entries no two of which are in the samerow or column.
A vertex is said to be matched if there is an edge in the matchingincident on the vertex, and to be unmatched otherwise. In a perfectmatching, all vertices are matched.
The cardinality of a matching is the number of edges in it. A maximumcardinality matching or a maximum matching is a matching of maximumcardinality. Solvable in polynomial time.
15/124 CR09
Definitions and some problemsB i l i h
http://find/http://goback/ -
8/3/2019 Lecture Graphs
16/124
Basic algorithmsQuestions
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 matchingentries are along the main diagonal.
1
r2
r3 3
c
2c
1cr
1 2 3
1 2 3
2 1 3
1 2 3
=
1 2 3
1 2 3
1 2 3
1 12 13 1
16/124 CR09
Definitions and some problemsB i l ith
http://find/http://goback/ -
8/3/2019 Lecture Graphs
17/124
Basic algorithmsQuestions
Definitions: Reducibility
Reducible matrix: An n n square matrix is reducible if there exists ann n permutation matrix P such that
PAPT = A11 A12
O A22 ,
where A11 is an r r submatrix, A22 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 iff its directed graph isstrongly connected.
Proof: Follows by definition.
17/124 CR09
Definitions and some problemsBasic algorithms
http://find/http://goback/ -
8/3/2019 Lecture Graphs
18/124
Basic algorithmsQuestions
Definitions: Fully indecomposability
Fully indecomposable matrix: There is no permutation matrices P and Qsuch that
PAQ=
A11 A12O A22
,
with the same condition on the blocks and their sizes as above.
Theorem: An n n square matrix A is fully indecomposable iff for somepermutation matrix P, the matrix PA is irreducible and has a zero-freemain 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 withzero-free main diagonal. B is fully indecomposable iffA is (why?).Therefore we may assume that A is irreducible and has a zero-freediagonal. Suppose, for the sake of contradiction, A is not fullyindecomposable.
18/124 CR09
Definitions and some problemsBasic algorithms
http://find/http://goback/ -
8/3/2019 Lecture Graphs
19/124
Basic algorithmsQuestions
Fully indecomposable matrices
Fully indecomposable matrix
There is no permutation matrices P and Q such that
PAQ= A11 A12
O A22 ,
with the same condition on the blocks and their sizes as above.
Proof cont.: Let P1AQ1 be of the form above with A11 of size r r. Wemay write P
1AQ
1= AQ, where A = P
1AP
1
T with zero-free diagonal(why?), and Q = P1Q1 is a permutation matrix which has to permute(why?) the first r columns among themselves, and similarly the last n rcolumns among themselves. Hence, A is in the above form, and A isreducible: contradiction. 2
19/124 CR09
Definitions and some problemsBasic algorithms
http://find/http://goback/ -
8/3/2019 Lecture Graphs
20/124
Basic algorithmsQuestions
Definitions: Cliques and independent sets
Clique
In an undirected graph G = (V,E), a set of vertices S V is a clique if for alls, t S, we have (s, t) E.
Maximum clique: A clique of maximum cardinality (finding a maximum cliquein an undirected graph is NP-complete).
Maximal clique: A clique is a maximal clique, if it is not contained in anotherclique.
In a symmetric matrix A, a clique corresponds to a subset of rows R and thecorresponding 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 toeach other. Can we find the largest one in polynomial time?
In a symmetric matrix A, an independent set corresponds to a subset of rows Rand the corresponding columns such that the matrix A(R,R) is either zero, ordiagonal.
20/124 CR09
Definitions and some problemsBasic algorithms
http://find/http://goback/ -
8/3/2019 Lecture Graphs
21/124
s c go t sQuestions
Definitions: More on cliques
Clique: In an undirected graph G = (V,E), a set of vertices S V is aclique if for all s, t S, we have (s, t) E.
In a symmetric matrix A corresponds to a subset of rows R and thecorresponding columns such that the matrix A(R,R) is full.
Cliques in bipartite graphs: Bi-cliquesIn a bipartite graph G = (U V,E), a pair of sets R,C where R Uand 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 columnsC such that the matrix A(R,C) is full.
The maximum node bi-clique problem asks for a bi-clique of maximumsize (e.g., |R| + |C|), and it is polynomial time solvable, whereasmaximum edge bi-clique problem (e.g., asks for a maximum |R| |C|) isNP-complete.
21/124 CR09
Definitions and some problemsBasic algorithms
http://find/http://goback/ -
8/3/2019 Lecture Graphs
22/124
gQuestions
Definitions: Hypergraphs
Hypergraph: A hypergraph H= (V,N) consists of a finite set V called the
vertex set and a set of non-empty subsets of vertices N called the hyperedgeset or the net set. A generalization of graphs.
For a matrix A, define a hypergraph whose vertices correspond to the rows andwhose nets correspond to the columns such that vertex vi is in net nj iffaij = 0(the column-net model).
A sample matrix
0@
1 2 3 4
1 2 3
1A
The column-net hypergraph model
4
3n
1n
1v
2v
3v
n
n2
v
2n
4n
3n
1n
1
2
3
v
v
22/124 CR09
Definitions and some problemsBasic algorithms
Breadth-first searchDepth-first searchTopological sort
http://find/http://goback/ -
8/3/2019 Lecture Graphs
23/124
QuestionsTopological sortStrongly connected components
Basic graph algorithms
Searching a graph: Systematically following the edges of the graph so asto visit all the vertices.
Breadth-first search,
Depth-first search.
Topological sort (of a directed acyclic graph): It is a linear ordering of allthe vertices such that if (u, v) directed is an edge, then u appears beforev in the ordering.
Strongly connected components (of a directed graph; why?): Recall thata strongly connected component is a maximal set of vertices for whichevery pair its vertices are reachable. We want to find 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
Definitions and some problemsBasic algorithms
Q i
Breadth-first searchDepth-first searchTopological sort
http://www.cs.bilkent.edu.tr/~aykanat/teaching.htmlhttp://www.cs.bilkent.edu.tr/~aykanat/teaching.htmlhttp://www.cs.bilkent.edu.tr/~aykanat/teaching.htmlhttp://www.cs.bilkent.edu.tr/~aykanat/teaching.htmlhttp://find/http://goback/ -
8/3/2019 Lecture Graphs
24/124
QuestionsTopological sortStrongly connected components
Breadth-first search: Idea
Graph G =(V,E),directed or undirected with adjacency list repres.
GOAL: Systematically explores edges ofG to
discover every vertex reachable from the source vertex s
compute theshortest path distance of every vertex
from the source vertexs
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
Definitions and some problemsBasic algorithms
Q ti
Breadth-first searchDepth-first searchTopological sort
http://find/http://goback/ -
8/3/2019 Lecture Graphs
25/124
Questionsp g
Strongly connected components
Breadth-first search: Key components
Maintains the following fields for each u ! V
color[u]:color ofu
"WHITE : not discovered yet
"GRAY : discovered and to be or being processed
"BLACK: discovered and processed
#[u]: parent ofu (NIL ofu = s or u is not discovered yet)
d[u]: distance ofu from s
Processing a vertex = scanning its adjacency list
25/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sort
http://find/http://goback/ -
8/3/2019 Lecture Graphs
26/124
Questionsp g
Strongly connected components
Breadth-first search: Algorithm
BFS(G, s)for each u ! V"{s} do
color[u]#WHITE$[u]# NIL; d[u]#%
color[s]# GRAY$[s]# NIL; d[s]# 0
Q# {s}while Q & ' do
u# head[Q]for each v inAdj[u] do
ifcolor[v] = WHITE thencolor[v]# GRAY
$[v]# ud[v]# d[u] + 1ENQUEUE(Q, v)
DEQUEUE(Q)color[u]# BLACK
26/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sort
http://find/http://goback/ -
8/3/2019 Lecture Graphs
27/124
QuestionsStrongly connected components
Breadth-first search: Example
Sample Graph:
a
b
g
c
f
d
e
h
i
0
sFIFO just after
queue Q processing vertex
!a" -
27/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortS l d
http://find/http://goback/ -
8/3/2019 Lecture Graphs
28/124
QuestionsStrongly connected components
Breadth-first search: Example
a
b
g
c
f
d
e
h
i
0
1
1
sFIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
28/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortSt l t d t
http://find/http://goback/ -
8/3/2019 Lecture Graphs
29/124
QStrongly connected components
Breadth-first search: Example
a
b
g
c
f
d
e
h
i
0
1
1
2
sFIFO just after
queue Q processing vertex
!a" -
!a,b,c" a
!a,b,c,f" b
29/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
30/124
Strongly connected components
Breadth-first search: Example
a
b
g
c
f
d
e
h
i
0
1
1
2
2
sFIFO 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
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
31/124
Strongly connected components
Breadth-first search: Example
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
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
32/124
Strongly connected components
Breadth-first search: Example
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
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
33/124
g y p
Breadth-first search: Example
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
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
34/124
g y p
Breadth-first search: Example
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
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
35/124
Breadth-first search: Example
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
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
36/124
Breadth-first search: Example
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
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
37/124
Breadth-first search: Analysis
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
processinggray vertices: O(E)
" each vertex is processed at most once and
!#
!=
Vu
EuAdj )(|][|
37/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
38/124
Breadth-first search: The paths to the root
BFS(G, s) , where V!={v " V: ![v] # NIL}${s} andE
! ={(![v],v) "E: v " V
!%{s}}
is a breadth-firsttree such that
% V!
consistsof all vertices in Vthat 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)
ifv = s then print selse 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
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
39/124
Breadth-first search: The BFS tree
Breadth-First Tree Generated by BFS
a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
3 3
3
3
a
b
g
c
f
d
e
h
i
0
1
1
2
2
s
3 3
3
3
BFS(G,a) terminated BFT generated by BFS(G,a)
39/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
40/124
Depth-first search: Idea
# #
#
7 8,601#8!9:;4,()*(>#?,#+@>4,()*(>
7 A>B6)(@)C#D45*#,(0,(5(@*6*4?@
7 8?6DE#"C5*(F6*4)6DDC#(G0D?,(#(H(,C#H(,*(G#6@>#
(H(,C#(>I(
7 J>(6E#5(6,)1#>((0(,#K1(@(H(,#0?554LD(
M N54@I#6#'J3O#P+(+(#9"*6)QR#3J3O#P+(+(#+5(>#4@#S3"=
40/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
41/124
Depth-first search: Key components
# #
#
6 7538*5384#4(9(,5:#;3(:
6 '3?(#@2"A#)=:=,4 *0(#9(,*3)(4#*=#38
-
8/3/2019 Lecture Graphs
42/124
Depth-first search: Key components
# #
#
6 7893:(#;2",#?,5/0#@!
/,>=+)(=#AB#
.2"#C>,D4#4/58838?#C>,(4*
6 @!"EF,(4*#E.22H#34#)>D/>4(=#>C#
=34Q>38*#=(/*01C3,4*#*,((4#E.2R4H
42/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
http://find/http://goback/ -
8/3/2019 Lecture Graphs
43/124
Depth-first search: Key components
# #
#
7 /3"#6859#*4:(5*6:05#(6)1#;(,*(?!@A#,()9,>5#=1(B#! 45#C4,5*#>45)9;(,(>#6B>#D,6E(>
7 C?!@A#,()9,>5#=1(B#! 45#C4B451(>#6B>#F86)G(B(>
7 "4B)(#*1(,(#45#9B8E#9B(#>45)9;(,E#(;(B*#6B>#C4B4514BD#(;(B*#C9,#(6)1#;(,*(?!@#" C?!@! HIJI
- H >?!@ C?!@ HIJI
K4:(#6
-
8/3/2019 Lecture Graphs
44/124
Depth-first search: Algorithm
# #
#
!"#789
$%&'()*+ !!:#,%);" "#$%&
#=!>#" ?@'%$'&" A
$%&'()*+ !!:#,%
-$'); $ "#$%& .+(/
!"#012#23485'!6
!"#012#23485'!6
);" ()*+
B=!>" %$'&" %$'& %-
$%&'()*+ ,! CBD=!>#,%-$'); $ "#$%& .+(/#=,>#" !!"#012#23485',6
);" -.*/0E=!>" %$'&" %$'& %-
44/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
D h fi h A l i
http://find/http://goback/ -
8/3/2019 Lecture Graphs
45/124
Depth-first search: Analysis
# #
#
6 7+88389#*3:(;#!S#*C*5A#)C4*#CG#(E()+*389#ACC/#CG#
!"#$%&'(34#!?
45/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
D h fi h E l
http://find/http://goback/ -
8/3/2019 Lecture Graphs
46/124
Depth-first search: Example
# #
# #
! " #
$%
& ' (
46/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
D th fi t h E l
http://find/http://goback/ -
8/3/2019 Lecture Graphs
47/124
Depth-first search: Example
# #
# #
! " #
$%
& ' (
!
47/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
D th fi t h E l
http://find/http://goback/ -
8/3/2019 Lecture Graphs
48/124
Depth-first search: Example
# #
# #
! " #
$%
& ' (
!
"
48/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
49/124
Depth-first search: Example
# #
# #
! " #
$ %
& ' (
!
"
#
49/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
50/124
Depth-first search: Example
# #
# #
! " #
$ %
& ' (
!
"
#
50/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
51/124
Depth-first search: Example
# #
# #
! " #
$ %
& ' (
!
"
# $
51/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
52/124
Depth-first search: Example
# #
# #
! " #
$ %
& ' (
!
"
# $ %
52/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
53/124
Depth-first search: Example
# #
# #
! " #
$ %
& ' (
!
"
# $ %
53/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
54/124
Depth-first search: Example
# #
# #
! " #
$ %
& ' (
!
"
# $ % &
54/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
55/124
Depth first search: Example
# #
# #
! " #
$ %
& ' (
!
"
# $ % &
'
55/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
56/124
Depth first search: Example
# #
# #
! " #
$ %
& ' (
!
"
# $ % &
'
(
56/124 CR09
Definitions and some problemsBasic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
57/124
Depth first search: Example
# #
# #
! " #
$ %
& ' (
!
"
# $ % &
'
(
57/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
58/124
Depth first search: Example
# #
# #
! " #
$ %
& ' (
!
"
# $ % &
'
(
)
58/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
59/124
pt st s c p
# #
# #
! " #
$ %
& ' (
!
"
# $ % &
'
(
)
59/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
60/124
p p
# #
# #
! " #
$ %
& ' (
!
"
# $ % &
'
(
) !*
60/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
61/124
p p
# #
# #
! " #
$ %
& ' (
!
"
# $ % &
' ( !)
* !!
61/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
62/124
p p
# #
# #
! " #
$ %
& ' (
!
" # $ %
& ' ()
* ((( (!
62/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
63/124
# #
# #
! " #
$ %
& ' (
!
" # $ %
& ' ()
* ((( (! ("
63/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
64/124
# #
# #
! " #
$ %
& ' (
!
" # $ %
& ' ()
* ((( (! ("
64/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
65/124
# #
# #
! " #
$ %
& ' (
!
" # $ %
& ' ()
* ((( (! ("
65/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
66/124
# #
# #
! " #
$ %
& ' (
!
" # $ %
& ' ()
* ((( (! ("
(#
66/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
67/124
# #
# #
! " #
$ %
& ' (
!
" # $ %
& ' ()
* ((( (! ("
(#
67/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
68/124
# #
# #
! " #
$ %
& ' (
!
" # $ %
& ' ()
* ((( (! ("
(# ($
68/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Example# #
http://find/http://goback/ -
8/3/2019 Lecture Graphs
69/124
# #
! " #
$ %
& ' (
!
" # $ %
& ' ()
* ((( (!
(# ($
(" (%
69/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: DFT and DFF
http://find/http://goback/ -
8/3/2019 Lecture Graphs
70/124
# #
# #
! " #
$ %
& ' (
!
" # $ %
& ' ()
* ((( (!
(# ($
(" (%
! " #
$ %
& ' (
!
" # $ %
& ' ()
* ((( (!
(# ($
(" (%
/3"#*(,:4?6*(@ /(0*12A4,5*#AB,(5*#
70/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Parenthesis theorem
http://find/http://goback/ -
8/3/2019 Lecture Graphs
71/124
Thm: In any DFS ofG=(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 ofu in a DFT
int[u] is entirely contained in int[v] and
u is a descendant ofv in a DFT
71/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Parenthesis theorem
http://find/http://goback/ -
8/3/2019 Lecture Graphs
72/124
ParenthesisTheorem
x y z
s t
w v u
2
3 4 5 6
7 9 1 0
8 1 11 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
Definitions and some problems
Basic algorithmsQuestions
Breadth-first search
Depth-first searchTopological sortStrongly connected components
Depth-first search: Edge classification
http://find/http://goback/ -
8/3/2019 Lecture Graphs
73/124
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
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Depth-first search: Edge classification
http://find/http://goback/ -
8/3/2019 Lecture Graphs
74/124
How to decide which GRAY to BLACK edgesare forward, which are cross
Let BLACK vertex v !Adj[u] is encounteredwhile processing GRAY vertex u
(u,v) isaforward edge ifd[u] > d[v]
74/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
75/124
x y z
s t
w v u
1
75/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
76/124
x y z
s t
w v u
1
2
T
76/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
77/124
x y z
s t
w v u
1
2
3
T
T
77/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
78/124
x y z
s t
w v u
1
2
3
T
T
B
78/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
79/124
x y z
s t
w v u
1
2
3 4
T
T
B
79/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
80/124
x y z
s t
w v u
1
2
3 4 5
T
T
T
B
80/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
81/124
x y z
s t
w v u
1
2
3 4 5
T
T
T
B
C
81/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
82/124
x y z
s t
w v u
1
2
3 4 5 6
T
T
T
B
C
82/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
83/124
x y z
s t
w v u
1
2
3 4 5 6
7
T
T
T
B
C
83/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
84/124
x y z
s t
w v u
1
2
3 4 5 6
7
8T
T
T
TB
C
84/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
85/124
x y z
s t
w v u
1
2
3 4 5 6
7
8T
T
T
T
B
C
C
85/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
86/124
x y z
s t
w v u
1
2
3 4 5 6
7
8
9
T
T
T
T
TB
C
C
86/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
87/124
x y z
s t
w v u
1
2
3 4 5 6
7
8
9
T
T
TT
T
C
B C
C
87/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
88/124
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
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
89/124
x y z
s t
w v u
1
2
3 4 5 6
7 9 10
8 11
T
T
T
T
TB
C
C
C
F
89/124 CR09
Definitions and some problems
Basic algorithmsQuestions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
90/124
x y z
s t
w v u
2
3 4 5 6
7 9 10
8 111 12T
T
TT
T
B
F
C
C
C
90/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
91/124
x y z
s t
w v u
2
3 4 5 6
7 9 10
8 111 12 13
T
T
T
T
T
B C
F
C
C
91/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
92/124
x y z
s t
w v u
2
3 4 5 6
7 9 10
8 111 12 13
T
T
T
T
T
C
BF
C
C
C
92/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
93/124
x y z
s t
w v u
2
3 4 5 6
7 9 10
8 111 12 13
T
T
T
TT
B
C
F
C
C
C
C
93/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
94/124
x y z
s t
w v u
2
3 4 5 6
7 9 10
8 111 12 13
14
T
T
T
T
T
B
F
C
CC
C
C
T
94/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
95/124
x y z
s t
w v u
2
3 4 5 6
7 9 10
8 111 12 13
14
C
T
T
T
T
T
T
B
F
C
CC
C
C
95/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Edge classification example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
96/124
x y z
s t
w v u
2
3 4 5 6
7 9 10
8 111 12
14 15
13 16T
T
T
TT
T
C
F
B CC
C
C
C
96/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Undirected graphs
Edge classification
http://find/http://goback/ -
8/3/2019 Lecture Graphs
97/124
g
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/86/7
9/10
4/11
13/14
15/18
i
j
h
g
c f
e d
a
b
97/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Depth-first search: Non-recursive algorithm
http://find/http://goback/ -
8/3/2019 Lecture Graphs
98/124
[, d, f]=DFS(G, v)
top 1stack(top) vd(v) ctime 1while top> 0 dou stack(top)
if there is a vertex w Adj(u) where (w) is not set thentop top+ 1stack(top) w(w) ud(w) ctime ctime+ 1
elsef(u) ctime ctime+ 1top top 1
98/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Topological sort
http://find/http://goback/ -
8/3/2019 Lecture Graphs
99/124
Topological sort (of a directed acyclic graph): It is a linear ordering of allthe 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
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Topological sort: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
100/124
3/4
shortsocks watch
pants shoes shirt
tiebelt jacket
pantsundershort
socks shoes watch shirt belt tie jacket
6/7 3/4
11/16 17/18 9/10
13/14
2/5
1/812/15
17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5
under
100/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Topological sort: Algorithm
The algorithm
http://find/http://find/http://goback/ -
8/3/2019 Lecture Graphs
101/124
run DFS(G)when a vertex is finished, 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 ofv during exploring the edge (u, v), where uis Gray. 2
v cannot be Gray (otherwise a Back edge in an acyclic graph !!!).Ifv is White, then u is an ancestor ofv, hence f[u] > f[v].
Ifv is Black, f[v] is computed already, f[u] is going to be computed,hence f[u] > f[v].
101/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Strongly connected components (SCC)
http://find/http://goback/ -
8/3/2019 Lecture Graphs
102/124
The strongly connected components of a directed graph are theequivalence classes of vertices under the are mutually reachablerelation.
For a graph G = (V,E), the transpose is defined as GT
= (V,ET
),where ET = {(u, v) : (v, u) E}.
Constructing GT from G takes O(V + E) time with adjacency list (likethe CSR or CSC storage format for sparse matrices) representation.
Notice that G and GT have the same SCCs.
102/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Strongly connected components: Algorithm
http://find/http://goback/ -
8/3/2019 Lecture Graphs
103/124
(1) Run DFS(G) to compute finishing times for all u!V
(2) Compute GT
(3) Call DFS(GT) processing vertices in main loop indecreasing f[u] computed in Step (1)
(4) Output vertices of each DFT in DFF of Step (3) as a
separate SCC
103/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Strongly connected components: Analysis
Lemma 1: no path between a pair of vertices in
http://find/http://goback/ -
8/3/2019 Lecture Graphs
104/124
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
wSCC
104/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Strongly connected components: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
105/124
a b c
e
d
f g h
105/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Strongly connected components: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
106/124
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
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first search
Topological sortStrongly connected components
Strongly connected components: Example
http://find/http://goback/ -
8/3/2019 Lecture Graphs
107/124
a b c
e
d
f g h
1
1
10
2 73 4 5 6
8 9
(1)Run DFS(G) to compute finishing times for all u!V
107/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Strongly connected components: Example
!
http://find/http://goback/ -
8/3/2019 Lecture Graphs
108/124
a b c
e
d
f g h
1
2
10
2 73 4 5 6
8 911
(1)Run DFS(G) to compute finishing times for allu!
V
108/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Strongly connected components: Example
2 1
http://find/http://goback/ -
8/3/2019 Lecture Graphs
109/124
a b c
e
d
f g h
1 10
2 73 4 5 6
8 916111413
1512
Vertices sorted according to the finishing times:
!!!!b,e,a,c,d,g,h,f""""
109/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Strongly connected components: Example
T
http://find/http://goback/ -
8/3/2019 Lecture Graphs
110/124
a b c
e
d
f g h
(2)Compute GT
110/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Strongly connected components: Example
T
http://find/http://goback/ -
8/3/2019 Lecture Graphs
111/124
a b c
e
d
f g h
(3) Call DFS(GT
) processing vertices in main loop indecreasingf[u] order: !!!!b,e,a,c,d,g,h,f""""
111/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Strongly connected components: Example
T
http://find/http://goback/ -
8/3/2019 Lecture Graphs
112/124
a b c
e
d
f g h
r1=
(3) Call DFS(G ) processing vertices in main loop indecreasingf[u] order: !!!!b,e,a,c,d,g,h,f""""
112/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Strongly connected components: Example
DFS T
http://find/http://goback/ -
8/3/2019 Lecture Graphs
113/124
a b c
e
d
f g h
r1=
(3) CallDFS
(G ) processing vertices in main loop indecreasingf[u] order: !!!!b,e,a,c,d,g,h,f""""
113/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Strongly connected components: Example
T
http://find/http://goback/ -
8/3/2019 Lecture Graphs
114/124
a b c
e
d
f g h
r1
= r2
=
(3) Call DFS(G ) processing vertices in main loop indecreasingf[u] order: !!!!b,e,a,c,d,g,h,f""""
114/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sortStrongly connected components
Strongly connected components: Example
T
http://find/http://goback/ -
8/3/2019 Lecture Graphs
115/124
a b c
e
d
f g h
r1
= r2
=
(3) Call DFS(G ) processing vertices in main loop indecreasingf[u] order: !!!!b,e,a,c,d,g,h,f""""
115/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sort
Strongly connected components
Strongly connected components: Example
DFS T
http://find/http://goback/ -
8/3/2019 Lecture Graphs
116/124
a b c
e
d
f g h
r1
= r2
=
r3
=
(3) CallDFS
(G ) processing vertices in main loop indecreasingf[u] order: !!!!b,e,a,c,d,g,h,f""""
116/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sort
Strongly connected components
Strongly connected components: Example
(3) Call DFS(GT) processing vertices in main loop in
http://find/http://goback/ -
8/3/2019 Lecture Graphs
117/124
a b c
e
d
f g h
r1
= r2
=
r3
=
(3) Call DFS(G ) processing vertices in main loop in
decreasingf[u] order: !!!!b,e,a,c,d,g,h,f""""
117/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sort
Strongly connected components
Strongly connected components: Example
(3) Call DFS(GT) processing vertices in main loop in
http://find/http://goback/ -
8/3/2019 Lecture Graphs
118/124
a b c
e
d
f g h
r1
= r2
=
r3
= r4
=
( ) ( ) p g p
decreasingf[u] order: !!!!b,e,a,c,d,g,h,f""""
118/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sort
Strongly connected components
Strongly connected components: Example
(4) Output vertices of each DFT in DFF as a separate SCC
http://find/http://goback/ -
8/3/2019 Lecture Graphs
119/124
a b c
e
d
fg
h
r1
= r2
=
r3
= r4
=
Cb={b,a,e}Cg={g,f} Ch={h}
Cc={c,d}
119/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sort
Strongly connected components
Strongly connected components: Example
a b c d
http://find/http://goback/ -
8/3/2019 Lecture Graphs
120/124
e f g h
hf,g
c,d
a,b,e
Acyclic component
graph
Cb Cg
Cc
Ch
120/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sort
Strongly connected components
Strongly connected components: Observations
http://find/http://goback/ -
8/3/2019 Lecture Graphs
121/124
In any DFS(G), all vertices in the same SCC are placed in the sameDFT.
In the DFS(G) step of the algorithm, the last vertex finished in an
SCC is the first vertex discovered in the SCC.
Consider the vertex r with the largest finishing time. It is a root of aDFT. Any vertex that is reachable from r in GT should be in theSCC of r (why?)
121/124 CR09
Definitions and some problemsBasic algorithms
Questions
Breadth-first searchDepth-first searchTopological sort
Strongly connected components
SCC and reducibility
http://find/http://goback/ -
8/3/2019 Lecture Graphs
122/124
To detect if there exists a permutation matrix P such that
PAPT =
A11 A12O A22
,
where A11 is an r r submatrix, A22 is an (n r) (n r) submatrix,where 1 r< n:
run SCC on the directed graph ofA to identify each strongly connectedcomponent as an irreducible block (more than one SCC?). Hence A11,
too, can be in that form (how many SCCs?).
122/124 CR09
Definitions and some problemsBasic algorithms
Questions
Could not get enough of it: Questions
http://find/http://goback/ -
8/3/2019 Lecture Graphs
123/124
How would you describe the following in the language of graphs
the structure ofPAPT for a given square sparse matrix A and apermutation matrix P,
the structure ofPAQ for a given square sparse matrix A and two
permutation matrices P and Q,
the structure ofAk, for k> 1,
the structure ofAAT,
the structure of the vector b, where b= Ax for a given sparsematrix A, and a sparse vector x.
123/124 CR09
Definitions and some problemsBasic algorithms
Questions
Could not get enough of it: Questions
http://find/http://goback/ -
8/3/2019 Lecture Graphs
124/124
Can you define:
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
http://find/http://goback/