11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?

Slides:



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

CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort.
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.
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.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Tirgul 11 DFS Properties of DFS Topological sort.
Graphs-Part II Depth First Search (DFS). We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time – Visit all outgoing edges.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
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.
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.
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
CSC 331: Algorithm Analysis Decompositions of Graphs.
CS 312: Algorithm Design & Analysis Lecture #17: Connectedness in Graphs This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
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,
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
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.
Biconnected Components CS 312. Objectives Formulate problems as problems on graphs Implement iterative DFS Describe what a biconnected component is Be.
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.
 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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Topological Sort: Definition
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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)
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,
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
Introduction to Algorithms
Topological Sorting.
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.
Lecture 11 Graph Algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Topological Sort Minimum Spanning Tree
Lecture 12 Graph Algorithms
Topological Sort (an application of DFS)
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
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.
Trevor Brown DC 2338, Office hour M3-4pm
Lecture 11 Graph Algorithms
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

11 Graph Search Algorithms

2 What parts of the graph are reachable from a given vertex ?

3 Exploring a graph is like Navigating a Maze During the exploration process we will collect information. For each node, we will save the times of two important events: 1.The moment of first discovery (previsit) 2.Final departure (postvisit). We will define a simple counter clock, initially set to 1, which is advanced after every event.

4 Explore Input: G = (V,E); v  V Output: –visited(u) = true for all nodes u reachable from v –pre(u),post(u): start & end times Explore(G,v) 1.visited(v) = true 2.pre(v) = clock 3.clock ++ 4.for each edge (v, u)  E if not visited(u) { explore(G,u) } 5.post(v) = clock 6.clock ++

5 Explore on a Directed Graph BA EF c D G H D B A E F c G H D , ,14 8, ,11, 10,7, 6

6 DFS The explore procedure visits only parts of the graph reachable from its starting point. To examine the rest of the graph, we need to restart the procedure elsewhere, at some node that has not yet been visited. The algorithm depth-first search (DFS), does this repeatedly until the entire graph has been traversed. DFS calls explore to vertices which were not visited yet.

7 DFS DFS(G) for all v  V visited(v) = false // initialization for all v  V if not visited(v) explore(v)

8 Types of Edges A is the root of the search tree Everything else is its descendant. Similarly, E has descendants F, G, and H, and conversely, is an ancestor of these three nodes. C is the parent of D, which is its child. For undirected graphs we distinguish between tree edges and non-tree edges. In the directed case, there is a slightly more elaborate taxonomy. B A E F c G H D

9 Types of Edges-Directed Graph Tree edges are actually part of the DFS forest. Forward edges lead from a node to a non-child descendant in the Explore tree. Back edges lead to an ancestor in the Explore tree. Cross edges lead to neither descendant nor ancestor; they therefore lead to a node that has already been completely explored (that is, already post-visited). B A E F c G` H D

10 Parenthesis Structure Ancestor and descendant relationships, as well as edge types, can be read off directly from pre and post numbers. Because of our exploration strategy, vertex u is an ancestor of vertex v exactly in those cases where u is discovered first and v is discovered during explore(u). pre(u) < pre(v) < post(v) < post(u), which we can depict pictorially as two nested intervals: u uvv

11 Brackets Property For any nodes u and v, the two intervals [pre(u),post(u)] and [pre(v),post(v)] are either disjoint or one is contained within the other. Why? [pre(u),post(u)] is the time after the discovery of u and before the departure of u. Every node is assigned pre time stamp exactly once, and post time stamp exactly once. The algorithm will departure u only after exploring all the nodes discovered starting from him. u u v v Not possible !

12 Types of Edges-Directed Pre/post ordering for (u,v) Edge Type Tree/Forward Back Cross u uvv v vuu u u v v

13 Checking for Acyclicity We can test for acyclicity in linear time, with a single Explore search. Property: A directed graph has a cycle if and only if its Explore search reveals a back edge. Proof: “→”: if (u, v) is a back edge, then there is a cycle consisting of this edge together with the path from v to u in the search tree. “←”: if the graph has a cycle v 0 → v 1 → v 2 → … → v k → v 0, look at the first node on this cycle to be discovered (the node with the lowest pre number). Suppose it is v i. All the other v j on the cycle are reachable from it and will therefore be its descendants in the search tree. In particular, the edge v i-1 → v i (or v k → v 0 if i = 0) leads from a node to its ancestor and is thus by definition a back edge.

14 DAG A cycle in a directed graph is a circular path v 0 → v 1 → v 2 → … → v k → v 0 A graph without cycles is acyclic. DAG=Directed Acyclic Graphs Is this a DAG?

15 Getting Dressed undershorts pants belt socks shoes shirt tie jacket watch undershortspantsbeltsocksshoesshirttiejacketwatch

16 Topological Sort What types of DAGs can be linearized? All of them. And once again Explore search tells us exactly how to do it: simply perform tasks in decreasing order of their post numbers. The only edges (u, v) in a graph for which post(u) < post(v) are back edges (recall the table of edge types) and we have seen that a DAG can NOT have back edges.

17 Topological Sort –Algorithm I Property: In a DAG, every edge leads to a node with a lower post number. This gives a linear-time algorithm for ordering the nodes of a DAG. Sort the nodes by decreasing post numbers When DFS departures the t node, we can store it in index n-t in an output array. Time complexity: O(|E|+|V|) (DFS)

18 Source and Sink The node with the smallest post number comes last in this linearization, and it must be a sink: no outgoing edges. The node with the highest post is a source: a node with no incoming edges

19 Topological Sort-Algorithm II Property: Every DAG has at least one source and at least one sink. This suggests an approach to linearization: Find a source, output it, and delete it from the graph. Repeat until the graph is empty.

20 Algorithm II-Time Complexity We can save for each node the number of incoming edges (in-degree) When we erase a node u from the graph, we update the in-degree of all nodes that have an incoming edge from u (u,v). If the nodes are stored in a priority queue implemented by as heap and sorted by in- degree, complexity is: O(|E|log|V|+|V|log|V| )