CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9

Slides:



Advertisements
Similar presentations
1. Find the cost of each of the following using the Nearest Neighbor Algorithm. a)Start at Vertex M.
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due.
Chapter 9: Graphs Topological Sort
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
Topological Sort and Hashing
Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Connected Components, Directed Graphs, Topological Sort COMP171.
Lecture 16 CSE 331 Oct 9, Announcements Hand in your HW4 Solutions to HW4 next week Remember next week I will not be here so.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
מבנה המחשב – מבוא למחשבים ספרתיים Foundations of Combinational Circuits תרגול מספר 3.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Section 13  Questions  Graphs. Graphs  A graph representation: –Adjacency matrix. Pros:? Cons:? –Neighbor lists. Pros:? Cons:?
Topological Sort: Definition
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
SSSP in DAGs (directed acyclic graphs). DFS (depth first search) DFS(vertex v) { v.visited = TRUE; for each w adjacent to v do if(!w.visited) then dfs(w);
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
Topological Sort. Sorting technique over DAGs (Directed Acyclic Graphs) It creates a linear sequence (ordering) for the nodes such that: –If u has an.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Topological Sorting.
Chapter 22 Elementary Graph Algorithms
CSE 373 Topological Sort Graph Traversals
MA/CSSE 473 Day 13 Finish Topological Sort Permutation Generation
Topological Sort In this topic, we will discuss: Motivations
CISC 235: Topic 10 Graph Algorithms.
SINGLE-SOURCE SHORTEST PATHS IN DAGs
More Graph Algorithms.
Topological Sort.
Topological Sort.
Paul Beame in lieu of Richard Anderson
"Learning how to learn is life's most important skill. " - Tony Buzan
CSE 373 Data Structures and Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Connected Components, Directed Graphs, Topological Sort
Directed Graph Algorithms
Directed Graph Algorithms
Richard Anderson Autumn 2016 Lecture 5
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Richard Anderson Winter 2009 Lecture 6
Lecture 15 CSE 331 Oct 3, 2011.
CSE 417: Algorithms and Computational Complexity
GRAPHS Lecture 17 CS2110 Spring 2018.
CSE 373: Data Structures and Algorithms
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
Presentation transcript:

CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9 slides created by Marty Stepp http://www.cs.washington.edu/373/ © University of Washington, all rights reserved.

Ordering a graph Suppose we have a directed acyclic graph (DAG) of courses, and we want to find an order in which the courses can be taken. Must take all prereqs before you can take a given course. Example: [142, 143, 140, 154, 341, 374, 331, 403, 311, 332, 344, 312, 351, 333, 352, 373, 414, 410, 417, 413, 415] There might be more than one allowable ordering. How can we find a valid ordering of the vertices? 142 143 154 140 311 312 331 351 333 341 344 403 352 373 410 332 374 415 413 417 414

Topological Sort topological sort: Given a digraph G = (V, E), a total ordering of G's vertices such that for every edge (v, w) in E, vertex v precedes w in the ordering. Examples: determining the order to recalculate updated cells in a spreadsheet finding an order to recompile files that have dependencies (any problem of finding an order to perform tasks with dependencies) 142 143 154 140 311 312 331 351 333 341 344 403 352 373 410 332 374 415 413 417 414

Topo sort example How many valid topological sort orderings can you find for the vertices in the graph below? [A, B, C, D, E, F], [A, B, C, D, F, E], [A, B, D, C, E, F], [A, B, D, C, F, E], [B, A, C, D, E, F], [B, A, C, D, F, E], [B, A, D, C, E, F], [B, A, D, C, F, E], [B, C, A, D, E, F], [B, C, A, D, F, E], ... What if there were a new vertex G unconnected to the others? C A F D B E

Topo sort: Algorithm 1 function topologicalSort(): ordering := { }. Repeat until graph is empty: Find a vertex v with in-degree of 0 (no incoming edges). (If there is no such vertex, the graph cannot be sorted; stop.) Delete v and all of its outgoing edges from the graph. ordering += v . C A F D B E

Topo sort example function topologicalSort(): ordering := { }. Repeat until graph is empty: Find a vertex v with in-degree of 0 (no incoming edges). (If there is no such vertex, the graph cannot be sorted; stop.) Delete v and all of its outgoing edges from the graph. ordering += v . ordering = { B } C A F D B E

Topo sort example function topologicalSort(): ordering := { }. Repeat until graph is empty: Find a vertex v with in-degree of 0 (no incoming edges). (If there is no such vertex, the graph cannot be sorted; stop.) Delete v and all of its outgoing edges from the graph. ordering += v . ordering = { B, C } C A F D B E

Topo sort example function topologicalSort(): ordering := { }. Repeat until graph is empty: Find a vertex v with in-degree of 0 (no incoming edges). (If there is no such vertex, the graph cannot be sorted; stop.) Delete v and all of its outgoing edges from the graph. ordering += v . ordering = { B, C, A } C A F D B E

Topo sort example function topologicalSort(): ordering := { }. Repeat until graph is empty: Find a vertex v with in-degree of 0 (no incoming edges). (If there is no such vertex, the graph cannot be sorted; stop.) Delete v and all of its outgoing edges from the graph. ordering += v . ordering = { B, C, A, D } C A F D B E

Topo sort example function topologicalSort(): ordering := { }. Repeat until graph is empty: Find a vertex v with in-degree of 0 (no incoming edges). (If there is no such vertex, the graph cannot be sorted; stop.) Delete v and all of its outgoing edges from the graph. ordering += v . ordering = { B, C, A, D, F } C A F D B E

Topo sort example function topologicalSort(): ordering := { }. Repeat until graph is empty: Find a vertex v with in-degree of 0 (no incoming edges). (If there is no such vertex, the graph cannot be sorted; stop.) Delete v and all of its outgoing edges from the graph. ordering += v . ordering = { B, C, A, D, F, E } C A F D B E

Revised algorithm We don't want to literally delete vertices and edges from the graph while trying to topological sort it; so let's revise the algorithm: map := {each vertex  its in-degree}. queue := {all vertices with in-degree = 0}. ordering := { }. Repeat until queue is empty: Dequeue the first vertex v from the queue. ordering += v. Decrease the in-degree of all v's neighbors by 1 in the map. queue += {any neighbors whose in-degree is now 0}. If all vertices are processed, success. Otherwise, there is a cycle.

Topo sort example 2 function topologicalSort(): map := {each vertex  its in-degree}. queue := {all vertices with in-degree = 0}. ordering := { }. Repeat until queue is empty: Dequeue the first vertex v from the queue. ordering += v. Decrease the in-degree of all v's neighbors by 1 in the map. queue += {any neighbors whose in-degree is now 0}. map := { A=0, B=0, C=1, D=2, E=2, F=2 } queue := { B, A } ordering := { } C A F D B E

Topo sort example 2 function topologicalSort(): map := {each vertex  its in-degree}. queue := {all vertices with in-degree = 0}. ordering := { }. Repeat until queue is empty: Dequeue the first vertex v from the queue. // B ordering += v. Decrease the in-degree of all v's // C, D neighbors by 1 in the map. queue += {any neighbors whose in-degree is now 0}. map := { A=0, B=0, C=0, D=1, E=2, F=2 } queue := { A, C } ordering := { B } C A F D B E

Topo sort example 2 function topologicalSort(): map := {each vertex  its in-degree}. queue := {all vertices with in-degree = 0}. ordering := { }. Repeat until queue is empty: Dequeue the first vertex v from the queue. // A ordering += v. Decrease the in-degree of all v's // D neighbors by 1 in the map. queue += {any neighbors whose in-degree is now 0}. map := { A=0, B=0, C=0, D=0, E=2, F=2 } queue := { C, D } ordering := { B, A } C A F D B E

Topo sort example 2 function topologicalSort(): map := {each vertex  its in-degree}. queue := {all vertices with in-degree = 0}. ordering := { }. Repeat until queue is empty: Dequeue the first vertex v from the queue. // C ordering += v. Decrease the in-degree of all v's // E, F neighbors by 1 in the map. queue += {any neighbors whose in-degree is now 0}. map := { A=0, B=0, C=0, D=0, E=1, F=1 } queue := { D } ordering := { B, A, C } C A F D B E

Topo sort example 2 function topologicalSort(): map := {each vertex  its in-degree}. queue := {all vertices with in-degree = 0}. ordering := { }. Repeat until queue is empty: Dequeue the first vertex v from the queue. // D ordering += v. Decrease the in-degree of all v's // F, E neighbors by 1 in the map. queue += {any neighbors whose in-degree is now 0}. map := { A=0, B=0, C=0, D=0, E=0, F=0 } queue := { F, E } ordering := { B, A, C, D } C A F D B E

Topo sort example 2 function topologicalSort(): map := {each vertex  its in-degree}. queue := {all vertices with in-degree = 0}. ordering := { }. Repeat until queue is empty: Dequeue the first vertex v from the queue. // F ordering += v. Decrease the in-degree of all v's // none neighbors by 1 in the map. queue += {any neighbors whose in-degree is now 0}. map := { A=0, B=0, C=0, D=0, E=0, F=0 } queue := { E } ordering := { B, A, C, D, F } C A F D B E

Topo sort example 2 function topologicalSort(): map := {each vertex  its in-degree}. queue := {all vertices with in-degree = 0}. ordering := { }. Repeat until queue is empty: Dequeue the first vertex v from the queue. // E ordering += v. Decrease the in-degree of all v's // none neighbors by 1 in the map. queue += {any neighbors whose in-degree is now 0}. map := { A=0, B=0, C=0, D=0, E=0, F=0 } queue := { } ordering := { B, A, C, D, F, E } C A F D B E

Topo sort runtime What is the runtime of our topological sort algorithm? (with an "adjacency map" graph internal representation) function topologicalSort(): map := {each vertex  its in-degree}. // O(V) queue := {all vertices with in-degree = 0}. ordering := { }. Repeat until queue is empty: // O(V) Dequeue the first vertex v from the queue. // O(1) ordering += v. // O(1) Decrease the in-degree of all v's // O(E) for all passes neighbors by 1 in the map. queue += {any neighbors whose in-degree is now 0}. Overall: O(V + E) ; essentially O(V) time on a sparse graph (fast!)