Graphs.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
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.
Graphs - II Algorithms G. Miller V. Adamchik CS Spring 2014 Carnegie Mellon University.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
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.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
Graphs – Depth First Search ORD DFW SFO LAX
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
Applications of graph traversals
Applications of DFS: Articulation Points and Biconnected Components
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
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.
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.
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
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
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.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Topological Sort: Definition
Introduction to Graph Theory Lecture 17: Graph Searching Algorithms.
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Properties and Applications of Depth-First Search Trees and Forests
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 361 – Chapter 13 Graph Review Purpose Representation Traversal Comparison with tree.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
Graph Search Applications, Minimum Spanning Tree
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Topological Sort (an application of DFS)
Graph Algorithms Using Depth First Search
Graph Search Applications
Many slides here are based on E. Demaine , D. Luebke slides
Graph Algorithms – 2 DAGs Topological order
Lecture 10 Algorithm Analysis
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Elementary graph algorithms Chapter 22
Topological Sort (an application of DFS)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Bridges and Articulation Points
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:

Graphs

Graphs Applications of Depth-First Search Undirected graphs: Connected components, articulation points, bridges, biconnected components Directed graphs: Cyclic/acyclic graphs Topological sort Strongly connected components

Connectivity, connected components An undirected graph is called a connected graph if there is a path between any two vertices. A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices in the supergraph.

Connected components – Example 1 5 2 7 6 3 4

Finding Connected Comps by DFS DFS-VISIT(G,s) reaches all nodes that are in the same connected component as s The number of connected components is equal with the number of calls of DFS-VISIT from DFS

Articulation points, Bridges, Biconnected Components Let G = (V;E) be a connected, undirected graph. An articulation point of G is a vertex whose removal disconnects G. A bridge of G is an edge whose removal disconnects G. A biconnected component of G is a maximal set of edges such that any two edges in the set lie on a common simple cycle These concepts are important because they can be used to identify vulnerabilities of networks

Articulation points – Example 1 5 7 2 6 3 4 8

How to find all articulation points ? Brute-force approach: one by one remove all vertices and see if removal of a vertex causes the graph to disconnect: For every vertex v, do : Remove v from graph See if the graph remains connected (use BFS or DFS) If graph is disconnected, add v to AP list Add v back to the graph Time complexity of above method is O(V*(V+E)) for a graph represented using adjacency list. Can we do better?

How to find all articulation points ? DFS- based-approach: We can prove following properties: The root of a DFS-tree is an articulation point if and only if it has at least two children. A nonroot vertex v of a DFS-tree is an articulation point of G if and only if has a child s such that there is no back edge from s or any descendant of s to a proper ancestor of v. Leafs of a DFS-tree are never articulation points

Finding articulation points by DFS 1 2 3 4 5 6 7 8 2 1 4 3 5 6 Case 1: The root of the DFS-tree is an AP if and only if it has at least 2 children 7 Node 2 is an AP because any node from the first subtree (1, 2) is connected to any node from the second subtree (4, 5, 6, 7, 8) by a path that includes node 2. If node 2 is removed, the 2 subtrees are disconnected 8

Finding articulation points by DFS 1 2 3 4 5 6 7 8 2 1 4 3 5 6 Case 2: A non-root node of the DFS-tree is an AP if it has a child that is not connected (directly or through its descendants) by back edges to an ancestor 7 8 Node 6 is an AP because its child node 7 is not connected by back edges to an ancestor of 6

Finding articulation points by DFS 1 2 3 4 5 6 7 8 2 1 4 3 5 6 Case 2: A non-root node of the DFS-tree is an AP if it has a child that is not connected (directly or through its descendants) by back edges to an ancestor 7 How can we efficiently implement the test for this case ? 8 Node 6 is an AP because its child node 7 is not connected by back edges to an ancestor of 6 The LOW function !

Reminder: DFS – v.d and v.f 1/17 DFS associates with every vertex v its discovery time and its finish time v.d /v.f 2 2/5 7/16 1 4 3/4 8/15 3 5 9/14 The discovery time of a node v is smaller than the discovery time of any node which is a descendant of v in the DFS-tree. A back-edge leads to a node with a smaller discovery time (a node above it in the DFS-tree). 6 10/13 7 11/12 8

