chandra chekuri manoj prabhakaranchandra chekuri manoj prabhakaran university of illinois,...

79
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

Upload: others

Post on 09-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

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

Page 2: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Today

Two topics:

Structure of directed graphs

DFS and its properties

One application of DFS to obtain fast algorithms

Chandra & Manoj (UIUC) CS374 2 Fall 2015 2 / 59

Page 3: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Strong Connected Components (SCCs)

Algorithmic ProblemFind all SCCs of a given directed graph.

Previous lecture:Saw an O(n · (n + m)) time algorithm.This lecture: sketch of a O(n + m) timealgorithm.

Basic Graph TheoryBreadth First searchDepth First Search

Directed Graphs

Digraphs and ConnectivityDigraph RepresentationSearching

Directed Graphs

AB C

DE F

G H

Definition

A directed graph (also called a digraph) is G = (V ,E ), where

V is a set of vertices or nodes

E ! V " V is set of ordered pairs of vertices called edges

Viswanathan CS473ug

Chandra & Manoj (UIUC) CS374 3 Fall 2015 3 / 59

Page 4: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Graph of SCCsAB C

DE F

G H

Graph G

B,E, F

G H

A,C,D

Graph of SCCs GSCC

Meta-graph of SCCs

Let S1, S2, . . . Sk be the strong connected components (i.e., SCCs)of G. The graph of SCCs is GSCC

1 Vertices are S1, S2, . . . Sk

2 There is an edge (Si, Sj) if there is some u ∈ Si and v ∈ Sj

such that (u, v) is an edge in G.

Chandra & Manoj (UIUC) CS374 4 Fall 2015 4 / 59

Page 5: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Reversal and SCCs

PropositionFor any graph G, the graph of SCCs of Grev is the same as thereversal of GSCC.

Proof.Exercise.

Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 59

Page 6: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

SCCs and DAGs

Proposition

For any graph G, the graph GSCC has no directed cycle.

Proof.

If GSCC has a cycle S1, S2, . . . , Sk then S1 ∪ S2 ∪ · · · ∪ Sk shouldbe in the same SCC in G. Formal details: exercise.

Chandra & Manoj (UIUC) CS374 6 Fall 2015 6 / 59

Page 7: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Part I

Directed Acyclic Graphs

Chandra & Manoj (UIUC) CS374 7 Fall 2015 7 / 59

Page 8: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Directed Acyclic Graphs

DefinitionA directed graph G is adirected acyclic graph(DAG) if there is no directedcycle in G. 1

2 3

4

Chandra & Manoj (UIUC) CS374 8 Fall 2015 8 / 59

Page 9: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Sources and Sinks

source sink

1

2 3

4

Definition1 A vertex u is a source if it has no in-coming edges.

2 A vertex u is a sink if it has no out-going edges.

Chandra & Manoj (UIUC) CS374 9 Fall 2015 9 / 59

Page 10: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Simple DAG Properties

PropositionEvery DAG G has at least one source and at least one sink.

Proof.Let P = v1, v2, . . . , vk be a longest path in G. Claim that v1 is asource and vk is a sink. Suppose not. Then v1 has an incoming edgewhich either creates a cycle or a longer path both of which arecontradictions. Similarly if vk has an outgoing edge.

1 G is a DAG if and only if Grev is a DAG.

2 G is a DAG if and only each node is in its own strongconnected component.

Formal proofs: exercise.

Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 59

Page 11: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Simple DAG Properties

PropositionEvery DAG G has at least one source and at least one sink.

Proof.Let P = v1, v2, . . . , vk be a longest path in G. Claim that v1 is asource and vk is a sink. Suppose not. Then v1 has an incoming edgewhich either creates a cycle or a longer path both of which arecontradictions. Similarly if vk has an outgoing edge.

1 G is a DAG if and only if Grev is a DAG.

2 G is a DAG if and only each node is in its own strongconnected component.

Formal proofs: exercise.

Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 59

Page 12: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Simple DAG Properties

PropositionEvery DAG G has at least one source and at least one sink.

Proof.Let P = v1, v2, . . . , vk be a longest path in G. Claim that v1 is asource and vk is a sink. Suppose not. Then v1 has an incoming edgewhich either creates a cycle or a longer path both of which arecontradictions. Similarly if vk has an outgoing edge.

