CS 374: Algorithms & Models of Computation
Chandra Chekuri Manoj Prabhakaran
University of Illinois, Urbana-Champaign
Fall 2015
Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59
CS 374: Algorithms & Models of Computation Chandra Chekuri - - PowerPoint PPT Presentation
CS 374: Algorithms & Models of Computation Chandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59 Today Two topics: Structure of directed graphs DFS
University of Illinois, Urbana-Champaign
Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59
Chandra & Manoj (UIUC) CS374 2 Fall 2015 2 / 59
A B C D E F G H
Chandra & Manoj (UIUC) CS374 3 Fall 2015 3 / 59
A B C D E F G H
B, E, F G H A, C, D
1
2
Chandra & Manoj (UIUC) CS374 4 Fall 2015 4 / 59
Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 59
Chandra & Manoj (UIUC) CS374 6 Fall 2015 6 / 59
Chandra & Manoj (UIUC) CS374 7 Fall 2015 7 / 59
1 2 3 4
Chandra & Manoj (UIUC) CS374 8 Fall 2015 8 / 59
1
2
Chandra & Manoj (UIUC) CS374 9 Fall 2015 9 / 59
Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 59
Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 59
1
2
Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 59
1 2 3 4
1 2 3 4
Chandra & Manoj (UIUC) CS374 11 Fall 2015 11 / 59
Chandra & Manoj (UIUC) CS374 12 Fall 2015 12 / 59
1
2
3
Chandra & Manoj (UIUC) CS374 13 Fall 2015 13 / 59
Chandra & Manoj (UIUC) CS374 14 Fall 2015 14 / 59
Chandra & Manoj (UIUC) CS374 15 Fall 2015 15 / 59
Chandra & Manoj (UIUC) CS374 16 Fall 2015 16 / 59
Chandra & Manoj (UIUC) CS374 17 Fall 2015 17 / 59
Chandra & Manoj (UIUC) CS374 18 Fall 2015 18 / 59
Chandra & Manoj (UIUC) CS374 19 Fall 2015 19 / 59
1
2
3
Chandra & Manoj (UIUC) CS374 20 Fall 2015 20 / 59
DFS(G)
Mark u as unvisited Set pred(u) to null T is set to ∅
DFS(u) Output T DFS(u) Mark u as visited
add edge uv to T set pred(v) to u DFS(v)
Chandra & Manoj (UIUC) CS374 21 Fall 2015 21 / 59
1 2 3 4 5 6 7 8 9 10
1
2
Chandra & Manoj (UIUC) CS374 22 Fall 2015 22 / 59
1
2
3
1
2
Chandra & Manoj (UIUC) CS374 23 Fall 2015 23 / 59
DFS(G)
Mark u as unvisited T is set to ∅ time = 0
DFS(u) Output T DFS(u) Mark u as visited pre(u) = ++time
add edge uv to T DFS(v) post(u) = ++time
Chandra & Manoj (UIUC) CS374 24 Fall 2015 24 / 59
1 2 3 4 5 6 7 8 9 10 Chandra & Manoj (UIUC) CS374 25 Fall 2015 25 / 59
Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59
Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59
Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59
Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59
Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59
Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59
DFS(G) Mark all nodes u as unvisited T is set to ∅ time = 0
DFS(u) Output T DFS(u) Mark u as visited pre(u) = ++time
add edge (u, v) to T DFS(v) post(u) = ++time
Chandra & Manoj (UIUC) CS374 27 Fall 2015 27 / 59
A B C D E F G H
Chandra & Manoj (UIUC) CS374 28 Fall 2015 28 / 59
1
Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 59
1
2
Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 59
1
2
3
Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 59
1
2
3
4
Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 59
1
2
3
4
Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 59
1
2
3
4
Chandra & Manoj (UIUC) CS374 30 Fall 2015 30 / 59
A B C D
Cross Forward Backward
Chandra & Manoj (UIUC) CS374 31 Fall 2015 31 / 59
Chandra & Manoj (UIUC) CS374 32 Fall 2015 32 / 59
... to check for Acylicity and compute Topological Ordering
Chandra & Manoj (UIUC) CS374 33 Fall 2015 33 / 59
... to check for Acylicity and compute Topological Ordering
1
2
3
Chandra & Manoj (UIUC) CS374 33 Fall 2015 33 / 59
... to check for Acylicity and compute Topological Ordering
1
2
3
Chandra & Manoj (UIUC) CS374 33 Fall 2015 33 / 59
Chandra & Manoj (UIUC) CS374 34 Fall 2015 34 / 59
Chandra & Manoj (UIUC) CS374 35 Fall 2015 35 / 59
Chandra & Manoj (UIUC) CS374 36 Fall 2015 36 / 59
Chandra & Manoj (UIUC) CS374 37 Fall 2015 37 / 59
Chandra & Manoj (UIUC) CS374 38 Fall 2015 38 / 59
Mark all vertices in V as not visited.
find SCC(G, u) the strong component of u: Compute rch(G, u) using DFS(G, u) Compute rch(Grev, u) using DFS(Grev, u) SCC(G, u) ⇐ rch(G, u) ∩ rch(Grev, u) ∀u ∈ SCC(G, u): Mark u as visited.
Chandra & Manoj (UIUC) CS374 38 Fall 2015 38 / 59
Mark all vertices in V as not visited.
find SCC(G, u) the strong component of u: Compute rch(G, u) using DFS(G, u) Compute rch(Grev, u) using DFS(Grev, u) SCC(G, u) ⇐ rch(G, u) ∩ rch(Grev, u) ∀u ∈ SCC(G, u): Mark u as visited.
Chandra & Manoj (UIUC) CS374 38 Fall 2015 38 / 59
A B C D E F G H
B, E, F G H A, C, D
Chandra & Manoj (UIUC) CS374 39 Fall 2015 39 / 59
Exploit structure of meta-graph...
1
2
3
Chandra & Manoj (UIUC) CS374 40 Fall 2015 40 / 59
Exploit structure of meta-graph...
1
2
3
1
Chandra & Manoj (UIUC) CS374 40 Fall 2015 40 / 59
Exploit structure of meta-graph...
1
2
3
1
2
3 4 Chandra & Manoj (UIUC) CS374 40 Fall 2015 40 / 59
Exploit structure of meta-graph...
1
2
3
1
2
3
4 Chandra & Manoj (UIUC) CS374 40 Fall 2015 40 / 59
Exploit structure of meta-graph...
1
2
3
1
2
3
4
Chandra & Manoj (UIUC) CS374 40 Fall 2015 40 / 59
Chandra & Manoj (UIUC) CS374 41 Fall 2015 41 / 59
Chandra & Manoj (UIUC) CS374 41 Fall 2015 41 / 59
Chandra & Manoj (UIUC) CS374 41 Fall 2015 41 / 59
...for computing the strong connected components in G
Mark all nodes as unvisited
DFS(u) Let Su be the nodes reached by u Output Su as a strong connected component Remove Su from G
Chandra & Manoj (UIUC) CS374 42 Fall 2015 42 / 59
G F E B C D H A
G F E B C D H A
G F E B C D H A
6] [1, [7, 12] [9, 10] [8, 11] [13 , 16] [14 , 15] [2, 5] [3, 4] G F E B C D H A Chandra & Manoj (UIUC) CS374 43 Fall 2015 43 / 59
Removing connected components: 1
G F E B C D H A
16 11 6 12 10 15 5 4
F E B C D H A
11 6 12 10 15 5 4
Chandra & Manoj (UIUC) CS374 44 Fall 2015 44 / 59
Removing connected components: 2
F E B C D H A
11 6 12 10 15 5 4
F E B C D A
11 6 12 10 5 4
Chandra & Manoj (UIUC) CS374 45 Fall 2015 45 / 59
Removing connected components: 3
F E B C D A
11 6 12 10 5 4
C D A
6 5 4
Chandra & Manoj (UIUC) CS374 46 Fall 2015 46 / 59
Removing connected components: 4
C D A
6 5 4
Chandra & Manoj (UIUC) CS374 47 Fall 2015 47 / 59
Final result
G F E B C D H A
Chandra & Manoj (UIUC) CS374 48 Fall 2015 48 / 59
Once the strong connected components are computed.
Chandra & Manoj (UIUC) CS374 49 Fall 2015 49 / 59
Chandra & Manoj (UIUC) CS374 50 Fall 2015 50 / 59
Chandra & Manoj (UIUC) CS374 51 Fall 2015 51 / 59
Chandra & Manoj (UIUC) CS374 52 Fall 2015 52 / 59
1
2
1
2
3
Chandra & Manoj (UIUC) CS374 53 Fall 2015 53 / 59
project: main.o utils.o command.o cc -o project main.o utils.o command.o main.o: main.c defs.h cc -c main.c utils.o: utils.c defs.h command.h cc -c utils.c command.o: command.c defs.h command.h cc -c command.c
Chandra & Manoj (UIUC) CS374 54 Fall 2015 54 / 59
project main.o utils.o command.o main.c utils.c defs.h command.h command.c
Chandra & Manoj (UIUC) CS374 55 Fall 2015 55 / 59
1
2
3
4
Chandra & Manoj (UIUC) CS374 56 Fall 2015 56 / 59
1
2
3
4
1
Chandra & Manoj (UIUC) CS374 57 Fall 2015 57 / 59
1
2
3
Chandra & Manoj (UIUC) CS374 58 Fall 2015 58 / 59