u is articulation point if it has a descendant The LOW function 1/17 Low=1 2 2/5 7/16 Low=1 1 4 Low=1 3/4 8/15 Low=1 3 5 Low=7 9/14 The LOW function: LOW(u) = the highest ancestor (identified by its smallest discovery time) of u that can be reached from a descendant of u by using back-edges 6 Low=9 10/13 7 Low=9 u is articulation point if it has a descendant v with LOW(v)>=u.d 11/12 8 Low=9

Finding Articulation Points Algorithm principle: During DFS, calculate also the values of the LOW function for every vertex After we finish the recursive search from a child v of a vertex u, we update u.low with the value of v.low. Vertex u is an articulation point, disconnecting v, if v.low >=u.d If vertex u is the root of the DFS tree, check whether v is its second child When encountering a back-edge (u,v) update u.low with the value of v.d

DFS_VISIT_AP(G, u) time=time+1 u.d=time u.color=GRAY u.low=u.d for each v in G.Adj[u] if v.color==WHITE v.pi=u DFS_VISIT_AP(G,v) if (u.pi==NIL) if (v is second son of u) “u is AP” // Case 1 else u.low=min(u.low, v.low) if (v.low>=u.d) “u is AP” // Case 2 else if ((v<>u.pi) and (v.d <u.d)) u.low=min(u.low, v.d) u.color=BLACK u.f=time

Bridge edges – Example 1 2 3 4 5 6

How to find all bridges ? Brute-force approach: one by one remove all edges and see if removal of an edge causes the graph to disconnect: For every edge e, do : Remove e from graph See if the graph remains connected (use BFS or DFS) If graph is disconnected, add e to B list Add e back to the graph Time complexity of above method is O(E*(V+E)) for a graph represented using adjacency list. Can we do better?

How to find all bridges ? DFS- approach: An edge of G is a bridge if and only if it does not lie on any simple cycle of G. if some vertex u has a back edge pointing to it, then no edge below u in the DFS tree can be a bridge. The reason is that each back edge gives us a cycle, and no edge that is a member of a cycle can be a bridge.  if we have a vertex v whose parent in the DFS tree is u, and no ancestor of v has a back edge pointing to it, then (u, v) is a bridge.

(u,v) is a bridge if LOW(v)>u.d Finding bridges by DFS 1/12 1 2 3 4 5 6 Low=1 1 2/5 Low=1 2 6/11 Low=6 3/4 3 5 Low=1 7/10 4 Low=6 8/9 6 Low=6 (u,v) is a bridge if LOW(v)>u.d

DFS_VISIT_Bridges(G, u) time=time+1 u.d=time u.color=GRAY u.low=u.d for each v in G.Adj[u] if v.color==WHITE v.pi=u DFS_VISIT_AP(G,v) u.low=min(u.low, v.low) if (v.low>u.d) “(u,v) is Bridge” else if ((v<>u.pi) and (v.d <u.d)) u.low=min(u.low, v.d) u.color=BLACK u.f=time

Biconnected components – Example 1 5 2 6 3 4 7 8

Finding biconnected components Two biconnected components cannot have a common edge, but they can have a common vertex -> We will mark the edges with an id of their biconnected component The common vertex of several biconnected components is an articulation point The articulation points separate the biconnected components of a graph. If the graph has no articulation points, it is biconnex -> We will try to identify the biconnected components while searching for articulation points

Finding biconnected components 1 2 3 5 4 6 7 8

Finding biconnected components Algorithm principle: During DFS, use a stack to store visited edges (tree edges or back edges) After we finish the recursive search from a child v of a vertex u, we check if u is an articulation point for v. If it is, we output all edges from the stack until (u,v). These edges form a biconnected component When we return to the root of the DFS-tree, we have to output the edges even if the root is no articulation point (graph may be biconnex) – we will not test the case of the root being an articulation point

DFS_VISIT_BiconnectedComp(G, u) time=time+1 u.d=time u.color=GRAY u.low=u.d u.AP=false for each v in G.Adj[u] if v.color==WHITE v.pi=u EdgeStack.push(u,v) DFS_VISIT_AP(G,v) u.low=min(u.low, v.low) if (v.low>=u.d) pop all edges from EdgeStack until (u,v) these are the edges of a Biconn Comp else if ((v<>u.pi) and (v.d <u.d)) u.low=min(u.low, v.d) u.color=BLACK u.f=time

