c h a p ter 22: e l eme n tary g ra p ha lgo r i t h ms
DESCRIPTION
C h a p ter 22: E l eme n tary G ra p hA lgo r i t h ms. U ndi rected G ra p h. Information T echnology. E x ample: A city map for pedestrians (edges are streets, nodes are intersections) ABC D. E. F. G. J. I. H. K. L. M. N. D i rected Grap h(Digrap h ). - PowerPoint PPT PresentationTRANSCRIPT
Info
rma
tion
T
ech
no
log
yUndirected Graph
Example: A city map for pedestrians (edges are streets,nodes are intersections)
A B C D
2
E
F G I
H K L M N
J
Info
rma
tion
T
ech
no
log
yDirected Graph (Digraph)
F G I
H K L M N
J
Example: A city map for cars (edges are one-way streets,nodes are intersections)
A B CD
3
E
Info
rma
tion
T
ech
no
log
yDirected & Undirected Graphs
4
Node, vertex (plural: vertices) Edge Self-loop (edge from a node to itself) Adjacent nodes (connected by an edge) Degree (#edges from or to a node) In-degree (#edges to a node) Out-degree (#edges from a node) Path (sequence of adjacent nodes)
Info
rma
tion
T
ech
no
log
yRepresentations of Graphs
5
Adjacency-matrix representation:a 2-dimensional array A of 0/1 values, with A[i,j ] containing the number of arcsbetween nodes i and j (undirected graph),or from node i to node j (digraph)
Adjacency-list representation:a 1-dimensional array Adj of adjacency lists, with Adj [i ] containing the list of nodesadjacent to node i
Info
rma
tion
T
ech
no
log
y
A B C D E F G H I J K L M N
A 1 1 1 1
B 1 1
C 1 1 1 1
D 1 1 1
E 1 1 1
F 1 1
G 1 1 1
H 1 1 1
I 1 1
J 1 1 1 1
1
1
1B1 1 C
K
L 1
M 1 1
N 1 1
A D E
F G I
H K L M N6
J
A:
B:
C:
D:
E:
F:
G:
J:
I:
H:
K:
L:
M:
N:
B D G J
A C K
C D I K L M
A F K
JNote: For our convenience, each adjacency list is alphabetically ordered.
J N
E M
B F G H
G H J
E J
A H
D I N
C E J
A C
Info
rma
tion
T
ech
no
log
y
A B C D E F G H I J K L M N
A 1 1
B 1
C 1 1
D 1 1 1
E 1
F 1
G 1 1
H 1 1
I 1
J 1 1 1
K
B1 11
L
M 1
N 1
A C D E
F G I
H K L M N
J
A:
B:
C:
D:
E:
F:
G:
J:
I:
H:
K:
L:
M:
N:
B F
C
G J
N
H
A K
I L M
E
A K
Note: For our convenience, each adjacency list is alphabetically ordered.
N
M
7
G H J
C E J
Info
rma
tion
T
ech
no
log
yGraph Traversals
8
by breadth-first search (BFS) by depth-first search (DFS)
BFS and DFS work on directed graphs as well as on undirected graphs(the examples below are for digraphs).
BFS and DFS assume the given graph is represented using adjacency lists.
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
A:
B:
C:
D:
E:
F:
G:
J:
I:
H:
K:
L:
M:
N:
B F
C
G J
N
H
A K
I L M
E
A K
N
M
Given a source node, A. Paint the source gray. Paint other nodes white. Add the source to the initially empty first-in first-out (FIFO) queue of gray nodes.
K L M N11
G H J
C E J
BFS order (black) Queue (gray)A
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
A:
B:
C:
D:
E:
F:
G:
J:
Paint it black (all adjacent I:
H:
K:
L:
M:
N:
B F
C
G J
N
H
A K
I L M
E
A K
N
M
Dequeue head node, A. Paint its undiscovered (=white) adjacent nodes gray and enqueue them.
K L M N12
nodes are discovered).G H J
C E J
BFS order (black) Queue (gray)A BF
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
A:
B:
C:
D:
E:
F:
G:
J:
I:
H:
K:
L:
M:
N:
B F
C
G J
N
H
A K
I L M
E
A K
N
M
K L M N13
G H J
C E J
BFS order (black) Queue (gray)A BFAB FC
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
A:
B:
C:
D:
E:
F:
G:
J:
I:
H:
K:
L:
M:
N:
B F
C
G J
N
H
A K
I L M
E
A K
N
M
K L M N14
G H J
C E J
BFS order (black) Queue (gray)A BFAB FCABF CH
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
A:
B:
C:
D:
E:
F:
G:
J:
I:
H:
K:
L:
M:
N:
B F
C
G J
N
H
A K
I L M
E
A K
N
M
K L M N15
G H J
C E J
BFS order (black) Queue (gray)A BFAB FCABF CHABFC HGJ
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
A:
B:
C:
D:
E:
F:
G:
J:
I:
H:
K:
L:
M:
N:
B F
C
G J
N
H
A K
I L M
E
A K
N
M
K L M N16
G H J
C E J
BFS order (black) Queue (gray)A BFAB FCABF CHABFC HGJABFCH GJK
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
A:
B:
C:
D:
E:
F:
G:
J:
I:
H:
K:
L:
M:
N:
B F
C
G J
N
H
A ) ( K )I L M
E
A K
N
M
(
K L M N17
G H J
C E J
BFS order (black) Queue (gray)A BFAB FCABF CHABFC HGJABFCH GJKABFCHG JK
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
A:
B:
C:
D:
E:
F:
G:
J:
I:
H:
K:
L:
M:
N:
B F
C
G J
N
H
A K
I L M
E
A K
N
M
K L M N18
G H J
C E J
BFS order (black) Queue (gray)A BFAB FCABF CHABFC HGJABFCH GJKABFCHG JKABFCHGJ KILM
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
A:
B:
C:
D:
E:
F:
G:
J:
I:
H:
K:
L:
M:
N:
B F
C
G J
N
H
A K
I L M
E
A K
G ) ( H ) ( J
N
M
)(
and so on
K L M N19
C E J
BFS order (black) Queue (gray)A BFAB FCABF CHABFC HGJABFCH GJKABFCHG JKABFCHGJ KILMABFCHGJK ILM
Info
rma
tion
T
ech
no
log
y
A B C E
F G IJ
L:
M:
N:
N
M
D
K L M N20
May restart from white source D
G H J
C E J
A: B F
B: C
C: G JD:
E: N
F: H
G: A K
J: I L M
I: E
H: A KK:
BFS order (black) Queue (gray)A BFAB FCABF CHABFC HGJABFCH GJKABFCHG JKABFCHGJ KILMABFCHGJK ILMABFCHGJKI LMEABFCHGJKIL MEABFCHGJKILM ENABFCHGJKILME NABFCHGJKILMEN
Info
rma
tion
T
ech
no
log
y
Analysis of BFS for graph (V,E) (without any restarts)
Aggregate analysis: O(V+E) time.20
The initialisations take Θ(V) time. Each node is enqueued (in O(1) time) and
dequeued (in O(1) time) at most once (because no node is ever repainted white), hence O(V) time for all queue operations.
Each adjacency list is scanned at most once, and their total length is Θ(E), hence O(E) time for scanning adjacency lists.
Info
rma
tion
T
ech
no
log
y
Refinements of BFS (at no increase in time complexity)
22
Store the predecessor (or parent) of each newly discovered node(initially NIL, as undiscovered = white):breadth-first tree, rooted at source s.
Store the distance (in #edges) from the source s to each newly discovered node (by adding 1 to the distance of its parent, with s being at distance 0): lengths ofshortest paths from s to all reachable nodes.
Info
rma
tion
T
ech
no
log
y
Breadth-first tree from source A (without restart from D):
A B C E
F G I
H K L M N
J
23
Info
rma
tion
T
ech
no
log
y
A B C D E
F G I
H
J
K L M N28
Paint all nodes white.DFS finish order (black) Stack (gray) Rest (white)
ABCDEFGHIJKLMN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G I
H
J
K L M N29
The first white node, A, is the first source node.
Paint the next undiscovered (=white) adjacent node gray and push it onto a stack before continuing on it (recursive case);if no such adjacent node (or: “child”) is found, then pop the top node off the stack,paint it black (all adjacent nodes discovered), and stop (base case).
DFS finish order (black) Stack (gray) Rest (white)A BCDEFGHIJKLMN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G I
H
J
K L M N30
Continue to A’s first undiscovered child, B.DFS finish order (black) Stack (gray) Rest (white)
A BCDEFGHIJKLMNBA CDEFGHIJKLMN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N31
Continue to B’s first undiscovered child, C.DFS finish order (black) Stack (gray) Rest (white)
A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N32
Continue to C’s first undiscovered child, G.DFS finish order (black) Stack (gray) Rest (white)
A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
Start at AContinue to G’s first undiscovered child, K.
Ht
K L M N33
DFS finish order (black) Stack (gray) Rest (white)A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N30
Continue to K’s first undiscovered child, H.DFS finish order (black) Stack (gray) Rest (white)
A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N35
H has no undiscovered child, backtrack until we find node K, with undiscovered child J.
DFS finish order (black) Stack (gray) Rest (white)A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N36
Continue to J’s first undiscovered child, I.DFS finish order (black) Stack (gray) Rest (white)
A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N37
Continue to I’s first undiscovered child, E.DFS finish order (black) Stack (gray) Rest (white)
A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMNH EIJKGCBA DFLMN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N38
Continue to E’s first undiscovered child, N.DFS finish order (black) Stack (gray) Rest (white)
A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMNH EIJKGCBA DFLMNH NEIJKGCBA DFLM
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N39
Continue to N’s first undiscovered child, M.DFS finish order (black) Stack (gray) Rest (white)
A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMNH EIJKGCBA DFLMNH NEIJKGCBA DFLMH MNEIJKGCBA DFL
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
M has no undiscovered child, backtrack until we find node J, with undiscovered child L.
H K L M N40
DFS finish order (black) Stack (gray) Rest (white)A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMNH EIJKGCBA DFLMNH NEIJKGCBA DFLMH MNEIJKGCBA DFLHMNEI LJKGCBA DF
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
L has no undiscovered child, backtrack until we find node A, with undiscovered child F.
H K L M N41
DFS finish order (black) Stack (gray) Rest (white)A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMNH EIJKGCBA DFLMNH NEIJKGCBA DFLMH MNEIJKGCBA DFLHMNEI LJKGCBA DFHMNEILJKGCB FA D
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
F has no undiscovered child; the backtrack clears the stack.
H K L M N42
DFS finish order (black) Stack (gray) Rest (white)A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMNH EIJKGCBA DFLMNH NEIJKGCBA DFLMH MNEIJKGCBA DFLHMNEI LJKGCBA DFHMNEILJKGCB FA DHMNEILJKGCBFA D
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
Restart from next white node, D, as source.
H K L M N43
DFS finish order (black) Stack (gray) Rest (white)A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMNH EIJKGCBA DFLMNH NEIJKGCBA DFLMH MNEIJKGCBA DFLHMNEI LJKGCBA DFHMNEILJKGCB FA DHMNEILJKGCBFA D
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N40
D has no undiscovered child; backtrack; done.DFS finish order (black) Stack (gray) Rest (white)
A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMNH EIJKGCBA DFLMNH NEIJKGCBA DFLMH MNEIJKGCBA DFLHMNEI LJKGCBA DFHMNEILJKGCB FA DHMNEILJKGCBFA DHMNEILJKGCBFAD
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N41
DFS order (of pushing): ABCGKHJIENMLFDDFS finish order (black) Stack (gray) Rest (white)
A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMNH EIJKGCBA DFLMNH NEIJKGCBA DFLMH MNEIJKGCBA DFLHMNEI LJKGCBA DFHMNEILJKGCB FA DHMNEILJKGCBFA DHMNEILJKGCBFAD
Info
rma
tion
T
ech
no
log
y
Analysis of DFS for graph (V,E) (with restarts)
46
Initialisations and restarts take Θ(V) time. Each node is visited exactly once
(because no node is ever repainted white). Each adjacency list is scanned exactly
once, and their total length is Θ(E), hence Θ(E) time for scanning adjacency lists.
Aggregate analysis: Θ(V+E) time.
It takes time linear in the size of the graph.
Info
rma
tion
T
ech
no
log
y
Refinements of DFS (at no increase in time complexity)
47
Store the predecessor (or parent) of each newly discovered node(initially NIL, as undiscovered = white):depth-first forest of depth-first trees that are rooted at the chosen source nodes.
Store the timestamps of discovering (graying) and finishing (blackening) each node: useful in many graph algorithms (for example: topological sort, see below).
Info
rma
tion
T
ech
no
log
y
Depth-first forest (of depth-first trees)
A B C D E
F G IJ
H K L M N48
DFS finish order (black) Stack (gray) Rest (white)A BCDEFGHIJKLMNBA CDEFGHIJKLMNCBA DEFGHIJKLMNGCBA DEFHIJKLMNKGCBA DEFHIJLMNHKGCBA DEFIJLMN
H JKGCBA DEFILMNH IJKGCBA DEFLMNH EIJKGCBA DFLMNH NEIJKGCBA DFLMH MNEIJKGCBA DFLHMNEI LJKGCBA DFHMNEILJKGCB FA DHMNEILJKGCBFA DHMNEILJKGCBFAD
Info
rma
tion
T
ech
no
log
y
Depth-first
forest A B
C
F G
H
K
D E
I
L M N
J
Another depth-first forest, starting from D A B C D E
F G J I
H K LM N
Starting from L, then M, then J, then K, then
D A B C D E
F G J IH K L M N49
Info
rma
tion
T
ech
no
log
y
Strongly Connected Components of a Digraph
50
Strongly connected component (SCC): maximal set of nodes where there is a path from each node to each other node.
Many algorithms first divide a digraph into its SCCs, then process these SCCs separately, and finally combine the sub- solutions. (This is not divide-&-conquer!)
An undirected graph can be decomposed into its connected components.
Info
rma
tion
T
ech
no
log
y
Strongly Connected Components of a Digraph
Strongly connected component (SCC): maximal set of nodes where there is a path from each node to each other node.
51
A B C D E
F G IJ
H K L M N
Info
rma
tion
T
ech
no
log
y
Strongly Connected Components of a Digraph
Strongly connected component (SCC): maximal set of nodes where there is a path from each node to each other node.
52
A B C D E
F G IJ
K L M N
Info
rma
tion
T
ech
no
log
yComputing the Strongly Connected Components of a Digraph G
49
1. Enumerate the nodes of G in DFS finish order, starting from any node
2. Compute the transpose GT
(that is, reverse all edges)
3. Make a DFS in GT, considering nodes in reverse finish order from original DFS
4. Each tree in this depth-first forest is a strongly connected component
Info
rma
tion
T
ech
no
log
y
A B C D E
F G I
H K L M N
J
HMNEILJKGCBFAD
DAFBCGKJLIENMH
DFS finish order of G:
Reverse DFS finish order of G:
A B C D E
F G I
H K L M N
JG
50
GT
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N55
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N56
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMH
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N57
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMH
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N58
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMH
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N59
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMH
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N60
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMH
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N61
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENM
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N62
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENMD|BC FHKGA JLIENM
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N63
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENMD|BC FHKGA JLIENMD|BCFHKGA| J LIENM
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N60
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENMD|BC FHKGA JLIENMD|BCFHKGA| J LIENMD|BCFHKGA|J| L IENM
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N65
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENMD|BC FHKGA JLIENMD|BCFHKGA| J LIENMD|BCFHKGA|J| L IENMD|BCFHKGA|J|L I ENM
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N66
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENMD|BC FHKGA JLIENMD|BCFHKGA| J LIENMD|BCFHKGA|J L IENMD|BCFHKGA|J|L I ENMD|BCFHKGA|J|L|I E NM
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N67
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENMD|BC FHKGA JLIENMD|BCFHKGA| J LIENMD|BCFHKGA|J L IENMD|BCFHKGA|J|L I ENMD|BCFHKGA|J|L|I E NMD|BCFHKGA|J|L|I|E N M
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N68
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENMD|BC FHKGA JLIENMD|BCFHKGA| J LIENMD|BCFHKGA|J L IENMD|BCFHKGA|J|L I ENMD|BCFHKGA|J|L|I E NMD|BCFHKGA|J|L|I|E N MD|BCFHKGA|J|L|I|E MN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N69
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENMD|BC FHKGA JLIENMD|BCFHKGA| J LIENMD|BCFHKGA|J L IENMD|BCFHKGA|J|L I ENMD|BCFHKGA|J|L|I E NMD|BCFHKGA|J|L|I|E N MD|BCFHKGA|J|L|I|E MND|BCFHKGA|J|L|I|E|MN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
H K L M N70
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENMD|BC FHKGA JLIENMD|BCFHKGA| J LIENMD|BCFHKGA|J L IENMD|BCFHKGA|J|L I ENMD|BCFHKGA|J|L|I E NMD|BCFHKGA|J|L|I|E N MD|BCFHKGA|J|L|I|E MND|BCFHKGA|J|L|I|E|MN
Info
rma
tion
T
ech
no
log
y
A B C D E
F G IJ
K L M N71
DFS finish order (black) Stack (gray) Rest (white)D AFBCGKJLIENMH
D| A FBCGKJLIENMHD| GA FBCKJLIENMHD| CGA FBKJLIENMHD| BCGA FKJLIENMHD|BC KGA FJLIENMHD|BC HKGA FJLIENMD|BC FHKGA JLIENMD|BCFHKGA| J LIENMD|BCFHKGA|J L IENMD|BCFHKGA|J|L I ENMD|BCFHKGA|J|L|I E NMD|BCFHKGA|J|L|I|E N MD|BCFHKGA|J|L|I|E MND|BCFHKGA|J|L|I|E|MN
Info
rma
tion
T
ech
no
log
y
Analysis of decompositioninto SCCs of digraphG=(V,E)
A strongly connected digraph has one SCC.73
Assume G is represented by adjacency lists:
1.DFS in G: Θ(V+E) time, including reversal
2. Compute transpose GT: Θ(V+E) time
3. DFS in GT: Θ(V+E) time
4.Output SCCs: Θ(V+E) time
Total: Θ(V+E) time
Info
rma
tion
T
ech
no
log
yTOPOLOGICAL SORT
74
A topological sort is a linear ordering of all nodes of a directed acyclic graph (DAG).
If there is a path from node ni to node nj
then ni appears before nj in the ordering.
Note on DAGs:There is no cycle (path from node to itself)There is at least one node of in-degree 0There is at least one node of out-degree 0
Info
rma
tion
T
ech
no
log
yTOPOLOGICAL SORT
75
Example: We are given courses at a university and
the prerequisite relations between. These can be modelled as a DAG. A topological sort of these courses gives
a valid sequence of taking them.
Info
rma
tion
T
ech
no
log
yTOPOLOGICAL SORT
Intro IT
PKD AD2
DArkDig Av
Algo
AINode with in-degree 0
77
Info
rma
tion
T
ech
no
log
yTOPOLOGICAL SORT
Intro IT
PKD AD2
DArkDig Av
Algo
AINodes with out-degree 0
78
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Select node with in-degree 0
Print it out Remove it Repeat
Intro IT
PKD AD2
DArk Dig Av
Algo
AI
79
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Select IntroIT
Intro IT
PKD AD2
DArk Dig Av
Algo
AI
80
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Select IntroIT Print IntroIT
Intro IT
PKD AD2
DArk Dig Av
Algo
AI
IntroIT81
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Select IntroIT Print IntroIT Remove it
PKD AD2
DArk Dig Av
Algo
AI
IntroIT82
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Select PKD Print PKDPKD AD2
DArk Dig Av
Algo
AI
IntroIT, PKD80
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Select PKD Print PKD Remove it
AD2
DArk Dig Av
Algo
AI
IntroIT, PKD85
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Av Algo
Select DArkDig
Print DArkDig
AD2
DArk Dig
AI
IntroIT, PKD, DArkDig87
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Av Algo
Select DArkDig
Print DArkDig Remove it
AD2
AI
IntroIT, PKD, DArkDig
88
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Av Algo
Select AI
Print AIAD2
AI
IntroIT, PKD, DArkDig, AI90
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Av Algo
Select AI
Print AI Remove it
AD2
IntroIT, PKD, DArkDig, AI91
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Av Algo
Select AD2
Print AD2AD2
IntroIT, PKD, DArkDig, AI, AD293
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Av Algo
Select AD2
Print AD2
Remove it
90
IntroIT, PKD, DArkDig, AI, AD2
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Av Algo
Select AvAlgo
95
IntroIT, PKD, DArkDig, AI, AD2
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
Av Algo
Select AvAlgo
Print AvAlgo
96
IntroIT, PKD, DArkDig, AI, AD2, AvAlgo
Info
rma
tion
T
ech
no
log
y
TOPOLOGICAL SORT
97
Select AvAlgo Print AvAlgo Remove it
IntroIT, PKD, DArkDig, AI, AD2, AvAlgo
Info
rma
tion
T
ech
no
log
yTOPOLOGICAL SORT
98
A topological sort is not necessarily unique: At any step, we may have more than one node
with in-degree 0, so we have to choose one among them.
IntroIT, PKD, AI, DArkDig, AD2, AvAlgo is also a valid sequence.