CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer 2010 1.

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graph Searching CSE 373 Data Structures Lecture 20.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Connected Components, Directed Graphs, Topological Sort COMP171.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths Nicki Dell Spring 2014.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
CSE 332 Data Abstractions: Graphs and Graph Traversals Kate Deibel Summer 2012 July 23, 2012CSE 332 Data Abstractions, Summer
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
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.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
CSE 373: Data Structures & Algorithms Lecture 17: Topological Sort / Graph Traversals Linda Shapiro Winter 2015.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
CSE 373 Topological Sort Graph Traversals
CSE 373: Data Structures & Algorithms Graph Traversals: Dijkstra’s
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
Cse 373 May 8th – Dijkstras.
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths
Instructor: Lilian de Greef Quarter: Summer 2017
CS120 Graphs.
CSE 373: Data Structures & Algorithms Lecture 17: Topological Sort / Graph Traversals Linda Shapiro Spring 2016.
Paul Beame in lieu of Richard Anderson
Topological Sorting Minimum Spanning Trees Shortest Path
Presentation transcript:

CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer

Topological Sort 2 Problem: Given a DAG G=(V,E), output all the vertices in order such that if no vertex appears before any other vertex that has an edge to it Example input: Example output: 142, 126, 143, 311, 331, 332, 312, 341, 351, 333, 440, 352 CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Questions and comments 3  Why do we perform topological sorts only on DAGs?  Because a cycle means there is no correct answer  Is there always a unique answer?  No, there can be 1 or more answers; depends on the graph  What DAGs have exactly 1 answer?  Lists  Terminology: A DAG represents a partial order and a topological sort produces a total order that is consistent with it

Uses 4  Figuring out how to finish your degree  Computing the order in which to recompute cells in a spreadsheet  Determining the order to compile files using a Makefile  In general, taking a dependency graph and coming up with an order of execution

A first algorithm for topological sort 5 1. Label each vertex with its in-degree  Labeling also called marking  Think “write in a field in the vertex”, though you could also do this with a data structure (e.g., array) on the side 2. While there are vertices not yet output: a) Choose a vertex v with labeled with in-degree of 0 b) Output v and remove it (conceptually) from the graph c) For each vertex u adjacent to v (i.e. u such that (v,u) in E ), decrement the in-degree of u

Example 6 Output: Node: Removed? In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Example 7 Output: 126 Node: Removed? x In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Example 8 Output: Node: Removed? x x In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Example 9 Output: Node: Removed? x x x In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Example 10 Output: Node: Removed? x x x x In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Example 11 Output: Node: Removed? x x x x x In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Example 12 Output: Node: Removed? x x x x x x In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Example 13 Output: Node: Removed? x x x x x x x In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Example 14 Output: Node: Removed? x x x x x x x x In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Example 15 Output: Node: Removed? x x x x x x x x x In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

Example 16 Output: Node: Removed? x x x x x x x x x x x x In-degree: CSE 142CSE 143 CSE 331 CSE 311 CSE 351CSE 333 CSE 332 CSE 341 CSE 312 CSE 352 MATH 126 CSE 440 …

A couple of things to note 17  Needed a vertex with in-degree of 0 to start  No cycles  Ties between vertices with in-degrees of 0 can be broken arbitrarily  Potentially many different correct orders

Running time? 18  What is the worst-case running time?  Initialization O(|V|)  Sum of all find-new-vertex O(|V| 2 ) (because each O(|V|))  Sum of all decrements O(|E|) (assuming adjacency list)  So total is O(|V| 2 ) – not good for a sparse graph! labelEachVertexWithItsInDegree(); for(ctr=0; ctr < numVertices; ctr++){ v = findNewVertexOfDegreeZero(); put v next in output for each w adjacent to v w.indegree--; }

