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

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
§5 Minimum Spanning Tree 【 Definition 】 A spanning tree of a graph G is a tree which consists of V( G ) and a subset of E( G ) 〖 Example 〗 A complete.
Graphs - II Algorithms G. Miller V. Adamchik CS Spring 2014 Carnegie Mellon University.
CS203 Lecture 15.
Graph Algorithms - 4 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 14Feb 14, 2014Carnegie Mellon University.
Trees Chapter 11.
CS38 Introduction to Algorithms Lecture 2 April 3, 2014.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs – Depth First Search ORD DFW SFO LAX
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Applications of graph traversals
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Applications of DFS: Articulation Points and Biconnected Components
Jeffrey D. Ullman Stanford University Flow Graph Theory.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
1 Data Structures DFS, Topological Sort Dana Shapira.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
CS Data Structures Chapter 6 Graphs.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Elementary graph algorithms Chapter 22
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
IS 2610: Data Structures Graph April 5, 2004.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Graphs.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
Topological Sort: Definition
Properties and Applications of Depth-First Search Trees and Forests
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Graphs 2015, Fall Pusan National University Ki-Joune Li.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
5. Biconnected Components of A Graph If one city’s airport is closed by bad weather, can you still fly between any other pair of cities? If one computer.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
CS 361 – Chapter 13 Graph Review Purpose Representation Traversal Comparison with tree.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Breadth-First Search (BFS)
Chapter 22 Elementary Graph Algorithms
Graph Algorithms Using Depth First Search
Elementary graph algorithms Chapter 22
Applications of DFS Topological sort (for directed acyclic graph)
CSE 421: Introduction to Algorithms
Richard Anderson Autumn 2016 Lecture 5
Subgraphs, Connected Components, Spanning Trees
A Introduction to Computing II Lecture 13: Trees
Richard Anderson Winter 2009 Lecture 6
Elementary graph algorithms Chapter 22
Applications of DFS: Articulation Points and Biconnected Components
Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some.
Presentation transcript:

Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University

Plan: DFS Topological Sorting Classification of Edges Biconnected Components

Graphs Traversal Visiting all vertices in a systematic order. for all v in V do visited[v] = false for 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)

Graphs Traversals Depth-First Search (DFS) Breadth-First Search (BFS) DFS uses a stack for backtracking. BFS uses a queue for bookkeeping

Properties of DFS Property 1 DFS visits all the vertices in the connected component Property 2 The discovery edges labeled by DFS form a spanning tree of the connected component

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

Topological Sorting B A C E D It'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.

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)

Classification of Edges with DFS B CC F Tree edge Back edge Cross edge Forward edge F A B C D E

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

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

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.

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.

for all v in V do num[v] = 0, stack[v]=false for 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 }

Biconnectivity B A CD E In many applications its not enough to know that a graph is connected, but how well its connected.

Articulation points B A CD 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

Biconnected Components CD Biconnected graphs are of great interest in communication and transportation networks B A C E If a graph is not biconnected, we define the biconnected components

Equivalent classes partitioning of the edge set CD Define u v, if edges u and v lie on a simple cycle B A C E The equivalent class of as called a biconnected component. We need to prove that is an equivalence relation

Equivalence Relation Reflexivity v v. It follows from a fact that an edge itself is a simple cycle. Symmetry if v u then u v Edges can be interchanged in the definition. Transitivity if v u and u w then v w Join two cycles

Find articulation points Fred Hackers algorithm: Delete a vertex Run DFS to see if a graph is connected Choose a new vertex. Repeat. Complexity: O(V (V+E))

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 (!)

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.

Find articulation point: next observation What about the root? Can it be an articulation point? DFS root must have two or more children

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.

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.

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.

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].

The Algorithm FG ABC 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 GFAB All edges are on a stack 6/6 7/7 8/89/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

Theorem : Let G = (V, E) be a connected, undirected 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 descendant of s (including s itself) and a proper ancestor of x.

Theorem : Let G = (V, E) be a connected, undirected 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. Imagine 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 wont disconnect the graph.

Theorem : Let G = (V, E) be a connected, undirected 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 descendant of s (including s itself) and a proper ancestor of x. Proof: =>) If x is an articulation vertex, then removing it will disconnect child s from the parent of x. <=) If there is no such s, then x is not articulation point. To see this, suppose v 0 is the parent and v 1,…,v k are all children. By our assumption, there exists a path from v i to v 0. They are in the same connected components. Removing x, wont disconnect the graph.

AB DE 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/3 4/3 low(I)=dfs(G) G 7/1 3/1 2/1 A low(D)=dfs(A) 10/8 9/8 B

A D A I G J HG G B E C F B AA B