applications of bdf and dfs - indiana state universitycs.indstate.edu/~arash/algo2lec2.pdf · arash...

35
Applications of BDF and DFS Arash Rafiey January 14, 2016 Arash Rafiey Applications of BDF and DFS

Upload: others

Post on 22-Aug-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Applications of BDF and DFS

Arash Rafiey

January 14, 2016

Arash Rafiey Applications of BDF and DFS

Page 2: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

1 Deciding whether a graph is bipartite using BFS.

2 Finding connected components of a graph (both BFS andDFS) works.

3 Deciding whether a digraph is strongly connected.

4 Finding cut points in a graph.

5 Finding a cycle with the shortest length in a digraph.

6 Finding whether a digraph is acyclic and if so finding atopological ordering

7 Deciding whether a digraph is balanced

8 Coloring a digraph using a directed cycle of length 3, 4, ..(similar to 3-coloring)

Arash Rafiey Applications of BDF and DFS

Page 3: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Acyclic Digraphs and Topological Ordering

A digraph D is acyclic if it does not contain any directed cycle. Dis called DAG (directed acyclic graph).

DAG can be used to model the job scheduling with precedenceconstraint.

Suppose we want to schedule a set of jobs {J1, J2, . . . , Jn} wherethere are some dependencies between them (precedenceconstraint). For certain pair i , j , job Ji must be executed before

job Jj .

We want to find an ordering of the jobs respecting the precedenceconstraints.

Arash Rafiey Applications of BDF and DFS

Page 4: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Acyclic Digraphs and Topological Ordering

A digraph D is acyclic if it does not contain any directed cycle. Dis called DAG (directed acyclic graph).

DAG can be used to model the job scheduling with precedenceconstraint.

Suppose we want to schedule a set of jobs {J1, J2, . . . , Jn} wherethere are some dependencies between them (precedenceconstraint). For certain pair i , j , job Ji must be executed before

job Jj .

We want to find an ordering of the jobs respecting the precedenceconstraints.

Arash Rafiey Applications of BDF and DFS

Page 5: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Topological ordering

Let D be a digraph. We say an ordering v1, v2, . . . , vn of the nodesin D is a topological ordering if whenever vivj is an arc of D,i < j . In other words, all the arcs are forward and there is no

backward arc.

Lemma

Let D be an acyclic digraph. Then D has a node without in-degree.

Arash Rafiey Applications of BDF and DFS

Page 6: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Topological ordering

Let D be a digraph. We say an ordering v1, v2, . . . , vn of the nodesin D is a topological ordering if whenever vivj is an arc of D,i < j . In other words, all the arcs are forward and there is no

backward arc.

Lemma

Let D be an acyclic digraph. Then D has a node without in-degree.

Arash Rafiey Applications of BDF and DFS

Page 7: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Topological ordering

Theorem

Let D be a digraph. D has a topological ordering if and only if Dis acyclic.

Proof.

If D contains a directed cycle C then in every ordering of thevertices of C at least one arc is backward. Conversely if D isacyclic we show that there is a topological ordering. We followAC-Order algorithm.

AC-Order(D)

1.If D is empty then return.

2.Else Let v be a node without in neighbor in D.

3. Printout v .

4. Call AC-Order(D − v)

Arash Rafiey Applications of BDF and DFS

Page 8: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Topological ordering algorithm using queue

AC-Order(D)

1. Initial queue Q to be empty.

2. For every node v set the Indegre[v ] to be the number of nodeshaving arc to v .

3. For every vertex v , If (Indegree[v ] = 0 ) { Q.add(v); }

4. While Q is not empty

5. u = Q.delete();

6. Printout(u);

7. For every arc uw ∈ A(D)

8. Indegree[w ] = Indegree[w ]− 1;

9. If (Indeegree[w ] = 0)

10 Q.add(w);

The AC-Order(D) Algorithms runs in time O(|D|+ |E |).

Arash Rafiey Applications of BDF and DFS

Page 9: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Topological ordering algorithm using queue

AC-Order(D)

1. Initial queue Q to be empty.

2. For every node v set the Indegre[v ] to be the number of nodeshaving arc to v .

3. For every vertex v , If (Indegree[v ] = 0 ) { Q.add(v); }

4. While Q is not empty

