CS 374: Algorithms & Models of Computation Chandra Chekuri - - PowerPoint PPT Presentation

cs 374 algorithms models of computation
SMART_READER_LITE
LIVE PREVIEW

CS 374: Algorithms & Models of Computation Chandra Chekuri - - PowerPoint PPT Presentation

CS 374: Algorithms & Models of Computation Chandra Chekuri University of Illinois, Urbana-Champaign Spring 2017 Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 1 Today Two topics: Structure of directed graphs DFS and its properties One


slide-1
SLIDE 1

CS 374: Algorithms & Models of Computation

Chandra Chekuri

University of Illinois, Urbana-Champaign

Spring 2017

Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 1

slide-2
SLIDE 2

Today

Two topics: Structure of directed graphs DFS and its properties One application of DFS to obtain fast algorithms

Chandra Chekuri (UIUC) CS374 2 Spring 2017 2 / 1

slide-3
SLIDE 3

Strong Connected Components (SCCs)

Algorithmic Problem

Find 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) time algorithm.

A B C D E F G H

Chandra Chekuri (UIUC) CS374 3 Spring 2017 3 / 1

slide-4
SLIDE 4

Graph of SCCs

A B C D E 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)

  • f 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 Chekuri (UIUC) CS374 4 Spring 2017 4 / 1

slide-5
SLIDE 5

Reversal and SCCs

Proposition

For any graph G, the graph of SCCs of Grev is the same as the reversal of GSCC.

Proof.

Exercise.

Chandra Chekuri (UIUC) CS374 5 Spring 2017 5 / 1

slide-6
SLIDE 6

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 should be in the same SCC in G. Formal details: exercise.

Chandra Chekuri (UIUC) CS374 6 Spring 2017 6 / 1

slide-7
SLIDE 7

Part I Directed Acyclic Graphs

Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 1

slide-8
SLIDE 8

Directed Acyclic Graphs

Definition

A directed graph G is a directed acyclic graph (DAG) if there is no directed cycle in G.

1 2 3 4

Chandra Chekuri (UIUC) CS374 8 Spring 2017 8 / 1

slide-9
SLIDE 9

Sources and Sinks

source sink 1 2 3 4

Definition

1

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 Chekuri (UIUC) CS374 9 Spring 2017 9 / 1

slide-10
SLIDE 10

Simple DAG Properties

Proposition

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

Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 1

slide-11
SLIDE 11

Simple DAG Properties

Proposition

Every 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 a source and vk is a sink. Suppose not. Then v1 has an incoming edge which either creates a cycle or a longer path both of which are

  • contradictions. Similarly if vk has an outgoing edge.

Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 1

slide-12
SLIDE 12

Simple DAG Properties

Proposition

Every 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 a source and vk is a sink. Suppose not. Then v1 has an incoming edge which either creates a cycle or a longer path both of which are

  • contradictions. 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 strong connected component. Formal proofs: exercise.

Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 1

slide-13
SLIDE 13

Topological Ordering/Sorting

1 2 3 4

Graph G

1 2 3 4

Topological Ordering of G

Definition

A topological ordering/topological sorting of G = (V, E) is an

  • rdering ≺ 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 Chekuri (UIUC) CS374 11 Spring 2017 11 / 1

slide-14
SLIDE 14

DAGs and Topological Sort

Lemma

A directed graph G can be topologically ordered iff it is a DAG. Need to show both directions.

Chandra Chekuri (UIUC) CS374 12 Spring 2017 12 / 1

slide-15
SLIDE 15

DAGs and Topological Sort

Lemma

A 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 Chekuri (UIUC) CS374 13 Spring 2017 13 / 1

slide-16
SLIDE 16

Topological Sort: Example a b c d e f g h

Chandra Chekuri (UIUC) CS374 14 Spring 2017 14 / 1

slide-17
SLIDE 17

DAGs and Topological Sort

Lemma

A 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 a cycle 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 Chekuri (UIUC) CS374 15 Spring 2017 15 / 1

slide-18
SLIDE 18