Doing better 19 The trick is to avoid searching for a zero-degree node every time!  Keep the “pending” zero-degree nodes in a list, stack, queue, box, table, or something  Order we process them affects output but not correctness or efficiency provided add/remove are both O(1) Using a queue: 1. Label each vertex with its in-degree, enqueue 0-degree nodes 2. While queue is not empty a) v = dequeue() b) Output v and remove it from the graph c) For each vertex u adjacent to v (i.e. u such that (v,u) in E ), decrement the in-degree of u, if new degree is 0, enqueue it

Running time now? 20  What is the worst-case running time?  Initialization: O(|V|)  Sum of all enqueues and dequeues: O(|V|)  Sum of all decrements: O(|E|) (assuming adjacency list)  So total is O(|E| + |V|) – much better for sparse graph! labelAllAndEnqueueZeros(); for(ctr=0; ctr < numVertices; ctr++){ v = dequeue(); put v next in output for each w adjacent to v { w.indegree--; if(w.indegree==0) enqueue(v); }

Graph Traversals 21 Next problem: For an arbitrary graph and a starting node v, find all nodes reachable (i.e., there exists a path) from v  Possibly “do something” for each node  Print to output, set some field, etc. Related:  Is an undirected graph connected?  Is a directed graph weakly / strongly connected?  For strongly, need a cycle back to starting node for all nodes Basic idea:  Keep following nodes  But “mark” nodes after visiting them, so the traversal terminates and processes each reachable node exactly once

Abstract idea 22 traverseGraph(Node start) { Set pending = emptySet(); pending.add(start) mark start as visited while(pending is not empty) { next = pending.remove() for each node u adjacent to next if(u is not marked) { mark u pending.add(u) }

Running time and options 23  Assuming add and remove are O(1), entire traversal is O(|E|)  The order we traverse depends entirely on add and remove  Popular choice: a stack “depth-first graph search” “DFS”  Popular choice: a queue “breadth-first graph search” “BFS”  DFS and BFS are “big ideas” in computer science  Depth: recursively explore one part before going back to the other parts not yet explored  Breadth: Explore areas closer to the start node first  Aside: These are important concepts in AI  Conceive of tree of all possible chess states  Traverse to find ‘optimal’ strategy

Example: trees 24  In a tree DFS and BFS are particularly easy to “see” A B DE C F HG DFS(Node start) { initialize stack s to hold start mark start as visited while(s is not empty) { next = s.pop() for each node u adjacent to next if(u is not marked) mark u and push onto s } A, C, F, H, G, B, E, D – The marking is because we support arbitrary graphs and we want to process each node exactly once

Example: trees 25  In a tree DFS and BFS are particularly easy to “see” A B DE C F HG BFS(Node start) { initialize queue q to hold start mark start as visited while(q is not empty) { next = q.dequeue() for each node u adjacent to next if(u is not marked) mark u and enqueue onto q } A, B, C, D, E, F, G, H A “level-order” traversal

Comparison 26  Breadth-first always finds shortest paths – “optimal solutions”  Why?  Better for “what is the shortest path from x to y”  But depth-first can use less space in finding a path  If longest path in the graph is p and highest out-degree is d then DFS stack never has more than d*p elements  But a queue for BFS may hold O(|V|) nodes  A third approach:  Iterative deepening (IDFS): Try DFS but don’t allow recursion more than K levels deep. If that fails, increment K and start the entire search over  Like BFS, finds shortest paths. Like DFS, less space.

Saving the path 27  Our graph traversals can answer the reachability question:  “Is there a path from node x to node y?”  But what if we want to actually output the path?  Like getting driving directions rather than just knowing it’s possible to get there!  Easy:  Instead of just “marking” a node, store the previous node along the path (when processing u causes us to add v to the search, set v.path field to be u)  When you reach the goal, follow path fields back to where you started (and then reverse the answer)  If just wanted path length, could put the integer distance at each node instead

Example using BFS 28 Seattle San Francisco Dallas Salt Lake City What is a path from Seattle to Tyler (Texas) – Remember marked nodes are not re-enqueued – Not shortest paths may not be unique Chicago Tyler