CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.

Slides:



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

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
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.
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.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Applications of graph traversals
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 Data Structures DFS, Topological Sort Dana Shapira.
Connected Components, Directed Graphs, Topological Sort COMP171.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
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.
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.
CS 312: Algorithm Analysis Lecture #16: Strongly Connected Components This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
IS 2610: Data Structures Graph April 5, 2004.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Spring 2015 Lecture 10: Elementary Graph Algorithms
CSC 331: Algorithm Analysis Decompositions of Graphs.
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,
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs.
Biconnected Components CS 312. Objectives Formulate problems as problems on graphs Implement iterative DFS Describe what a biconnected component is Be.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Topological Sort: Definition
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
Properties and Applications of Depth-First Search Trees and Forests
Graphs and Paths : Chapter 15 Saurav Karmakar
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
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 ?
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
Lecture 7 Graph Traversal
Week 11 - Wednesday.  What did we talk about last time?  Exam 2  And before that:  Graph representations  Depth first search.
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,
Graph Search Applications, Minimum Spanning Tree
Introduction to Algorithms
Breadth-First Search (BFS)
Chapter 22 Elementary Graph Algorithms
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.
Chapter 3. Decompositions of Graphs
Lecture 11 Graph Algorithms
Lecture 12 Graph Algorithms
CS120 Graphs.
Graph Algorithms Using Depth First Search
Planarity Testing.
Lectures on Graph Algorithms: searching, testing and sorting
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Lecture 6 Graph Traversal
Lecture 11 Graph Algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
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:

CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch. 3 focuses on algorithms to find connectivity in graphs Ch. 4 focuses on algorithms to find paths within graphs G = (V,E) – V is a set of vertices (nodes) – E is a set of edges between vertices, can be directed or undirected Undirected: e = {x,y} Directed: e = (x,y) – Degree of a node is the number of impinging edges Nodes in a directed graph have an in-degree and an out-degree WWW is a graph – Directed or undirected?

Graph Representation – Adjacency Matrix n = |V| for vertices v 1, v 2, …, v n Adjacency Matrix A is n ✕ n with A is symmetric if it is undirected One step lookup to see if an edge exists between nodes n 2 size is wasteful if A is sparse (i.e. not highly connected) If densely connected then |E| ≈ |V| 2 CS 312 – Graph Algorithms2

Graph Representation – Adjacency List Adjacency List: n lists, one for each vertex Linked list for a vertex u contains the vertices to which u has an outgoing edge – For directed graphs each edge appears in just one list – For undirected graph each edge appears in both vertex lists Size is O(|V|+|E|) Finding if two vertices are connected is no longer constant time Which representation is best? CS 312 – Graph Algorithms3

Graph Representation – Adjacency List Adjacency List: n lists, one for each vertex Linked list for a vertex u contains the vertices to which u has an outgoing edge – For directed graphs each edge appears in just one list – For undirected graph each edge appears in both vertex lists Size is O(|V|+|E|) Finding if two vertices are connected is no longer constant time Which representation is best? – Depends on type of graph applications – If dense connectivity, Matrix is usually best – If sparse, then List is often best CS 312 – Graph Algorithms4

Depth-First Search of Undirected Graphs What parts of the graph are reachable from a given vertex – Reachable if there is a path between the vertices Note that in our representations the computer only knows which are the neighbors of a specific vertex Deciding reachability is like exploring a labyrinth – If we are not careful, we could miss paths, explore paths more than once, etc. – What algorithm do you use if you are at a cave entrance and want to find the cave exit on the other side? Chalk or string is sufficient Recursive stack will be our string, and visited(v)=true our chalk Why not just "left/right-most" with no chalk/visited? Depth first vs Breadth first? CS 312 – Graph Algorithms5

Explore Procedure DFS algorithm to find which nodes are reachable from an initial node v previsit(v) and postvisit(v) are optional updating procedures Complexity? CS 312 – Graph Algorithms6

Explore Procedure DFS algorithm to find which nodes are reachable from an initial node v previsit(v) and postvisit(v) are optional updating procedures Complexity – Each reachable edge e r checked exactly once (twice if undirected) – O(|E r |) CS 312 – Graph Algorithms7

Visiting Entire Graph An undirected graph is connected if there is a path between any pair of nodes Otherwise graph is made up of disjoint connected components Visiting entire graph is O(|V| + |E|) – Note this is the amount of time it would take just to scan through the adjacency list Why not just say O(|E|) since |V| is usually smaller than |E|? What do we accomplish by visiting entire graph? CS 312 – Graph Algorithms8

Previsit and Postvisit Orderings – Each pre and post visit is an ordered event 9 Account for all edges – Tree edges and Back edges