1 G is a DAG if and only if Grev is a DAG.

2 G is a DAG if and only each node is in its own strongconnected component.

Formal proofs: exercise.

Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 59

Page 13: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Topological Ordering/Sorting

1

2 3

4

Graph G

1 2 3 4

Topological Ordering of G

DefinitionA topological ordering/topological sorting of G = (V,E) is anordering ≺ on V such that if (u, v) ∈ E then u ≺ v.

Informal equivalent definition:

One can order the vertices of the graph along a line (say the x-axis)such that all edges are from left to right.

Chandra & Manoj (UIUC) CS374 11 Fall 2015 11 / 59

Page 14: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DAGs and Topological Sort

LemmaA directed graph G can be topologically ordered iff it is a DAG.

Need to show both directions.

Chandra & Manoj (UIUC) CS374 12 Fall 2015 12 / 59

Page 15: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DAGs and Topological Sort

LemmaA directed graph G can be topologically ordered if it is a DAG.

Proof.Consider the following algorithm:

1 Pick a source u, output it.

2 Remove u and all edges out of u.

3 Repeat until graph is empty.

Exercise: prove this gives toplogical sort.

Exercise: show algorithm can be implemented in O(m + n) time.

Chandra & Manoj (UIUC) CS374 13 Fall 2015 13 / 59

Page 16: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Topological Sort: Example

a b c

d e

f g

h

Chandra & Manoj (UIUC) CS374 14 Fall 2015 14 / 59

Page 17: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DAGs and Topological Sort

LemmaA directed graph G can be topologically ordered only if it is a DAG.

Proof.Suppose G is not a DAG and has a topological ordering ≺. G has acycle C = u1, u2, . . . , uk, u1.Then u1 ≺ u2 ≺ . . . ≺ uk ≺ u1!That is... u1 ≺ u1.A contradiction (to ≺ being an order).Not possible to topologically order the vertices.

Chandra & Manoj (UIUC) CS374 15 Fall 2015 15 / 59

Page 18: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DAGs and Topological Sort

Note: A DAG G may have many different topological sorts.

Question: What is a DAG with the most number of distincttopological sorts for a given number n of vertices?

Question: What is a DAG with the least number of distincttopological sorts for a given number n of vertices?

Chandra & Manoj (UIUC) CS374 16 Fall 2015 16 / 59

Page 19: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Cycles in graphs

Question: Given an undirected graph how do we check whether ithas a cycle and output one if it has one?

Question: Given an directed graph how do we check whether it hasa cycle and output one if it has one?

Chandra & Manoj (UIUC) CS374 17 Fall 2015 17 / 59

Page 20: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

To Remember: Structure of Graphs

Undirected graph: connected components of G = (V,E) partitionV and can be computed in O(m + n) time.

Directed graph: the meta-graph GSCC of G can be computed inO(m + n) time. GSCC gives information on the partition of V intostrong connected components and how they form a DAG structure.

Above structural decomposition will be useful in several algorithms

Chandra & Manoj (UIUC) CS374 18 Fall 2015 18 / 59

Page 21: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Part II

Depth First Search (DFS)

Chandra & Manoj (UIUC) CS374 19 Fall 2015 19 / 59

Page 22: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Depth First Search

DFS is a special case of Basic Search but is a versatile graphexploration strategy. John Hopcroft and Bob Tarjan (Turing Awardwinners) demonstrated the power of DFS to understand graphstructure. DFS can be used to obtain linear time (O(m + n))algorithms for

1 Finding cut-edges and cut-vertices of undirected graphs

2 Finding strong connected components of directed graphs

3 Linear time algorithm for testing whether a graph is planar

Many other applications as well.

Chandra & Manoj (UIUC) CS374 20 Fall 2015 20 / 59

Page 23: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DFS in Undirected Graphs

Recursive version. Easier to understand some properties.

DFS(G)for all u ∈ V(G) do

Mark u as unvisited

Set pred(u) to null

T is set to ∅while ∃ unvisited u do

DFS(u)Output T

DFS(u)Mark u as visited

for each uv in Out(u) doif v is not visited then

add edge uv to Tset pred(v) to uDFS(v)

Implemented using a global array Visited for all recursive calls.T is the search tree/forest.

