graph algorithms algorithm design and analysis victor adamchikcs 15-451 spring 2014 lecture 11feb...

31
Graph Algorithms Algorithm Design and Analysis Victor Adamchik CS 15-451 Spring 2014 Lecture 11 Feb 07, 2014 Carnegie Mellon University

Upload: justice-wylie

Post on 29-Mar-2015

216 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Graph Algorithms

Algorithm Design and Analysis

Victor Adamchik CS 15-451 Spring 2014

Lecture 11 Feb 07, 2014 Carnegie Mellon University

Page 2: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Plan:

DFSTopological SortingClassification of EdgesBiconnected Components

Page 3: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Graphs TraversalVisiting all vertices in a systematic order.

for all v in V do visited[v] = falsefor all v in V do if !visited[v] traversal(v)

traversal(v) { visited[v] = true for all w in adj(v) do if !visited[w] traversal(w) }

O(V + E)

Page 4: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Graphs Traversals

•Depth-First Search (DFS)•Breadth-First Search (BFS)

DFS uses a stack for backtracking.BFS uses a queue for bookkeeping

Page 5: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Properties of DFS

Property 1DFS visits all

the vertices in the connected componentProperty 2

The discovery edges labeled by DFS form a spanning tree of the connected component

1 2 3

5

9

13 14 15 16

10 11 12

6 7 8

4

Page 6: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Applications of DFS

• Determine the connected components of a graph

• Find cycles in a graph • Determine if a graph is bipartite.• Topologically sort in a directed graph• Find the biconnected components

Page 7: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Topological Sorting

B

A

C

E

DIt's easy to see that such an ordering exists.Find a vertex with zero in-degree. Print it, delete it from the graph, and repeat.

Complexity-?

PQ wrt in-degrees. O( E log V)

Find an ordering of the vertices such that all edges go forward in the ordering.

Page 8: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Topological Sorting with DFS

B

A

C

E

D

DFS (v) { visited[v] = true for all w in adj(v) do if !visited[w] DFS (w); print(v); }

Do DFS;Reverse the order;

Complexity-?O( E + V)

Page 9: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Classification of Edges with DFS

B

CC

F

Tree edge

Back edge

Cross edge

Forwardedge

F

ABC

D E

123

4 5 6

Page 10: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Tree edges - are edges in the DFS

Classification of Edges

Forward edges – edges (u,v) connecting u to a descendant v in a depth-first tree

Back edges – edges (u,v) connecting u to an ancestor v in a depth-first tree

Cross edges – all other edges

Page 11: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Theorem.A directed graph is acyclic iff a DFS yields no back edges.

DAG

Page 12: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Theorem.A directed graph is acyclic iff a DFS yields no back edges.

DAG

Proof.=>) by contrapositive.If there is a back edge, the graph is surely cyclic.

Page 13: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Theorem.A directed graph is acyclic iff a DFS yields no back edges.

Proof.<=) Suppose there is a cycle.Let v be the first vertex discovered in the cycle. Let (u, v) be the preceding edge in this cycle. When we push v on the stack, no any vertices on the cycle were discovered yet. Thus, vertex u becomes a descendent of v in DFS. Therefore, (u, v) is a back edge.

Page 14: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

for all v in V do num[v] = 0, stack[v]=falsefor all v in V do if num[v]==0 DFS(v)k = 0;

DFS(v) { k++; num[v] = k; stack[v]=true for all w in adj(v) do if num[w]==0 DFS(w) tree edge else if num[w] > num[v] forward edge else if stack[w] back edge else cross edge stack [v]=false }

Page 15: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Biconnectivity

B

A

C D

E

In many applications it’s not

enough to know that a graph is connected, but “how well” it’s

connected.

Page 16: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Articulation points

B

A

C D

E

A vertex is an articulation point if its removal (with

edges) disconnect a graph.

A connected graph is biconnected if it has no articulation

points.

If a graph is not biconnected, we define the biconnected

components

Page 17: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Biconnected Components

C D

Biconnected graphs are of great interest in

communication and transportation

networksB

A

C

E

If a graph is not biconnected, we

define the biconnected components

Page 18: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Find articulation points

Fred Hacker’s algorithm:

Delete a vertexRun DFS to see if a graph is connectedChoose a new vertex. Repeat.