Previsit and Postvisit Orders CS 312 – Graph Algorithms10 DFS yields a forest (disjoint trees) when there are disjoint components in the graph Can use pre/post visit numbers to detect properties of graphs Account for all edges: Tree edges (solid) and back edges (dashed) Back edges detect cycles Properties still there even if explored in a different order (i.e. start with F, then J)

Depth-First Search in Directed Graphs Can use the same DFS algorithm as before, but only traverse edges in the prescribed direction – Thus, each edge just considered once (not twice like undirected) still can have separate edges in both directions (e.g. (e, b)) – Root node of DFS tree is A in this case (if we go alphabetically), All other nodes are descendants of A – Natural definitions for ancestor, parent, child in the DFS Tree CS 312 – Graph Algorithms11

Depth-First Search in Directed Graphs Tree edges and back edges (2 below) the same as before Added terminology for DFS Tree of a directed graph – Forward edges lead from a node to a nonchild descendant (2 below) – Cross edges lead to neither descendant or ancestor, lead to a node which has already had its postvisit (2 below) CS 312 – Graph Algorithms12

Back Edge Detection Ancestor/descendant relations, as well as edge types can be read off directly from pre and post numbers of the DFS tree – just check nodes connected by each edge Initial node of edge is u and final node is v Tree/forward reads pre(u) < pre(v) < post(v) < post(u) CS 312 – Graph Algorithms13 If G First?

DAG – Directed Acyclic Graph DAG – a directed graph with no cycles Very common in applications – Ordering of a set of tasks. Must finish pre-tasks before another task can be done How do we test if a directed graph is acyclic in linear time? CS 312 – Graph Algorithms14

DAG – Directed Acyclic Graph DAG – a directed graph with no cycles Very common in applications – Ordering of a set of tasks. Must finish pre-tasks before another task can be done How do we test if a directed graph is acyclic in linear time? Property: A directed graph has a cycle iff DFS reveals a back edge Just do DFS and see if there are any back edges How do we check DFS for back edges and what is complexity? CS 312 – Graph Algorithms15

DAG – Directed Acyclic Graph DAG – a directed graph with no cycles Very common in applications – Ordering of a set of tasks. Must finish pre-tasks before another task can be done How do we test if a directed graph is acyclic in linear time? Property: A directed graph has a cycle iff DFS reveals a back edge Just do DFS and see if there are any back edges How do we check DFS for back edges and what is complexity? O(|E|) – for edge check pre/post values of nodes – Could equivalently test while building the DFS tree CS 312 – Graph Algorithms16

DAG Properties Every DAG can be linearized – More than one linearization usually possible Algorithm to linearize – Property: In a DAG, every edge leads to a node with lower post number – Do DFS, then linearize by sorting nodes by decreasing post number – Which node do we start DFS at? Makes no difference, B will always have largest post (Try A and F) – Other linearizations may be possible Another property: Every DAG has at least one source and at least one sink – Source has no input edges – If multiple sources, linearization can start with any of them – Sink has no output edges Is the above directed graph connected – looks like it if it were undirected, but can any node can reach any other node? CS 312 – Graph Algorithms17

Alternative Linearization Algorithm Another linear time linearization algorithm This technique will help us in our next goal – Find a source node and put it next on linearization list How do we find source nodes? Do a DFS and and count in-degree of each node Put nodes with 0 in-degree in a source list Each time a node is taken from the source list and added to the linearization, decrement the in-degree count of all nodes to which the source node had an out link. Add any adjusted node with 0 in-degree to the source list – Delete the source node from the graph – Repeat until the graph is empty CS 312 – Graph Algorithms18

Strongly Connected Components Two nodes u and v in a directed graph are connected if there is a path from u to v and a path from v to u The disjoint subsets of a directed graph which are connected are called strongly connected components How could we make the entire graph strongly connected? CS 312 – Graph Algorithms19

Meta-Graphs Every directed graph is a DAG of its strongly connected components CS 312 – Graph Algorithms20 This meta-graph decomposition will be very useful in many applications (e.g. a high level flow of the task with subtasks abstracted)

Algorithm to Decompose a Directed Graph into its Strongly Connected Components explore(G,v) will terminate precisely when all nodes reachable from v have been visited If we could pick a v from any sink meta-node then call explore, it would explore that complete SCC and terminate We could then mark it as an SCC, remove it from the graph, and repeat CS 312 – Graph Algorithms21 How do we detect if a node is in a meta-sink? How about starting from the node with the lowest post-visit value? Won't work with arbitrary graphs (with cycles) which are what we are dealing with AB C