Chandra & Manoj (UIUC) CS374 21 Fall 2015 21 / 59

Page 24: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Example

Basic Graph TheoryBreadth First searchDepth First Search

Directed Graphs

GraphsConnectivity in GraphsTreesGraph Representation

Connected Graphs

1

2 3

4 5

6

7

8

9

10

Definition

The set of connected components of a graph is the set{con(u) | u ! V }

The connected components in the above graph are{1, 2, 3, 4, 5, 6, 7, 8} and {9, 10}

A graph is said to be connected when it has exactly oneconnected component.

In other words, every pair of vertices inthe graph are connected.

Viswanathan CS473ug

Edges classified into two types: uv ∈ E is a

1 tree edge: belongs to T

2 non-tree edge: does not belong to T

Chandra & Manoj (UIUC) CS374 22 Fall 2015 22 / 59

Page 25: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Properties of DFS tree

Proposition1 T is a forest

2 connected components of T are same as those of G.3 If uv ∈ E is a non-tree edge then, in T, either:

1 u is an ancestor of v, or2 v is an ancestor of u.

Question: Why are there no cross-edges?

Chandra & Manoj (UIUC) CS374 23 Fall 2015 23 / 59

Page 26: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DFS with Visit Times

Keep track of when nodes are visited.

DFS(G)for all u ∈ V(G) do

Mark u as unvisited

T is set to ∅time = 0while ∃unvisited u do

DFS(u)Output T

DFS(u)Mark u as visited

pre(u) = ++timefor each uv in Out(u) do

if v is not marked thenadd edge uv to TDFS(v)

post(u) = ++time

Chandra & Manoj (UIUC) CS374 24 Fall 2015 24 / 59

Page 27: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Example

Basic Graph TheoryBreadth First searchDepth First Search

Directed Graphs

GraphsConnectivity in GraphsTreesGraph Representation

Connected Graphs

1

2 3

4 5

6

7

8

9

10

Definition

The set of connected components of a graph is the set{con(u) | u ! V }

The connected components in the above graph are{1, 2, 3, 4, 5, 6, 7, 8} and {9, 10}

A graph is said to be connected when it has exactly oneconnected component.

In other words, every pair of vertices inthe graph are connected.

Viswanathan CS473ug

Chandra & Manoj (UIUC) CS374 25 Fall 2015 25 / 59

Page 28: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

pre and post numbers

Node u is active in time interval [pre(u), post(u)]

Proposition

For any two nodes u and v, the two intervals [pre(u), post(u)] and[pre(v), post(v)] are disjoint or one is contained in the other.

Proof.Assume without loss of generality that pre(u) < pre(v). Thenv visited after u.

If DFS(v) invoked before DFS(u) finished,post(v) < post(u).

If DFS(v) invoked after DFS(u) finished, pre(v) > post(u).

pre and post numbers useful in several applications of DFS

Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59

Page 29: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

pre and post numbers

Node u is active in time interval [pre(u), post(u)]

Proposition

For any two nodes u and v, the two intervals [pre(u), post(u)] and[pre(v), post(v)] are disjoint or one is contained in the other.

Proof.

Assume without loss of generality that pre(u) < pre(v). Thenv visited after u.

If DFS(v) invoked before DFS(u) finished,post(v) < post(u).

If DFS(v) invoked after DFS(u) finished, pre(v) > post(u).

pre and post numbers useful in several applications of DFS

Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59

Page 30: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

pre and post numbers

Node u is active in time interval [pre(u), post(u)]

Proposition

For any two nodes u and v, the two intervals [pre(u), post(u)] and[pre(v), post(v)] are disjoint or one is contained in the other.

Proof.Assume without loss of generality that pre(u) < pre(v). Thenv visited after u.

If DFS(v) invoked before DFS(u) finished,post(v) < post(u).

If DFS(v) invoked after DFS(u) finished, pre(v) > post(u).

pre and post numbers useful in several applications of DFS

Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59

Page 31: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

pre and post numbers

Node u is active in time interval [pre(u), post(u)]

Proposition

For any two nodes u and v, the two intervals [pre(u), post(u)] and[pre(v), post(v)] are disjoint or one is contained in the other.

Proof.Assume without loss of generality that pre(u) < pre(v). Thenv visited after u.

