Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
How to Win Coding Competitions: Secrets of Champions
Week 4: Algorithms on GraphsLecture 5: Topological sort
Maxim BuzdalovSaint Petersburg 2016
Intuition
Assume you are solving a problem from a competition.What should you do?
And in which order?
Read the statement
Write the solution template
Get the names of I/O files
Invent the algorithmWrite the solution
Submit the solution
2 / 7
Intuition
Assume you are solving a problem from a competition.What should you do?
And in which order?
Read the statement
Write the solution template
Get the names of I/O files
Invent the algorithmWrite the solution
Submit the solution
2 / 7
Intuition
Assume you are solving a problem from a competition.What should you do?And in which order?
Read the statement
Write the solution template
Get the names of I/O files
Invent the algorithmWrite the solution
Submit the solution
2 / 7
Intuition
Assume you are solving a problem from a competition.What should you do?And in which order?
Read the statement
Write the solution template
Get the names of I/O files
Invent the algorithmWrite the solution
Submit the solution
2 / 7
Topological sort
For the directed acyclic graph G = 〈V ,E 〉,a topological sort is an assignment I : V → [1;N] of indices to vertices, such that:
I for every two distinct vertices u, v it holds that I (u) 6= I (v)I for each edge (u, v) ∈ E it holds that I (u) < I (v)
Sometimes it is convenient to have an array o[i ] such that:
I o[I (v)] = v , or, just the same, I (o[i ]) = i
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
3 / 7
Topological sort
For the directed acyclic graph G = 〈V ,E 〉,a topological sort is an assignment I : V → [1;N] of indices to vertices, such that:
I for every two distinct vertices u, v it holds that I (u) 6= I (v)I for each edge (u, v) ∈ E it holds that I (u) < I (v)
Sometimes it is convenient to have an array o[i ] such that:
I o[I (v)] = v , or, just the same, I (o[i ]) = i
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
3 / 7
Topological sort
For the directed acyclic graph G = 〈V ,E 〉,a topological sort is an assignment I : V → [1;N] of indices to vertices, such that:
I for every two distinct vertices u, v it holds that I (u) 6= I (v)I for each edge (u, v) ∈ E it holds that I (u) < I (v)
Sometimes it is convenient to have an array o[i ] such that:
I o[I (v)] = v , or, just the same, I (o[i ]) = i
6
2
3
1
5
8
9
4
7
10
12
13
14
11
16
15
3 / 7
Topological sort
For the directed acyclic graph G = 〈V ,E 〉,a topological sort is an assignment I : V → [1;N] of indices to vertices, such that:
I for every two distinct vertices u, v it holds that I (u) 6= I (v)I for each edge (u, v) ∈ E it holds that I (u) < I (v)
Sometimes it is convenient to have an array o[i ] such that:
I o[I (v)] = v , or, just the same, I (o[i ]) = i
4
2
3
1
7
8
9
6
5
10
12
13
14
11
15
16
3 / 7
Topological sort
For the directed acyclic graph G = 〈V ,E 〉,a topological sort is an assignment I : V → [1;N] of indices to vertices, such that:
I for every two distinct vertices u, v it holds that I (u) 6= I (v)I for each edge (u, v) ∈ E it holds that I (u) < I (v)
Sometimes it is convenient to have an array o[i ] such that:
I o[I (v)] = v , or, just the same, I (o[i ]) = i
4
2
3
1
7
8
9
6
5
10
12
13
14
11
15
16
3 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
16
P
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
16
P
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
M
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
L
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
K
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
J
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
G
H
I
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
10
H
I
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
F
10
H
I
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
9
10
H
I
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
A
B
C
D
E
9
10
H
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
B
C
D
E
9
10
H
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
B
C
D
E
9
10
H
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
B
C
D
E
9
10
H
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
B
C
D
E
9
10
H
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
B
C
D
E
9
10
H
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
B
C
D
6
9
10
H
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
B
C
D
6
9
10
5
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
B
4
D
6
9
10
5
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
3
4
D
6
9
10
5
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
3
4
D
6
9
10
5
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
3
4
2
6
9
10
5
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
3
4
2
6
9
10
5
8
11
12
13
14
N
16
15
4 / 7
Topological sort using Depth First Search
procedure TopoSort(V , E )U ← ∅, I ← ∅, t ← |V |, o ← {}A(v) = {u | (v , u) ∈ E}for v ∈ V do
if v /∈ U then DFS(v , A) end ifend for
end procedure
procedure DFS(v , A)U ← U ∪ vfor u ∈ A(v) do
if u /∈ U then DFS(u, A) end ifend forI (v)← t, o[t]← v , t ← t − 1
end procedure
7
3
4
2
6
9
10
5
8
11
12
13
14
1
16
15
4 / 7
Topological sort and graphs with cycles
What will happen if topological sort is called on a graph with cycles?
I DFS will eventually find a cycle
I It may return an error which indicates that there is no topological sortI But what if we ignore it?
I Every two vertices u and v , where u is reachable from v , but not vice versa,will have I (u) > I (v)
I Condensation of a graph G : a graph whose vertices are strong connectivitycomponents of G and whose edges are the edges between vertices from differentcomponents
I Condensation is a treeI . . . and this tree will be traversed in a correct order by topological sort!I So we may use topological sort to find the strong connectivity components
5 / 7
Topological sort and graphs with cycles
What will happen if topological sort is called on a graph with cycles?
I DFS will eventually find a cycle
I It may return an error which indicates that there is no topological sortI But what if we ignore it?
I Every two vertices u and v , where u is reachable from v , but not vice versa,will have I (u) > I (v)
I Condensation of a graph G : a graph whose vertices are strong connectivitycomponents of G and whose edges are the edges between vertices from differentcomponents
I Condensation is a treeI . . . and this tree will be traversed in a correct order by topological sort!I So we may use topological sort to find the strong connectivity components
5 / 7
Topological sort and graphs with cycles
What will happen if topological sort is called on a graph with cycles?
I DFS will eventually find a cycle
I It may return an error which indicates that there is no topological sort
I But what if we ignore it?
I Every two vertices u and v , where u is reachable from v , but not vice versa,will have I (u) > I (v)
I Condensation of a graph G : a graph whose vertices are strong connectivitycomponents of G and whose edges are the edges between vertices from differentcomponents
I Condensation is a treeI . . . and this tree will be traversed in a correct order by topological sort!I So we may use topological sort to find the strong connectivity components
5 / 7
Topological sort and graphs with cycles
What will happen if topological sort is called on a graph with cycles?
I DFS will eventually find a cycle
I It may return an error which indicates that there is no topological sortI But what if we ignore it?
I Every two vertices u and v , where u is reachable from v , but not vice versa,will have I (u) > I (v)
I Condensation of a graph G : a graph whose vertices are strong connectivitycomponents of G and whose edges are the edges between vertices from differentcomponents
I Condensation is a treeI . . . and this tree will be traversed in a correct order by topological sort!I So we may use topological sort to find the strong connectivity components
5 / 7
Topological sort and graphs with cycles
What will happen if topological sort is called on a graph with cycles?
I DFS will eventually find a cycle
I It may return an error which indicates that there is no topological sortI But what if we ignore it?
I Every two vertices u and v , where u is reachable from v , but not vice versa,will have I (u) > I (v)
I Condensation of a graph G : a graph whose vertices are strong connectivitycomponents of G and whose edges are the edges between vertices from differentcomponents
I Condensation is a treeI . . . and this tree will be traversed in a correct order by topological sort!I So we may use topological sort to find the strong connectivity components
5 / 7
Topological sort and graphs with cycles
What will happen if topological sort is called on a graph with cycles?
I DFS will eventually find a cycle
I It may return an error which indicates that there is no topological sortI But what if we ignore it?
I Every two vertices u and v , where u is reachable from v , but not vice versa,will have I (u) > I (v)
I Condensation of a graph G : a graph whose vertices are strong connectivitycomponents of G and whose edges are the edges between vertices from differentcomponents
I Condensation is a treeI . . . and this tree will be traversed in a correct order by topological sort!I So we may use topological sort to find the strong connectivity components
5 / 7
Topological sort and graphs with cycles
What will happen if topological sort is called on a graph with cycles?
I DFS will eventually find a cycle
I It may return an error which indicates that there is no topological sortI But what if we ignore it?
I Every two vertices u and v , where u is reachable from v , but not vice versa,will have I (u) > I (v)
I Condensation of a graph G : a graph whose vertices are strong connectivitycomponents of G and whose edges are the edges between vertices from differentcomponents
I Condensation is a tree
I . . . and this tree will be traversed in a correct order by topological sort!I So we may use topological sort to find the strong connectivity components
5 / 7
Topological sort and graphs with cycles
What will happen if topological sort is called on a graph with cycles?
I DFS will eventually find a cycle
I It may return an error which indicates that there is no topological sortI But what if we ignore it?
I Every two vertices u and v , where u is reachable from v , but not vice versa,will have I (u) > I (v)
I Condensation of a graph G : a graph whose vertices are strong connectivitycomponents of G and whose edges are the edges between vertices from differentcomponents
I Condensation is a treeI . . . and this tree will be traversed in a correct order by topological sort!
I So we may use topological sort to find the strong connectivity components
5 / 7
Topological sort and graphs with cycles
What will happen if topological sort is called on a graph with cycles?
I DFS will eventually find a cycle
I It may return an error which indicates that there is no topological sortI But what if we ignore it?
I Every two vertices u and v , where u is reachable from v , but not vice versa,will have I (u) > I (v)
I Condensation of a graph G : a graph whose vertices are strong connectivitycomponents of G and whose edges are the edges between vertices from differentcomponents
I Condensation is a treeI . . . and this tree will be traversed in a correct order by topological sort!I So we may use topological sort to find the strong connectivity components
5 / 7
Condensation
procedure Condensation(V , E )TopoSort(V , E )AR(v) = {u | (v , u) ∈ E}C ← {−1}K ← 1for i from 1 to |V | do
if C [o[i ]] = −1 thenDFS(o[i ], AR , K )K ← K + 1
end ifend for
end procedure
procedure DFS(v , A, K )C [v ]← Kfor u ∈ A(v) do
if C [u] 6= −1 thenDFS(u, A, K )
end ifend for
end procedure
I Second DFS traverses vertices in theorder of topological sort
I . . . and does it using reversed edges
6 / 7
Condensation
procedure Condensation(V , E )TopoSort(V , E )AR(v) = {u | (v , u) ∈ E}C ← {−1}K ← 1for i from 1 to |V | do
if C [o[i ]] = −1 thenDFS(o[i ], AR , K )K ← K + 1
end ifend for
end procedure
procedure DFS(v , A, K )C [v ]← Kfor u ∈ A(v) do
if C [u] 6= −1 thenDFS(u, A, K )
end ifend for
end procedure
I Second DFS traverses vertices in theorder of topological sort
I . . . and does it using reversed edges
6 / 7
Condensation
procedure Condensation(V , E )TopoSort(V , E )AR(v) = {u | (v , u) ∈ E}C ← {−1}K ← 1for i from 1 to |V | do
if C [o[i ]] = −1 thenDFS(o[i ], AR , K )K ← K + 1
end ifend for
end procedure
procedure DFS(v , A, K )C [v ]← Kfor u ∈ A(v) do
if C [u] 6= −1 thenDFS(u, A, K )
end ifend for
end procedure
I Second DFS traverses vertices in theorder of topological sort
I . . . and does it using reversed edges
6 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
16
M
N
O
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
16
M
N
15
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
16
M
N
15
P
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
16
M
N
15
14
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
K
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
J
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
E
F
G
H
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
E
F
10
H
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
E
F
10
H
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
E
F
10
H
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
E
F
10
H
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
9
F
10
H
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
9
F
10
H
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
9
F
10
H
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
9
F
10
8
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
C
D
9
F
10
8
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
B
7
D
9
F
10
8
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
6
7
D
9
F
10
8
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
6
7
5
9
F
10
8
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
6
7
5
9
4
10
8
I
11
12
16
13
N
15
14
7 / 7
Example of condensation
A
6
7
5
9
4
10
8
3
11
12
16
13
N
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
N
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
N
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7
Example of condensation
2
6
7
5
9
4
10
8
3
11
12
16
13
1
15
14
7 / 7