Algorithm to Decompose a Directed Graph into its Strongly Connected Components explore(G,v) will terminate precisely when all nodes reachable from v have been visited If we could pick a v from any sink meta-node then call explore, it would explore that complete SCC and terminate We could then mark it as an SCC, remove it from the graph, and repeat CS 312 – Graph Algorithms22 How do we detect if a node is in a meta-sink? Property: Node with the highest post in a DFS search must lie in a source SCC Just temporarily reverse the edges in the graph and do a DFS on G R to get post ordering Then node with highest post in DFS of G R, where it is in a source SCC, must be in a sink SCC in G Repeat until graph is empty: Pick node with highest remaining post score (from DFS on G R ), explore and mark that SCC in G, and then prune that SCC

Example Create G R by reversing graph G Do DFS on G R Repeat until graph G is empty: – Pick node with highest remaining post score (from DFS tree on G R ), and explore starting from that node in G – That will discover one sink SCC in G – Prune all nodes in that SCC from G and G R Complexity? – Create G R – Post-ordered list –add nodes as do dfs(G R ) – Do DFS with V reverse ordered from PO list visited flag = removed CS 312 – Graph Algorithms G GRGR

Biconnected Components If deleting vertex a from a component/graph G splits the graph then a is called a separating vertex (cut point, articulation point) of G A graph G is biconnected if and only if there are no separating vertices. That is, it requires deletion of at least 2 vertices to disconnect G. – Why might this be good in computer networks, etc? – A graph with just 2 connected nodes is also a biconnected component A bridge is an edge whose removal disconnects the graph Any 2 distinct biconnected components have at most one vertex in common a is a separating vertex of G if and only if a is common to more than one of the biconnected components of G CS 312 – Graph Algorithms24

Biconnected Algorithm – Some hints DFS can be used to to identify the biconnected components, bridges, and separating vertices of an undirected graph in linear time a is a separating vertex of G if and only if either 1. a is the root of the DFS tree and has more than one child, or 2. a is not the root, and there exists a child s of a such that there is no backedge between any descendent of s (including s) and a proper ancestor of a. low(u) = min(pre(u), pre(w)), where (v,w) is a backedge for some descendant v of u Use low to identify separating vertices, and run another DFS with an extra stack of edges to remove biconnected components one at a time CS 312 – Graph Algorithms25

CS 312 – Graph Algorithms26 Discovering Separating Vertices with BFS a is a separating vertex of G if and only if either 1. a is the root of the DFS tree and has more than one child, or 2. a is not the root, and there exists a child s of a such that there is no backedge between any descendent of s (including s) and a proper ancestor of a. a:1 DFS tree with pre-ordering e:5 d:4 c:3b:2 a e c d b

CS 312 – Graph Algorithms27 a a is a separating vertex of G if and only if either 1. a is the root of the DFS tree and has more than one child, or 2. a is not the root, and there exists a child s of a such that there is no backedge between any descendent of s (including s) and a proper ancestor of a. e c d b a:1,1 DFS tree with pre-ordering and low numbering e:5,3 d:4,3 c:3,3b:2,2 u is a sep. vertex iff there is any child v of u s.t. low(v) ≥ pre(u)

CS 312 – Graph Algorithms28 a a is a separating vertex of G if and only if either 1. a is the root of the DFS tree and has more than one child, or 2. a is not the root, and there exists a child s of a such that there is no backedge between any descendent of s (including s) and a proper ancestor of a. e c d b a:1,1 DFS tree with pre-ordering and low numbering e:5,1 d:4,1 c:3,1b:2,2 u is a sep. vertex iff there is any child v of u s.t. low(v) ≥ pre(u)

CS 312 – Graph Algorithms29 a a is a separating vertex of G if and only if either 1. a is the root of the DFS tree and has more than one child, or 2. a is not the root, and there exists a child s of a such that there is no backedge between any descendent of s (including s) and a proper ancestor of a. e c d b a:1,1 DFS tree with pre-ordering and low numbering e:5,1 d:4,1 c:3,1b:2,2 u is a sep. vertex iff there is any child v of u s.t. low(v) ≥ pre(u) f f:6,6

CS 312 – Graph Algorithms30 Example with pre and low a b c d e f g a,1 b,2 g,3 c,4 d,5 h,6 e,7 f,8 h pre numbering

CS 312 – Graph Algorithms31 Example a b c d e f g a,1,1 b,2,1 g,3,1 c,4,3 d,5,3 h,6,4 e,7,3 f,8,3 h low numbering u is a sep. vertex iff there is any child v of u s.t. low(v) ≥ pre(u)

CS 312 – Graph Algorithms32 Example with pre and low a b c d e f g h a,1,1 b,2,1 g,3,1 c,4,3 d,5,3 h,6,4 e,7,1 f,8,1 low numbering u is a sep. vertex iff there is any child v of u s.t. low(v) ≥ pre(u)

HW CS 312 – Graph Algorithms33 a e c d b

BIG TUNA CS 312 – Graph Algorithms34