If DFS(v) invoked before DFS(u) finished,post(v) < post(u).

If DFS(v) invoked after DFS(u) finished, pre(v) > post(u).

pre and post numbers useful in several applications of DFS

Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59

Page 32: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

pre and post numbers

Node u is active in time interval [pre(u), post(u)]

Proposition

For any two nodes u and v, the two intervals [pre(u), post(u)] and[pre(v), post(v)] are disjoint or one is contained in the other.

Proof.Assume without loss of generality that pre(u) < pre(v). Thenv visited after u.

If DFS(v) invoked before DFS(u) finished,post(v) < post(u).

If DFS(v) invoked after DFS(u) finished, pre(v) > post(u).

pre and post numbers useful in several applications of DFS

Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59

Page 33: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

pre and post numbers

Node u is active in time interval [pre(u), post(u)]

Proposition

For any two nodes u and v, the two intervals [pre(u), post(u)] and[pre(v), post(v)] are disjoint or one is contained in the other.

Proof.Assume without loss of generality that pre(u) < pre(v). Thenv visited after u.

If DFS(v) invoked before DFS(u) finished,post(v) < post(u).

If DFS(v) invoked after DFS(u) finished, pre(v) > post(u).

pre and post numbers useful in several applications of DFS

Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59

Page 34: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DFS in Directed Graphs

DFS(G)Mark all nodes u as unvisited

T is set to ∅time = 0while there is an unvisited node u do

DFS(u)Output T

DFS(u)Mark u as visited

pre(u) = ++timefor each edge (u, v) in Out(u) do

if v is not visited

add edge (u, v) to TDFS(v)

post(u) = ++time

Chandra & Manoj (UIUC) CS374 27 Fall 2015 27 / 59

Page 35: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Example

Basic Graph TheoryBreadth First searchDepth First Search

Directed Graphs

Digraphs and ConnectivityDigraph RepresentationSearching

Directed Graphs

AB C

DE F

G H

Definition

A directed graph (also called a digraph) is G = (V ,E ), where

V is a set of vertices or nodes

E ! V " V is set of ordered pairs of vertices called edges

Viswanathan CS473ug

Chandra & Manoj (UIUC) CS374 28 Fall 2015 28 / 59

Page 36: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DFS Properties

Generalizing ideas from undirected graphs:

1 DFS(G) takes O(m + n) time.

2 Edges added form a branching: a forest of out-trees. Output ofDFS(G) depends on the order in which vertices are considered.

3 If u is the first vertex considered by DFS(G) then DFS(u)outputs a directed out-tree T rooted at u and a vertex v is in Tif and only if v ∈ rch(u)

4 For any two vertices x, y the intervals [pre(x), post(x)] and[pre(y), post(y)] are either disjoint or one is contained in theother.

Note: Not obvious whether DFS(G) is useful in dir graphs but it is.

Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 59

Page 37: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DFS Properties

Generalizing ideas from undirected graphs:

1 DFS(G) takes O(m + n) time.

2 Edges added form a branching: a forest of out-trees. Output ofDFS(G) depends on the order in which vertices are considered.

3 If u is the first vertex considered by DFS(G) then DFS(u)outputs a directed out-tree T rooted at u and a vertex v is in Tif and only if v ∈ rch(u)

4 For any two vertices x, y the intervals [pre(x), post(x)] and[pre(y), post(y)] are either disjoint or one is contained in theother.

Note: Not obvious whether DFS(G) is useful in dir graphs but it is.

Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 59

Page 38: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DFS Properties

Generalizing ideas from undirected graphs:

1 DFS(G) takes O(m + n) time.

2 Edges added form a branching: a forest of out-trees. Output ofDFS(G) depends on the order in which vertices are considered.

3 If u is the first vertex considered by DFS(G) then DFS(u)outputs a directed out-tree T rooted at u and a vertex v is in Tif and only if v ∈ rch(u)

4 For any two vertices x, y the intervals [pre(x), post(x)] and[pre(y), post(y)] are either disjoint or one is contained in theother.

Note: Not obvious whether DFS(G) is useful in dir graphs but it is.

Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 59

Page 39: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DFS Properties

Generalizing ideas from undirected graphs:

1 DFS(G) takes O(m + n) time.