DAGs and Topological Sort

Note: A DAG G may have many different topological sorts. Question: What is a DAG with the most number of distinct topological sorts for a given number n of vertices? Question: What is a DAG with the least number of distinct topological sorts for a given number n of vertices?

Chandra Chekuri (UIUC) CS374 16 Spring 2017 16 / 1

slide-19
SLIDE 19

Cycles in graphs

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

Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 1

slide-20
SLIDE 20

To Remember: Structure of Graphs

Undirected graph: connected components of G = (V, E) partition V and can be computed in O(m + n) time. Directed graph: the meta-graph GSCC of G can be computed in O(m + n) time. GSCC gives information on the partition of V into strong connected components and how they form a DAG structure. Above structural decomposition will be useful in several algorithms

Chandra Chekuri (UIUC) CS374 18 Spring 2017 18 / 1

slide-21
SLIDE 21

Part II Depth First Search (DFS)

Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 1

slide-22
SLIDE 22

Depth First Search

DFS is a special case of Basic Search but is a versatile graph exploration strategy. John Hopcroft and Bob Tarjan (Turing Award winners) demonstrated the power of DFS to understand graph

  • structure. 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 Chekuri (UIUC) CS374 20 Spring 2017 20 / 1

slide-23
SLIDE 23

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) do if v is not visited then

add edge uv to T set pred(v) to u DFS(v)

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

Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 1

slide-24
SLIDE 24

Example

1 2 3 4 5 6 7 8 9 10

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 Chekuri (UIUC) CS374 22 Spring 2017 22 / 1

slide-25
SLIDE 25

Properties of DFS tree

Proposition

1

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, or

2

v is an ancestor of u.

Question: Why are there no cross-edges?

Chandra Chekuri (UIUC) CS374 23 Spring 2017 23 / 1

slide-26
SLIDE 26

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 = 0

while ∃unvisited u do

DFS(u) Output T DFS(u) Mark u as visited pre(u) = ++time

for each uv in Out(u) do if v is not marked then

add edge uv to T DFS(v) post(u) = ++time

Chandra Chekuri (UIUC) CS374 24 Spring 2017 24 / 1

slide-27
SLIDE 27

Example

1 2 3 4 5 6 7 8 9 10 Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 1

slide-28
SLIDE 28

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.

Chandra Chekuri (UIUC) CS374 26 Spring 2017 26 / 1

slide-29
SLIDE 29

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.

Chandra Chekuri (UIUC) CS374 26 Spring 2017 26 / 1

slide-30
SLIDE 30

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). Then v visited after u.

Chandra Chekuri (UIUC) CS374 26 Spring 2017 26 / 1

slide-31
SLIDE 31

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). Then v visited after u. If DFS(v) invoked before DFS(u) finished, post(v) < post(u).

Chandra Chekuri (UIUC) CS374 26 Spring 2017 26 / 1

slide-32
SLIDE 32

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). Then v 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).

Chandra Chekuri (UIUC) CS374 26 Spring 2017 26 / 1

slide-33
SLIDE 33

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). Then v 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 Chekuri (UIUC) CS374 26 Spring 2017 26 / 1

slide-34
SLIDE 34

DFS in Directed Graphs

DFS(G) Mark all nodes u as unvisited T is set to ∅ time = 0

while there is an unvisited node u do

DFS(u) Output T DFS(u) Mark u as visited pre(u) = ++time

for each edge (u, v) in Out(u) do if v is not visited

add edge (u, v) to T DFS(v) post(u) = ++time

Chandra Chekuri (UIUC) CS374 27 Spring 2017 27 / 1

slide-35
SLIDE 35

Example

A B C D E F G H

Chandra Chekuri (UIUC) CS374 28 Spring 2017 28 / 1

slide-36
SLIDE 36

DFS Properties

Generalizing ideas from undirected graphs:

1

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

Chandra Chekuri (UIUC) CS374 29 Spring 2017 29 / 1

slide-37
SLIDE 37

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 of DFS(G) depends on the order in which vertices are considered.