Complexity: O(V (V+E))

Page 19: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Biconnected Component Algorithm

• It is based on a DFS• We assume that G is undirected and

connected.• We cannot distinguish between

forward and back edges• Also there are no cross edges (!)

Page 20: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

If for some child, there is no back

edge going to an ancestor of u, then u is an articulation point.

Find articulation point:an observation

u We need to keep a track of back edges!We keep a track of back

edge that goes higher in the tree.

Page 21: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Find articulation point:next observation

What about the root? Can it be an articulation point?

DFS root must have two or more children

Page 22: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Biconnected Component Algorithm

• Run DFS• When we reach a dead end, we will

back up. On the way up, we will discover back edges. They will tell us how far in the tree we could have gone.

• These back edges indicate a cycle in the graph. All nodes in a cycle must be in the same component.

Page 23: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Bookkeeping

• For each vertex we will store two indexes. One is the counter of nodes we have visited so far dfs[v]. Second - the back index low[v].

• Definition.low[v] is the DFS number of the lowest

numbered vertex x (i.e. highest in the tree) such that there is a back edge from some descendent of v to x.

Page 24: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

How to compute low[v]?

• Back edge (u, v) low[u] = min( low[u], dfs[v] )If the edge goes to a lower dfs value then the previous back edge, make this the new

low.

• Tree edge (u, v) low[u] = min( low[u], low[v] )Vertices u and v are in the same cycle.

Page 25: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

How to test for articulation point?

Using low[u] value we can test whether u is an articulation point.

If for some child, there is no back edge going to

an ancestor of u, then u is an articulation point.If there was a back edge from child v,

than low[v] < dfs[u].

It follows, u is an articulation point iff it has a child v such that low[v] >= dfs[u].

Page 26: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

The Algorithm

F G

A B C

H

D

I

E

1/12/23/3

4/4

Back edges

5/55/2

Backtracking

4/2

3/2

Vertex labels dfs/low

low(A) = dfs(B)Remove bicomponent

GFABAll edges are on a stack

6/6 7/7

8/8 9/9

low(E)>dsf(D)Remove bicomponent

DE

Back edge to C

8/1

7/16/1

2/1

Backtracking to BBicomponent CBHIDE

Store edges on a stack as you run

DFS

Page 27: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Theorem : Let G = (V, E) be a connected, undi-rected graph and S be a depth-first tree of G. Vertex x is an articulation point of G if and only if one of the following is true:

(1) x is the root of S and x has two or more children in S. (2) x is not the root and for some child s of x, there is no back edge between any descen-dant of s (including s itself) and a proper ances-tor of x.

Page 28: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Theorem : Let G = (V, E) be a connected, undi-rected graph and S be a depth-first tree of G. Vertex x is an articulation point of G if and only if one of the following is true: (1) x is the root of S and x has two or more children in S. Proof: Let two of the children be v and w. Imag-ine a subtree rooted at v and another one rooted at w. There is no an edge between these trees! This is because the DFS tree has no cross edges. Thus, any path must go through the root. If we delete the root, we disconnect the graph.Conversely, suppose the root has one child. Clearly, deleting the root won’t disconnect the graph.

Page 29: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Theorem : Let G = (V, E) be a connected, undi-rected graph and S be a depth-first tree of G. Vertex x is an articulation point of G if and only if one of the following is true: (2) x is not the root and for some child s of x, there is no back edge between any descen-dant of s (including s itself) and a proper ances-tor of x.

Proof: =>) If x is an articulation vertex, then removing it will disconnect child s from the par-ent of x.<=) If there is no such s, then x is not articula-tion point. To see this, suppose v0 is the parent and v1,…,vk are all children. By our assumption, there exists a path from vi to v0. They are in the same connected components. Removing x, won’t disconnect the graph.

Page 30: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

A B

D E

7/7

3/3

2/2

8/8

I

G

J

H

1/1

4/4

6/6

5/5

C

F

9/9

10/10

6/3

5/34/3

low(I)=dfs(G)

G

7/1

3/1

2/1

A

low(D)=dfs(A)

10/8

9/8B

Page 31: Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS 15-451 Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

A

D

A

I

G

J

HG

G

BE

C

F

B

AA B