2 Edges added form a branching: a forest of out-trees. Output ofDFS(G) depends on the order in which vertices are considered.

3 If u is the first vertex considered by DFS(G) then DFS(u)outputs a directed out-tree T rooted at u and a vertex v is in Tif and only if v ∈ rch(u)

4 For any two vertices x, y the intervals [pre(x), post(x)] and[pre(y), post(y)] are either disjoint or one is contained in theother.

Note: Not obvious whether DFS(G) is useful in dir graphs but it is.

Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 59

Page 40: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DFS Properties

Generalizing ideas from undirected graphs:

1 DFS(G) takes O(m + n) time.

2 Edges added form a branching: a forest of out-trees. Output ofDFS(G) depends on the order in which vertices are considered.

3 If u is the first vertex considered by DFS(G) then DFS(u)outputs a directed out-tree T rooted at u and a vertex v is in Tif and only if v ∈ rch(u)

4 For any two vertices x, y the intervals [pre(x), post(x)] and[pre(y), post(y)] are either disjoint or one is contained in theother.

Note: Not obvious whether DFS(G) is useful in dir graphs but it is.

Chandra & Manoj (UIUC) CS374 29 Fall 2015 29 / 59

Page 41: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

DFS Tree

Edges of G can be classified with respect to the DFS tree T as:

1 Tree edges that belong to T

2 A forward edge is a non-tree edges (x, y) such thatpre(x) < pre(y) < post(y) < post(x).

3 A backward edge is a non-tree edge (y, x) such thatpre(x) < pre(y) < post(y) < post(x).

4 A cross edge is a non-tree edges (x, y) such that the intervals[pre(x), post(x)] and [pre(y), post(y)] are disjoint.

Chandra & Manoj (UIUC) CS374 30 Fall 2015 30 / 59

Page 42: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Types of Edges

A

B

C D

Cross

Forward

Backward

Chandra & Manoj (UIUC) CS374 31 Fall 2015 31 / 59

Page 43: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Cycles in graphs

Question: Given an undirected graph how do we check whether ithas a cycle and output one if it has one?

Question: Given an directed graph how do we check whether it hasa cycle and output one if it has one?

Chandra & Manoj (UIUC) CS374 32 Fall 2015 32 / 59

Page 44: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Using DFS...... to check for Acylicity and compute Topological Ordering

QuestionGiven G, is it a DAG? If it is, generate a topological sort. Elseoutput a cycle C.

DFS based algorithm:

1 Compute DFS(G)

2 If there is a back edge e = (v, u) then G is not a DAG. Outputcyclce C formed by path from u to v in T plus edge (v, u).

3 Otherwise output nodes in decreasing post-visit order. Note: noneed to sort, DFS(G) can output nodes in this order.

Algorithm runs in O(n + m) time.Correctness is not so obvious. See next two propositions.

Chandra & Manoj (UIUC) CS374 33 Fall 2015 33 / 59

Page 45: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Using DFS...... to check for Acylicity and compute Topological Ordering

QuestionGiven G, is it a DAG? If it is, generate a topological sort. Elseoutput a cycle C.

DFS based algorithm:

1 Compute DFS(G)

2 If there is a back edge e = (v, u) then G is not a DAG. Outputcyclce C formed by path from u to v in T plus edge (v, u).

3 Otherwise output nodes in decreasing post-visit order. Note: noneed to sort, DFS(G) can output nodes in this order.

Algorithm runs in O(n + m) time.

Correctness is not so obvious. See next two propositions.

Chandra & Manoj (UIUC) CS374 33 Fall 2015 33 / 59

Page 46: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Using DFS...... to check for Acylicity and compute Topological Ordering

QuestionGiven G, is it a DAG? If it is, generate a topological sort. Elseoutput a cycle C.

DFS based algorithm:

1 Compute DFS(G)

2 If there is a back edge e = (v, u) then G is not a DAG. Outputcyclce C formed by path from u to v in T plus edge (v, u).

3 Otherwise output nodes in decreasing post-visit order. Note: noneed to sort, DFS(G) can output nodes in this order.

Algorithm runs in O(n + m) time.Correctness is not so obvious. See next two propositions.

Chandra & Manoj (UIUC) CS374 33 Fall 2015 33 / 59

Page 47: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Back edge and Cycles

