Topological Sort.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort.
Chapter 9: Graphs Topological Sort
Topological Sort Example This job consists of 10 tasks with the following precedence rules: Must start with 7, 5, 4 or 9. Task 1 must follow 7. Tasks 3.
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.
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.
Algorithms and Data Structures
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Lecture 15 CSE 331 Oct 7, Mid-term stuff Chapters 1-3 in [KT] Sample mid-term (and graded HW3) at the END of class The web version has the correct.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
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.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
Answers to Assignment #2
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
COP 3530 Spring2012 Data Structures & Algorithms Discussion Session.
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.
Topological Sort. Sorting technique over DAGs (Directed Acyclic Graphs) It creates a linear sequence (ordering) for the nodes such that: –If u has an.
Introduction to Algorithms
Lecture 16 CSE 331 Oct 5, 2011.
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.
CSE 2331/5331 Topic 9: Basic Graph Alg.
Topological Sort (an application of DFS)
Lecture 20 CSE 331 Oct 15, 2010.
Lecture 15 CSE 331 Oct 3, 2016.
Lecture 16 CSE 331 Oct 5, 2016.
Lecture 17 CSE 331 Oct 3, 2014.
Depth-First Search.
CS200: Algorithm Analysis
SINGLE-SOURCE SHORTEST PATHS IN DAGs
More Graph Algorithms.
Topological Sort.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill.
Graph Algorithm.
Lecture 15 CSE 331 Sep 29, 2014.
Lecture 15 CSE 331 Oct 3, 2016.
Graph Algorithms – 2 DAGs Topological order
Advanced Algorithms Analysis and Design
Lecture 14 CSE 331 Sep 30, 2016.
كلية المجتمع الخرج البرمجة - المستوى الثاني
Topological Sort Neil Tang 03/02/2010
"Learning how to learn is life's most important skill. " - Tony Buzan
Lecture 17 CSE 331 Oct 10, 2012.
Topological Sort CSE 373 Data Structures Lecture 19.
Graph Representation (23.1/22.1)
Chapter 22: Elementary Graph Algorithms
Connected Components, Directed Graphs, Topological Sort
Directed Acyclic Graphs && Topological Sorting
Lecture 16 CSE 331 Oct 4, 2017.
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
Lecture 19 CSE 331 Oct 13, 2010.
Lecture 14 CSE 331 Sep 29, 2017.
Lecture 16 CSE 331 Oct 8, 2012.
Lecture 16 CSE 331 Oct 2, 2013.
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.
CSE 417: Algorithms and Computational Complexity
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Assignment #2 (Assignment due: Nov. 06, 2018) v1 v2 v3 v4 v5
Advanced Algorithms Analysis and Design
DAGs Longin Jan Latecki
Presentation transcript:

Topological Sort

Topological Sort Sorting technique over DAGs (Directed Acyclic Graphs) It creates a linear sequence (ordering) for the nodes such that: If u has an outgoing edge to v  then u must finish before v starts Very common in ordering jobs or tasks

Topological Sort Example A job consists of 10 tasks with the following precedence rules: Must start with 7, 5, 4 or 9. Task 1 must follow 7. Tasks 3 & 6 must follow both 7 & 5. 8 must follow 6 & 4. 2 must follow 4. 10 must follow 2. Make a directed graph and then do DFS.

9 Tasks shown as a directed graph. 1 7 3 8 5 6 4 10 2

Topological Sort using DFS To create a topological sort from a DAG 1- Final linked list is empty 2- Run DFS 3- When a node becomes black (finishes) insert it to the top of a linked list

Example 9 1 7 3 8 5 6 4 10 2

Example 9 1 7 3 8 5 6 4 10 2

Example 9 1 7 3 8 5 6 4 10 2

Example 9 1 7 3 8 5 6 4 10 2

Example head 9 1 10 7 3 8 5 6 4 10 2

Example head 9 1 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 1, 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 1, 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 3, 1, 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 3, 1, 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 6, 3, 1, 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 7, 6, 3, 1, 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 7, 6, 3, 1, 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 9, 7, 6, 3, 1, 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 9, 7, 6, 3, 1, 4, 8, 2, 10 7 3 8 5 6 4 10 2

Example head 9 1 5, 9, 7, 6, 3, 1, 4, 8, 2, 10 7 3 8 5 6 4 10 2

Topological Sort: Summary head 9 1 5, 9, 7, 6, 3, 1, 4, 8, 2, 10 7 3 The final order or jobs 8 5 6 Time complexity = DFS complexity O(V + E) 4 There can be many orders that meet the requirements 10 2