Chandra Chekuri (UIUC) CS374 29 Spring 2017 29 / 1

slide-38
SLIDE 38

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 of DFS(G) depends on the order in which vertices are considered.

3

If u is the first vertex considered by DFS(G) then DFS(u)

  • utputs a directed out-tree T rooted at u and a vertex v is in T

if and only if v ∈ rch(u)

Chandra Chekuri (UIUC) CS374 29 Spring 2017 29 / 1

slide-39
SLIDE 39

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 of DFS(G) depends on the order in which vertices are considered.

3

If u is the first vertex considered by DFS(G) then DFS(u)

  • utputs a directed out-tree T rooted at u and a vertex v is in T

if 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 the

  • ther.

Chandra Chekuri (UIUC) CS374 29 Spring 2017 29 / 1

slide-40
SLIDE 40

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 of DFS(G) depends on the order in which vertices are considered.

3

If u is the first vertex considered by DFS(G) then DFS(u)

  • utputs a directed out-tree T rooted at u and a vertex v is in T

if 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 the

  • ther.

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

Chandra Chekuri (UIUC) CS374 29 Spring 2017 29 / 1

slide-41
SLIDE 41

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 that pre(x) < pre(y) < post(y) < post(x).

3

A backward edge is a non-tree edge (y, x) such that pre(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 Chekuri (UIUC) CS374 30 Spring 2017 30 / 1

slide-42
SLIDE 42

Types of Edges

A B C D

Cross Forward Backward

Chandra Chekuri (UIUC) CS374 31 Spring 2017 31 / 1

slide-43
SLIDE 43

Cycles in graphs

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

Chandra Chekuri (UIUC) CS374 32 Spring 2017 32 / 1

slide-44
SLIDE 44

Using DFS...

... to check for Acylicity and compute Topological Ordering

Question

Given G, is it a DAG? If it is, generate a topological sort. Else

  • utput a cycle C.

Chandra Chekuri (UIUC) CS374 33 Spring 2017 33 / 1

slide-45
SLIDE 45

Using DFS...

... to check for Acylicity and compute Topological Ordering

Question

Given G, is it a DAG? If it is, generate a topological sort. Else

  • utput 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. Output cyclce C formed by path from u to v in T plus edge (v, u).

3

Otherwise output nodes in decreasing post-visit order. Note: no need to sort, DFS(G) can output nodes in this order. Algorithm runs in O(n + m) time.

Chandra Chekuri (UIUC) CS374 33 Spring 2017 33 / 1

slide-46
SLIDE 46

Using DFS...

... to check for Acylicity and compute Topological Ordering

Question

Given G, is it a DAG? If it is, generate a topological sort. Else

  • utput 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. Output cyclce C formed by path from u to v in T plus edge (v, u).

3

Otherwise output nodes in decreasing post-visit order. Note: no need 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 Chekuri (UIUC) CS374 33 Spring 2017 33 / 1

slide-47
SLIDE 47

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 the path 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 reachable from vi. Therefore, (vi−1, vi) (or (vk, v1) if i = 1) is a back edge.

Chandra Chekuri (UIUC) CS374 34 Spring 2017 34 / 1

slide-48
SLIDE 48

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 derive a 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 a descendent of v. Edge (u, v) implies a cycle in G but G is assumed 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 Chekuri (UIUC) CS374 35 Spring 2017 35 / 1

slide-49
SLIDE 49

Example a b c d e f g h

Chandra Chekuri (UIUC) CS374 36 Spring 2017 36 / 1

slide-50
SLIDE 50

Part III Linear time algorithm for finding all strong connected components of a directed graph

Chandra Chekuri (UIUC) CS374 37 Spring 2017 37 / 1

slide-51
SLIDE 51

Finding all SCCs of a Directed Graph

Problem

Given a directed graph G = (V, E), output all its strong connected components.

Chandra Chekuri (UIUC) CS374 38 Spring 2017 38 / 1

slide-52
SLIDE 52

Finding all SCCs of a Directed Graph

Problem

Given a directed graph G = (V, E), output all its strong connected components. Straightforward algorithm:

Mark all vertices in V as not visited.

for each vertex u ∈ V not visited yet do

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.

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

Chandra Chekuri (UIUC) CS374 38 Spring 2017 38 / 1

slide-53
SLIDE 53

Finding all SCCs of a Directed Graph

Problem

Given a directed graph G = (V, E), output all its strong connected components. Straightforward algorithm:

Mark all vertices in V as not visited.

for each vertex u ∈ V not visited yet do

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.

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

Chandra Chekuri (UIUC) CS374 38 Spring 2017 38 / 1

slide-54
SLIDE 54

Structure of a Directed Graph

A B C D E 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 a single vertex.

Proposition

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

Chandra Chekuri (UIUC) CS374 39 Spring 2017 39 / 1

slide-55
SLIDE 55

Linear-time Algorithm for SCCs: Ideas

Exploit structure of meta-graph...

Wishful Thinking Algorithm

1

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

Chandra Chekuri (UIUC) CS374 40 Spring 2017 40 / 1

slide-56
SLIDE 56

Linear-time Algorithm for SCCs: Ideas

Exploit structure of meta-graph...

Wishful Thinking Algorithm

1

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

Justification

1

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

Chandra Chekuri (UIUC) CS374 40 Spring 2017 40 / 1

slide-57
SLIDE 57

Linear-time Algorithm for SCCs: Ideas

Exploit structure of meta-graph...

Wishful Thinking Algorithm

1

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

Justification

1

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

2

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

3 4 Chandra Chekuri (UIUC) CS374 40 Spring 2017 40 / 1

slide-58
SLIDE 58

Linear-time Algorithm for SCCs: Ideas

Exploit structure of meta-graph...

Wishful Thinking Algorithm

1

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

Justification

1

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 Chekuri (UIUC) CS374 40 Spring 2017 40 / 1

slide-59
SLIDE 59

Linear-time Algorithm for SCCs: Ideas

Exploit structure of meta-graph...

Wishful Thinking Algorithm

1

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

Justification

1

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 Chekuri (UIUC) CS374 40 Spring 2017 40 / 1

slide-60
SLIDE 60

Big Challenge(s)

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

Chandra Chekuri (UIUC) CS374 41 Spring 2017 41 / 1

slide-61
SLIDE 61

Big Challenge(s)

How do we find a vertex in a sink SCC of GSCC? Can we obtain an implicit topological sort of GSCC without computing GSCC?

Chandra Chekuri (UIUC) CS374 41 Spring 2017 41 / 1

slide-62
SLIDE 62

Big Challenge(s)

How do we find a vertex in a sink SCC of GSCC? Can we obtain an implicit topological sort of GSCC without computing GSCC? Answer: DFS(G) gives some information!

Chandra Chekuri (UIUC) CS374 41 Spring 2017 41 / 1

slide-63
SLIDE 63

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 do if u is not visited then

DFS(u) Let Su be the nodes reached by u Output Su as a strong connected component Remove Su from G

Theorem

Algorithm runs in time O(m + n) and correctly outputs all the SCCs

  • f G.

Chandra Chekuri (UIUC) CS374 42 Spring 2017 42 / 1

slide-64
SLIDE 64

Linear Time Algorithm: An Example - Initial steps

Graph G:

G F E B C D H A

= ⇒ Reverse graph Grev:

G F E B C D H A

= ⇒ DFS of reverse graph:

G F E B C D H A

= ⇒ Pre/Post DFS numbering

  • f reverse graph:

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 Chekuri (UIUC) CS374 43 Spring 2017 43 / 1

slide-65
SLIDE 65

Linear Time Algorithm: An Example

Removing connected components: 1

Original graph G with rev post numbers:

G F E B C D H A

16 11 6 12 10 15 5 4

= ⇒ Do DFS from vertex G remove it.

F E B C D H A

11 6 12 10 15 5 4

SCC computed: {G}

Chandra Chekuri (UIUC) CS374 44 Spring 2017 44 / 1

slide-66
SLIDE 66

Linear Time Algorithm: An Example

Removing connected components: 2

Do DFS from vertex G remove it.

F E B C D H A

11 6 12 10 15 5 4

SCC computed: {G} = ⇒ Do DFS from vertex H, remove it.

F E B C D A

11 6 12 10 5 4

SCC computed: {G}, {H}

Chandra Chekuri (UIUC) CS374 45 Spring 2017 45 / 1

slide-67
SLIDE 67

Linear Time Algorithm: An Example

Removing connected components: 3

Do DFS from vertex H, remove it.

F E B C D A

11 6 12 10 5 4

SCC computed: {G}, {H} = ⇒ Do DFS from vertex B Remove visited vertices: {F, B, E}.

C D A

6 5 4

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

Chandra Chekuri (UIUC) CS374 46 Spring 2017 46 / 1

slide-68
SLIDE 68

Linear Time Algorithm: An Example

Removing connected components: 4

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

C D A

6 5 4

SCC computed: {G}, {H}, {F, B, E} = ⇒ Do DFS from vertex A Remove visited vertices: {A, C, D}. SCC computed: {G}, {H}, {F, B, E}, {A, C, D}

Chandra Chekuri (UIUC) CS374 47 Spring 2017 47 / 1

slide-69
SLIDE 69

Linear Time Algorithm: An Example

Final result

G F E B C D H A

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

Chandra Chekuri (UIUC) CS374 48 Spring 2017 48 / 1

slide-70
SLIDE 70

Obtaining the meta-graph...

Once the strong connected components are computed.

Exercise:

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

Chandra Chekuri (UIUC) CS374 49 Spring 2017 49 / 1

slide-71
SLIDE 71

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 a general directed graph G by considering the meta graph GSCC?

Chandra Chekuri (UIUC) CS374 50 Spring 2017 50 / 1

slide-72
SLIDE 72

Part IV An Application to make

Chandra Chekuri (UIUC) CS374 51 Spring 2017 51 / 1

slide-73
SLIDE 73

Make/Makefile

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

Chandra Chekuri (UIUC) CS374 52 Spring 2017 52 / 1

slide-74
SLIDE 74

make Utility [Feldman]

1

Unix utility for automatically building large software applications

2

A makefile specifies

1

Object files to be created,

2

Source/object files to be used in creation, and

3

How to create them

Chandra Chekuri (UIUC) CS374 53 Spring 2017 53 / 1

slide-75
SLIDE 75

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 Chekuri (UIUC) CS374 54 Spring 2017 54 / 1

slide-76
SLIDE 76

makefile as a Digraph

project main.o utils.o command.o main.c utils.c defs.h command.h command.c

Chandra Chekuri (UIUC) CS374 55 Spring 2017 55 / 1

slide-77
SLIDE 77

Computational Problems for make

1

Is the makefile reasonable?

2

If it is reasonable, in what order should the object files be created?

3

If it is not reasonable, provide helpful debugging information.

4

If some file is modified, find the fewest compilations needed to make application consistent.

Chandra Chekuri (UIUC) CS374 56 Spring 2017 56 / 1

slide-78
SLIDE 78

Algorithms for make

1

Is the makefile reasonable? Is G a DAG?

2

If it is reasonable, in what order should the object files be created? 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 connected components.

4

If some file is modified, find the fewest compilations needed to make application consistent.

1

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

Chandra Chekuri (UIUC) CS374 57 Spring 2017 57 / 1

slide-79
SLIDE 79

Take away Points

1

Given a directed graph G, its SCCs and the associated acyclic meta-graph GSCC give a structural decomposition of G that should be kept in mind.

2

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

3

DAGs arise in many application and topological sort is a key property in algorithm design. Linear time algorithms to compute a topological sort (there can be many possible orderings so not unique).

Chandra Chekuri (UIUC) CS374 58 Spring 2017 58 / 1