Proposition

G has a cycle iff there is a back-edge in DFS(G).

Proof.If: (u, v) is a back edge implies there is a cycle C consisting of thepath from v to u in DFS search tree and the edge (u, v).

Only if: Suppose there is a cycle C = v1 → v2 → . . .→ vk → v1.Let vi be first node in C visited in DFS.All other nodes in C are descendants of vi since they are reachablefrom vi.Therefore, (vi−1, vi) (or (vk, v1) if i = 1) is a back edge.

Chandra & Manoj (UIUC) CS374 34 Fall 2015 34 / 59

Page 48: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Proof

Proposition

If G is a DAG and post(v) > post(u), then (u, v) is not in G.

Proof.Assume post(v) > post(u) and (u, v) is an edge in G. We derivea contradiction. One of two cases holds from DFS property.

Case 1: [pre(u), post(u)] is contained in [pre(v), post(v)].Implies that u is explored during DFS(v) and hence is adescendent of v. Edge (u, v) implies a cycle in G but G isassumed to be DAG!

Case 2: [pre(u), post(u)] is disjoint from [pre(v), post(v)].This cannot happen since v would be explored from u.

Chandra & Manoj (UIUC) CS374 35 Fall 2015 35 / 59

Page 49: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Example

a b c

d e

f g

h

Chandra & Manoj (UIUC) CS374 36 Fall 2015 36 / 59

Page 50: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Part III

Linear time algorithm for finding allstrong connected components of a

directed graph

Chandra & Manoj (UIUC) CS374 37 Fall 2015 37 / 59

Page 51: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Finding all SCCs of a Directed Graph

ProblemGiven a directed graph G = (V,E), output all its strong connectedcomponents.

Straightforward algorithm:

Mark all vertices in V as not visited.

for each vertex u ∈ V not visited yet dofind 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.

Running time: O(n(n + m))Is there an O(n + m) time algorithm?

Chandra & Manoj (UIUC) CS374 38 Fall 2015 38 / 59

Page 52: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Finding all SCCs of a Directed Graph

ProblemGiven a directed graph G = (V,E), output all its strong connectedcomponents.

Straightforward algorithm:

Mark all vertices in V as not visited.

for each vertex u ∈ V not visited yet dofind 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.

Running time: O(n(n + m))

Is there an O(n + m) time algorithm?

Chandra & Manoj (UIUC) CS374 38 Fall 2015 38 / 59

Page 53: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Finding all SCCs of a Directed Graph

ProblemGiven a directed graph G = (V,E), output all its strong connectedcomponents.

Straightforward algorithm:

Mark all vertices in V as not visited.

for each vertex u ∈ V not visited yet dofind 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.

Running time: O(n(n + m))Is there an O(n + m) time algorithm?

Chandra & Manoj (UIUC) CS374 38 Fall 2015 38 / 59

Page 54: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Structure of a Directed GraphAB C

DE F

G H

Graph G

B,E, F

G H

A,C,D

Graph of SCCs GSCC

Reminder

GSCC is created by collapsing every strong connected component to asingle vertex.

Proposition

For a directed graph G, its meta-graph GSCC is a DAG.

Chandra & Manoj (UIUC) CS374 39 Fall 2015 39 / 59

Page 55: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear-time Algorithm for SCCs: IdeasExploit structure of meta-graph...

Wishful Thinking Algorithm1 Let u be a vertex in a sink SCC of GSCC

2 Do DFS(u) to compute SCC(u)

3 Remove SCC(u) and repeat

Justification1 DFS(u) only visits vertices (and edges) in SCC(u)

2

3

4

Chandra & Manoj (UIUC) CS374 40 Fall 2015 40 / 59

Page 56: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear-time Algorithm for SCCs: IdeasExploit structure of meta-graph...

Wishful Thinking Algorithm1 Let u be a vertex in a sink SCC of GSCC

2 Do DFS(u) to compute SCC(u)

3 Remove SCC(u) and repeat

Justification1 DFS(u) only visits vertices (and edges) in SCC(u)

2

3

4

Chandra & Manoj (UIUC) CS374 40 Fall 2015 40 / 59

Page 57: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear-time Algorithm for SCCs: IdeasExploit structure of meta-graph...