5. u = Q.delete();

6. Printout(u);

7. For every arc uw ∈ A(D)

8. Indegree[w ] = Indegree[w ]− 1;

9. If (Indeegree[w ] = 0)

10 Q.add(w);

The AC-Order(D) Algorithms runs in time O(|D|+ |E |).Arash Rafiey Applications of BDF and DFS

Page 10: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Balanced Digraphs

Let C be a cycle (induced). We oriented each edge of C (givedirection) and we obtain an oriented cycle C ′

We say C ′ is balanced if the number of forward arcs and thenumber of backward arcs in C ′ are the same (in a clockwisedirection).

We say digraph D is balanced if each of its oriented cycle isbalanced.

Alternatively, digraph D is balanced if there is a partitioningV0,V1, . . . ,Vk−1 of its vertices such that each arc of D is from avertex in some Vi to a vertex in Vi+1.

Design an algorithm that decides whether a given digraph D isbalanced or not.

Arash Rafiey Applications of BDF and DFS

Page 11: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Balanced Checking

Balanced (v, level)

1. set `(v) = level ;2. for every unvisited arc vu3. set visit vu = true.4. if (`(u) exists && `(u) 6= `(v) + 1)5. print-out “Not balanced”6. exit(1);7. else8. Balanced(u, level+1);9. for every unvisited arc uv10 set visit uv = true.11. if (`(u) exists && `(u) 6= `(v)− 1)12. print-out “Not balanced”13. exit(1);14. else15. Balanced(u, level-1);

Arash Rafiey Applications of BDF and DFS

Page 12: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Directed cycle coloring

Let−→C3 be a directed cycle on three vertices.

V (−→C3) = {0, 1, 2} and arcs set A(

−→C3) = {01, 12, 20}

We say a digraph D can be colored by−→C3 if for every arc uv ∈ D

whenever u is colored by i (i=0,1,2) then v is colored by i + 1(sum is mod 3).

Design an algorithm that decides whether a given digraph D has a−→C3 coloring.

Design an algorithm that decides whether a given digraph D has a−→Ck coloring.

Arash Rafiey Applications of BDF and DFS

Page 13: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Directed cycle coloring

Let−→C3 be a directed cycle on three vertices.

V (−→C3) = {0, 1, 2} and arcs set A(

−→C3) = {01, 12, 20}

We say a digraph D can be colored by−→C3 if for every arc uv ∈ D

whenever u is colored by i (i=0,1,2) then v is colored by i + 1(sum is mod 3).

Design an algorithm that decides whether a given digraph D has a−→C3 coloring.

Design an algorithm that decides whether a given digraph D has a−→Ck coloring.

Arash Rafiey Applications of BDF and DFS

Page 14: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Directed cycle coloring

Let−→C3 be a directed cycle on three vertices.

V (−→C3) = {0, 1, 2} and arcs set A(

−→C3) = {01, 12, 20}

We say a digraph D can be colored by−→C3 if for every arc uv ∈ D

whenever u is colored by i (i=0,1,2) then v is colored by i + 1(sum is mod 3).

Design an algorithm that decides whether a given digraph D has a−→C3 coloring.

Design an algorithm that decides whether a given digraph D has a−→Ck coloring.

Arash Rafiey Applications of BDF and DFS

Page 15: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Strong Digraphs

A digraph D is called strong, if for every two vertices u, v of Dthere is a directed path from u to v and there is a directed pathfrom v to u. A directed cycle is a strong digraph.

A directed cycle is a strong digraph.

A strong component of D is a maximal subset U of D which isstrong.

Strong components are : S1 = {a, b, c , j}, S2 = {d , e}, andS3 = {f , g , h, i}.

Arash Rafiey Applications of BDF and DFS

Page 16: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Strong Digraphs

A digraph D is called strong, if for every two vertices u, v of Dthere is a directed path from u to v and there is a directed pathfrom v to u. A directed cycle is a strong digraph.

A directed cycle is a strong digraph.

A strong component of D is a maximal subset U of D which isstrong.

Strong components are : S1 = {a, b, c , j}, S2 = {d , e}, andS3 = {f , g , h, i}.

Arash Rafiey Applications of BDF and DFS

Page 17: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Strong Digraphs

A digraph D is called strong, if for every two vertices u, v of Dthere is a directed path from u to v and there is a directed pathfrom v to u. A directed cycle is a strong digraph.

A directed cycle is a strong digraph.

A strong component of D is a maximal subset U of D which isstrong.

Strong components are : S1 = {a, b, c , j}, S2 = {d , e}, andS3 = {f , g , h, i}.

Arash Rafiey Applications of BDF and DFS

Page 18: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Strong Digraphs

A digraph D is called strong, if for every two vertices u, v of Dthere is a directed path from u to v and there is a directed pathfrom v to u. A directed cycle is a strong digraph.

A directed cycle is a strong digraph.

A strong component of D is a maximal subset U of D which isstrong.

Strong components are : S1 = {a, b, c , j}, S2 = {d , e}, andS3 = {f , g , h, i}.

Arash Rafiey Applications of BDF and DFS

Page 19: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Strong Components of a digraph

Finding strong components in a digraph.

Input: digraph G = (V, E)

Output: set of strongly connected components (sets of vertices)

Explaining Tarjan’s algorithm :

1) The nodes are placed on a stack in the order in which they arevisited.

2) When the depth-first search recursively explores a node v andits descendants, we may not popped them from the stack. Becausethere maybe a node v descendant of u which may have a path to anode earlier on the stack.

Arash Rafiey Applications of BDF and DFS

Page 20: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Strong Components of a digraph

Finding strong components in a digraph.

Input: digraph G = (V, E)

Output: set of strongly connected components (sets of vertices)

Explaining Tarjan’s algorithm :

1) The nodes are placed on a stack in the order in which they arevisited.

2) When the depth-first search recursively explores a node v andits descendants, we may not popped them from the stack. Becausethere maybe a node v descendant of u which may have a path to anode earlier on the stack.

Arash Rafiey Applications of BDF and DFS

Page 21: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Strong Components of a digraph

Finding strong components in a digraph.

Input: digraph G = (V, E)

Output: set of strongly connected components (sets of vertices)

Explaining Tarjan’s algorithm :

1) The nodes are placed on a stack in the order in which they arevisited.

2) When the depth-first search recursively explores a node v andits descendants, we may not popped them from the stack. Becausethere maybe a node v descendant of u which may have a path to anode earlier on the stack.

Arash Rafiey Applications of BDF and DFS

Page 22: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

3) Each node v is assigned a unique integer index(v), The timewhen v is visited for the first time.

4) We maintain a value lowlink(v) that represents (roughlyspeaking) the smallest index of any node known to be reachablefrom v, including v itself.

5) Therefore v must be left on the stack if lowlink(v) < index(v)

Arash Rafiey Applications of BDF and DFS

Page 23: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

3) Each node v is assigned a unique integer index(v), The timewhen v is visited for the first time.

4) We maintain a value lowlink(v) that represents (roughlyspeaking) the smallest index of any node known to be reachablefrom v, including v itself.

5) Therefore v must be left on the stack if lowlink(v) < index(v)

Arash Rafiey Applications of BDF and DFS

Page 24: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

3) Each node v is assigned a unique integer index(v), The timewhen v is visited for the first time.

4) We maintain a value lowlink(v) that represents (roughlyspeaking) the smallest index of any node known to be reachablefrom v, including v itself.

5) Therefore v must be left on the stack if lowlink(v) < index(v)

Arash Rafiey Applications of BDF and DFS

Page 25: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

6) v must be removed as the root of a strongly connectedcomponent if lowlink(v) = index(v).

7) The value lowlink(v) is computed during the depth-first searchfrom v .

Arash Rafiey Applications of BDF and DFS

Page 26: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

6) v must be removed as the root of a strongly connectedcomponent if lowlink(v) = index(v).

7) The value lowlink(v) is computed during the depth-first searchfrom v .

Arash Rafiey Applications of BDF and DFS

Page 27: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Strong Components-Tarjan’s Algorithm(D)

function strongconnect(v)

1. index(v) := index; lowlink(v) := index;

2. index:= index + 1; Stack.push(v)

3. for each arc vw ∈ E do

6. if (index(w) is undefined)

7. strongconnect(w)

8. lowlink(v) :=min(lowlink(v), lowlink(w))

9. else if (w is in Stack)

10. lowlink(v) := min(lowlink(v), index(w))

// If v is a root node, pop the stack for new strong component11. if (lowlink(v) = index(v))12. repeat

13. w := Stack.pop() add w to current strong component14. until (w = v)15. output the current strongly component

Arash Rafiey Applications of BDF and DFS

Page 28: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Strong Components-Tarjan’s Algorithm(D)

1. index := 02. Stack := empty3. for each v in V do4. if (index(v) is undefined)5. strongconnect(v)

Arash Rafiey Applications of BDF and DFS

Page 29: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Biconnected Components

Let G = (V ,E ) be a loop-free connected undirected graph. Avertex v in G is called an articulation point if κ(G − v) > κ(G ).G − v has more connected components than v .

A loop-free connected undirected graph with no articulation pointsis called biconnected .

Arash Rafiey Applications of BDF and DFS

Page 30: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Finding Articulation Points

1) We traverse the graph in DFS (preorder) manner.

2) For vertex x of G define dfi(x) to be the index of x in DFS(time we visit x). If y is a descendant of x then dfi(x) < dfi(y).

3) Define low(x) =min{dfi(y)| y is adjacent in G to either x or a descendant of x}

Arash Rafiey Applications of BDF and DFS

Page 31: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Finding Articulation Points

1) We traverse the graph in DFS (preorder) manner.

2) For vertex x of G define dfi(x) to be the index of x in DFS(time we visit x). If y is a descendant of x then dfi(x) < dfi(y).

3) Define low(x) =min{dfi(y)| y is adjacent in G to either x or a descendant of x}

Arash Rafiey Applications of BDF and DFS

Page 32: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Let z be the parent of x in T . Then :

1) low(x) = dfi(z) : In this case Tx contains no vertex that isadjacent to an ancestor of z (by back edge).Hence z is an articulation point of G .If Tx contains no articulation points, then Tx together with edgezx is a biconnected component of G .Remove Tx and the edge xz and apply on the remaining subtree ofT .

2) If low(x) < dfi(z) : there is a descendant of z in Tx that isjoined (by a back edge in G) to an ancestor of z .

Arash Rafiey Applications of BDF and DFS

Page 33: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Let z be the parent of x in T . Then :

1) low(x) = dfi(z) : In this case Tx contains no vertex that isadjacent to an ancestor of z (by back edge).Hence z is an articulation point of G .If Tx contains no articulation points, then Tx together with edgezx is a biconnected component of G .Remove Tx and the edge xz and apply on the remaining subtree ofT .

2) If low(x) < dfi(z) : there is a descendant of z in Tx that isjoined (by a back edge in G) to an ancestor of z .

Arash Rafiey Applications of BDF and DFS

Page 34: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

Algorithm for Articulation Points

1 ) Find a DFS ordering x1, x2, . . . , xn of the vertices of G .

2) Start from xn and continue to xn−1, xn−2, . . . , x1 and determinelow(xj) as follows :

a) low ′(xj) = min{dfi(z)| z is adjacent in G to xj}b) If c1, c2, . . . , cm are children of xj , then

low(xj) = min{low ′(xj), low′(c1), low

′(c2), . . . , low′(cm)}

3) Let wj be the parent of xj in T . If low(xj) = dfi(wj) then wj isan articulation point of G , unless wj is the root of T and wj hasno child in T other than xj .Moreover, in either situation the subtree rooted at xj together withthe edge wjxj is part of a biconnected component of G .

Arash Rafiey Applications of BDF and DFS

Page 35: Applications of BDF and DFS - Indiana State Universitycs.indstate.edu/~arash/algo2lec2.pdf · Arash Rafiey Applications of BDF and DFS. 3) Each node v is assigned a unique integer

a e

fg

h

d

c

b

d(1)

c(3)

b(2) e(4)

a(5)

f(6)

g(7)

h(8)

h(7,7)

g(6,6)

f(1,1)

a(4,1)

e(1,1)

d(2,1)

b(1,1)

c(1,1)

h(8,7)

g(7,6)

f(6,1)

a(5,1)

e(4,1)

c(3,1)

b(2,1)

d(1,1)dd

g

h

g

f f

a

e

c

b

Arash Rafiey Applications of BDF and DFS