Applications of DFS DFS has many applications For undirected graphs: Connected components Connectivity properties For directed graphs: Finding cycles Topological sorting Connectivity properties: Strongly connected components

Directed Acyclic Graphs A directed acyclic graph or DAG is a directed graph with no directed cycles 1 1 2 2 3 3 acyclic cyclic

DFS and cycles in graph A graph G is acyclic if a DFS of G results in no back edges u v w 1/ 2/ 4/ 3/ x y z

Topological Sort Topological sort of a DAG (Directed Acyclic Graph): Linear ordering of all vertices in a DAG G such that vertex u comes before vertex v if there is an edge (u, v)  G This property is important for a class of scheduling problems

Example – Topological Sorting u v w x y z There can be several orderings of the vertices that fulfill the topological sorting condition: u, v, w, y, x, z w, z, u, v, y, x w, u, v, y, x, z …

Topological Sorting Time: O(V+E) Algorithm principle: Call DFS to compute finishing time v.f for every vertex As every vertex is finished (BLACK) insert it onto the front of a linked list Return the list as the linear ordering of vertexes Time: O(V+E)

Using DFS for Topological Sorting

Correctness of Topological Sort Claim: (u,v)  G  u.f > v.f When (u,v) is explored, u is grey v = grey  (u,v) is back edge. Contradiction, since G is DAG and contains no back edges v = white  v becomes descendent of u  v.f < u.f (since it must finish v before backtracking and finishing u) v = black  v already finished  v.f < u.f

Applications of DFS DFS has many applications For undirected graphs: Connected components Connectivity properties For directed graphs: Finding cycles Topological sorting Connectivity properties: Strongly connected components

Strongly Connected Components A strongly connected component of a directed graph G=(V,E) is a maximal set of vertices C such that for every pair of vertices u and v in C, both vertices u and v are reachable from each other.

Strongly connected components - Example b d c e f g h

Strongly connected components – Example – The Component Graph cd abe h fg The Component Graph results by collapsing each strong component into a single vertex

The Component Graph Property: The Component Graph is a DAG (directed acyclic graph)

Strongly connected components Strongly connected components of a directed graph G Algorithm principle: Call DFS(G) to compute finishing times u.f for every vertex u Compute GT Call DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing u.f as computed in step 1 Output the vertices of each DFS-tree formed in step 3 as the vertices of a strongly connected component

Strongly connected components - Example Step1: call DFS(G), compute u.f for all u a b d c 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 e f g h

Strongly connected components - Example Step2: compute GT a b d c 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 e f g h

Strongly connected components - Example Step3: call DFS(GT), consider vertices in order of decreasing u.f a b d c 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 e f g h

Strongly connected components - Example Step3: call DFS(GT), consider vertices in order of decreasing u.f a b d c 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 e f g h

Strongly connected components - Example Step3: call DFS(GT), consider vertices in order of decreasing u.f a b d c 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 e f g h

Strongly connected components - Example Step3: call DFS(GT), consider vertices in order of decreasing u.f a b d c 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 e f g h

Strongly connected components - Example Step3: call DFS(GT), consider vertices in order of decreasing u.f a b d c 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 e f g h

Proof of Strong Components Algorithm Theorem The Algorithm presented before finds the strong components of G. Proof We must show that a set of vertices forms a strong components if and only if they are vertices of a tree in the DFS-forest of GT

Proof (cont) Suppose that v,w are vertices in the same strong component There is a DFS search in GT which starts at a vertex r and reaches v. Since v, w are in the same strong component, there is a path from v to w and from w to v. => then w will also be reached in this DFS search => Vertices v,w belong to the same spanning tree of GT.

Proof (cont) Suppose v,w are two vertices in the same DFS-spanning tree of GT. Let r be the root of that spanning tree. Then there exists paths in GT from r to each of v and w. So there exists paths in G to r from each of v and w.

Proof (cont) We will prove that there are paths in G from r to v and w as well We know that r.f > v.f (when r was selected as a root of its DFS-tree) If there is no path from r to v, then it is also no path from v to r, which is a contradiction Hence, there exists path in G from r to v, and similar argument gives path from r to w. So v and w are in a cycle of G and must be in the same strong component.

Summary Applications of Depth-First Search Undirected graphs: Connected components, articulation points, bridges, biconnected components Directed graphs: Cyclic/acyclic graphs Topological sort Strongly connected components