Wishful Thinking Algorithm1 Let u be a vertex in a sink SCC of GSCC

2 Do DFS(u) to compute SCC(u)

3 Remove SCC(u) and repeat

Justification1 DFS(u) only visits vertices (and edges) in SCC(u)

2 ... since there are no edges coming out a sink!

3

4

Chandra & Manoj (UIUC) CS374 40 Fall 2015 40 / 59

Page 58: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear-time Algorithm for SCCs: IdeasExploit structure of meta-graph...

Wishful Thinking Algorithm1 Let u be a vertex in a sink SCC of GSCC

2 Do DFS(u) to compute SCC(u)

3 Remove SCC(u) and repeat

Justification1 DFS(u) only visits vertices (and edges) in SCC(u)

2 ... since there are no edges coming out a sink!

3 DFS(u) takes time proportional to size of SCC(u)

4

Chandra & Manoj (UIUC) CS374 40 Fall 2015 40 / 59

Page 59: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear-time Algorithm for SCCs: IdeasExploit structure of meta-graph...

Wishful Thinking Algorithm1 Let u be a vertex in a sink SCC of GSCC

2 Do DFS(u) to compute SCC(u)

3 Remove SCC(u) and repeat

Justification1 DFS(u) only visits vertices (and edges) in SCC(u)

2 ... since there are no edges coming out a sink!

3 DFS(u) takes time proportional to size of SCC(u)

4 Therefore, total time O(n + m)!

Chandra & Manoj (UIUC) CS374 40 Fall 2015 40 / 59

Page 60: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Big Challenge(s)

How do we find a vertex in a sink SCC of GSCC?

Can we obtain an implicit topological sort of GSCC withoutcomputing GSCC?

Answer: DFS(G) gives some information!

Chandra & Manoj (UIUC) CS374 41 Fall 2015 41 / 59

Page 61: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Big Challenge(s)

How do we find a vertex in a sink SCC of GSCC?

Can we obtain an implicit topological sort of GSCC withoutcomputing GSCC?

Answer: DFS(G) gives some information!

Chandra & Manoj (UIUC) CS374 41 Fall 2015 41 / 59

Page 62: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Big Challenge(s)

How do we find a vertex in a sink SCC of GSCC?

Can we obtain an implicit topological sort of GSCC withoutcomputing GSCC?

Answer: DFS(G) gives some information!

Chandra & Manoj (UIUC) CS374 41 Fall 2015 41 / 59

Page 63: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear Time Algorithm...for computing the strong connected components in G

do DFS(Grev) and output vertices in decreasing post order.

Mark all nodes as unvisited

for each u in the computed order doif u is not visited then

DFS(u)Let Su be the nodes reached by uOutput Su as a strong connected component

Remove Su from G

TheoremAlgorithm runs in time O(m + n) and correctly outputs all the SCCsof G.

Chandra & Manoj (UIUC) CS374 42 Fall 2015 42 / 59

Page 64: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear Time Algorithm: An Example - Initial steps

Graph G:

G

FE

B C

D

H

A

=⇒

Reverse graph Grev:

G

FE

B C

D

H

A

=⇒

DFS of reverse graph:

G

FE

B C

D

H

A

=⇒

Pre/Post DFS numberingof reverse graph:

6][1,

[7, 12]

[9, 10] [8, 11]

[13, 16]

[14, 15]

[2, 5]

[3, 4]

G

FE

B C

D

H

A

Chandra & Manoj (UIUC) CS374 43 Fall 2015 43 / 59

Page 65: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear Time Algorithm: An ExampleRemoving connected components: 1

Original graph G with rev postnumbers:

G

FE

B C

D

H

A

16

11

612

10

15

5

4 =⇒

Do DFS from vertex Gremove it.

FE

B C

D

H

A

11

612

10

15

5

4

SCC computed:{G}

Chandra & Manoj (UIUC) CS374 44 Fall 2015 44 / 59

Page 66: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear Time Algorithm: An ExampleRemoving connected components: 2

Do DFS from vertex Gremove it.

FE

B C

D

H

A

11

612

10

15

5

4

SCC computed:{G}

=⇒

Do DFS from vertex H,remove it.

FE

B C

D

A

11

612

10 5

4

SCC computed:{G}, {H}

Chandra & Manoj (UIUC) CS374 45 Fall 2015 45 / 59

Page 67: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear Time Algorithm: An ExampleRemoving connected components: 3

Do DFS from vertex H,remove it.

FE

B C

D

A

11

612

10 5

4

SCC computed:{G}, {H}

=⇒

Do DFS from vertex BRemove visited vertices:{F,B,E}.

C

D

A

6

5

4

SCC computed:{G}, {H}, {F,B,E}

Chandra & Manoj (UIUC) CS374 46 Fall 2015 46 / 59

Page 68: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear Time Algorithm: An ExampleRemoving connected components: 4

Do DFS from vertex FRemove visited vertices:{F,B,E}.

C

D

A

6

5

4

SCC computed:{G}, {H}, {F,B,E}

=⇒

Do DFS from vertex ARemove visited vertices:{A,C,D}.

SCC computed:{G}, {H}, {F,B,E}, {A,C,D}

Chandra & Manoj (UIUC) CS374 47 Fall 2015 47 / 59

Page 69: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Linear Time Algorithm: An ExampleFinal result

G

FE

B C

D

H

A

SCC computed:{G}, {H}, {F,B,E}, {A,C,D}Which is the correct answer!

Chandra & Manoj (UIUC) CS374 48 Fall 2015 48 / 59

Page 70: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Obtaining the meta-graph...Once the strong connected components are computed.

Exercise:Given all the strong connected components of a directed graphG = (V,E) show that the meta-graph GSCC can be obtained inO(m + n) time.

Chandra & Manoj (UIUC) CS374 49 Fall 2015 49 / 59

Page 71: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Solving Problems on Directed Graphs

A template for a class of problems on directed graphs:

Is the problem solvable when G is strongly connected?

Is the problem solvable when G is a DAG?

If the above two are feasible then is the problem solvable in ageneral directed graph G by considering the meta graph GSCC?

Chandra & Manoj (UIUC) CS374 50 Fall 2015 50 / 59

Page 72: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Part IV

An Application to make

Chandra & Manoj (UIUC) CS374 51 Fall 2015 51 / 59

Page 73: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Make/Makefile

(A) I know what make/makefile is.

(B) I do NOT know what make/makefile is.

Chandra & Manoj (UIUC) CS374 52 Fall 2015 52 / 59

Page 74: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

make Utility [Feldman]

1 Unix utility for automatically building large software applications2 A makefile specifies

1 Object files to be created,2 Source/object files to be used in creation, and3 How to create them

Chandra & Manoj (UIUC) CS374 53 Fall 2015 53 / 59

Page 75: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

An Example makefile

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

Page 76: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

makefile as a Digraph

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

Page 77: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Computational Problems for make

1 Is the makefile reasonable?

2 If it is reasonable, in what order should the object files becreated?

3 If it is not reasonable, provide helpful debugging information.

4 If some file is modified, find the fewest compilations needed tomake application consistent.

Chandra & Manoj (UIUC) CS374 56 Fall 2015 56 / 59

Page 78: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Algorithms for make

1 Is the makefile reasonable? Is G a DAG?

2 If it is reasonable, in what order should the object files becreated? Find a topological sort of a DAG.

3 If it is not reasonable, provide helpful debugging information.Output a cycle. More generally, output all strong connectedcomponents.

4 If some file is modified, find the fewest compilations needed tomake application consistent.

1 Find all vertices reachable (using DFS/BFS) from modifiedfiles in directed graph, and recompile them in proper order.Verify that one can find the files to recompile and the orderingin linear time.

Chandra & Manoj (UIUC) CS374 57 Fall 2015 57 / 59

Page 79: Chandra Chekuri Manoj PrabhakaranChandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59. Today Two topics:

Take away Points

1 Given a directed graph G, its SCCs and the associated acyclicmeta-graph GSCC give a structural decomposition of G thatshould be kept in mind.

2 There is a DFS based linear time algorithm to compute all theSCCs and the meta-graph. Properties of DFS crucial for thealgorithm.

3 DAGs arise in many application and topological sort is a keyproperty in algorithm design. Linear time algorithms to computea topological sort (there can be many possible orderings so notunique).

Chandra & Manoj (UIUC) CS374 58 Fall 2015 58 / 59