Topological Sorting.

Slides:



Advertisements
Similar presentations
CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort.
Advertisements

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Chapter 9: Graphs Topological Sort
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.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
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.
Algorithms and Data Structures
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Lecture 10 Topics Application of DFS Topological Sort
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Topological Sort: Definition
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
Graphs Upon completion you will be able to:
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
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.
Introduction to Algorithms
Elementary Graph Algorithms
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.
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Topological Sort Minimum Spanning Tree
CSC 413/513: Intro to Algorithms
Depth-First Search.
CS200: Algorithm Analysis
Graph: representation and traversal CISC4080, Computer Algorithms
More Graph Algorithms.
Graph.
Topological Sort.
Topological Sort.
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)
Paul Beame in lieu of Richard Anderson
Advanced Algorithms Analysis and Design
"Learning how to learn is life's most important skill. " - Tony Buzan
CSE 373 Data Structures and Algorithms
Search Related Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Graph Representation (23.1/22.1)
Lectures on Graph Algorithms: searching, testing and sorting
Chapter 22: Elementary Graph Algorithms
Connected Components, Directed Graphs, Topological Sort
Directed Graph Algorithms
Directed Graph Algorithms
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
Richard Anderson Autumn 2016 Lecture 5
Lecture 16 CSE 331 Oct 8, 2012.
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
CSE 373: Data Structures and Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Elementary 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 .
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Topological Sorting

Topological Sort - Example Example: Professor Bumstead’s order of dressing shorts pants belt shirt tie jacket socks shoes shorts (1/10) pants (2/9) shirt (11/14) socks (15/16) belt (3/6) shoes (7/8) jacket (4/5) tie (12/13) Final order: socks, shirt, tie, shorts, pants, shoes, belt, jacket Total Running Time: O(n+e)

Topological Sorting A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge (u,v) from vertex u to vertex v, u comes before v in the ordering. The vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another. A topological ordering is just a valid sequence for the tasks.

Topological Sorting A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG). Any DAG has at least one topological ordering, and algorithms are known for constructing a topological ordering of any DAG in linear time. Scheduling a sequence of jobs or tasks based on their dependencies. The jobs are represented by vertices, and there is an edge from x to y if job x must be completed before job y can be started When washing clothes, the washing machine must finish before we put the clothes in the dryer.

Topological Sort: Definition Consider the following graph of course prerequisities 201 306 427 111 123 213 304 446 205 Problem: Find an order in which all these courses can be taken. 220 302 402 To take a course, all of its prerequisites must be taken first Example: 111, 123, 201, 213, 304, 306, 427, 205, 446, 220, 302, 402

Topological Sorting Problem Given digraph G = (V, E), find a linear ordering of its vertices such that: for any edge (u, v) in E, u precedes v in the ordering How would you topo-sort this graph given an adjacency list representation of G=(V, E)? A B C D F E

Topological Sorting Problem Given digraph G = (V, E), find a linear ordering of its vertices such that: for any edge (u, v) in E, u precedes v in the ordering Any linear ordering in which all arrows go to the right is a valid ordering B C F A D E A B F C D E

Topological Sort Not a valid topological sort Given digraph G = (V, E), find a linear ordering of its vertices such that: for any edge (u, v) in E, u precedes v in the ordering B C F A Not a valid topological sort D E A B F D C E

Topological Sort Algorithm Step1: Identify vertices that have no incoming edge in-degree of these vertices is 0 B C A F D E

Topological Sort Algorithm Step1: Identify vertices that have no incoming edge If no such vertices, the graph has cycles (cyclic) Cyclic graphs cannot be topo-sort Only Directed Acyclic Graphs (DAG) can be topologically sorted B C A An example cyclic graph: No vertex with in-degree = 0 D

Topological Sort Algorithm Step1: Identify vertices that have no incoming edge Select one such vertex B C Select A F D E

Topological Sort Algorithm Step 2: Output the vertex Delete this vertex and all of its outgoing edges from the graph B C A F B C D F E D E A Output:

Topological Sort Algorithm Repeat Steps 1 & 2 until the graph is empty Select Delete B & all its outgoing edges B C C D F E F D E A Output: B

Topological Sort Algorithm Repeat Steps 1 & 2 until the graph is empty Select Delete F & all its outgoing edges C C D E F D E A Output: B F

Topological Sort Algorithm Repeat Steps 1 & 2 until the graph is empty Select Delete C & all its outgoing edges C D E D E A Output: B F C

Topological Sort Algorithm Repeat Steps 1 & 2 until the graph is empty Delete D & all its outgoing edges Select D E E A Output: B F C D

Topological Sort Algorithm Repeat Steps 1 & 2 until the graph is empty Delete E & all its outgoing edges Select Empty Graph E A Output: B F C D E

Summary of Top-Sort Algorithm Store each vertex’s In Degree (# of incoming edges) in an array while (there are vertices remaining) { Find a vertex with In-Degree zero and output it Reduce in-degree of all vertices adjacent to it by 1 Mark this vertex deleted (in-degree = -1) } /* end=while */ A B C D F E In- degree 1 2 A B D B C C D E D E E F

Can we do better than this? Running Time Analysis For input graph G = (V,E), Running Time = ? Break down into total time required to: Initialize In-Degree array: O(n+e) Find vertex with in-degree 0: O(n) N vertices, each takes O(n) to search In-Degree array. Total time = O(n2) Reduce In-Degree of all vertices adjacent to a vertex: O(e) Output and mark vertex: O(n) Total time = O(n2 + e) - Quadratic time! Can we do better than this? Problem: a faster way to find a vertex with in-degree = 0

Making Top-Sort Faster Key idea: Initialize and maintain a queue (or stack) of vertices with In-Degree 0 In- degree Queue: A F 1 2 A B D B C A B C D F E C D E D E E F

Making Top-Sort Faster After each vertex is output, update In-Degree array, and enqueue any vertex whose In-Degree has become zero Enqueue Queue: F B In- degree Dequeue 1 2 A B D Output: A B C A B C D F E C D E D E E F

Fast Top-Sort Algorithm Store each vertex’s In-Degree in an array 2. Initialize a queue with all in-degree zero vertices 3. while (there are vertices remaining in the queue) { 3.1. Dequeue and output a vertex 3.2. Reduce In-Degree of all vertices adjacent to it by 1 3.3. Enqueue any of these vertices whose In-Degree became zero } //end-while Running Time Analysis: Step 1 – Initialization - O(n+e) Step 2 – Initialize Q – O(n) Step 3.1 – O(1) – n times – O(n) Step 3.2 + 3.3 – O(e) Total Running Time – O(n+e) - Linear

Topological Sort – Using DFS There is another O(n+e) algorithm for topological sort that is based on the DFS Think about how DFS works We start from a vertex, and go all the way down the graph until we can go no further This means that the node deepest down the tree must come last in topological order, followed by its ancestors and so on To state this in another way, for every edge (u, v) in a DAG, the finish time of u is greater than the finish time of v. Thus it suffices to output the vertices in reverse order of finishing time.

Topological Sort – based on DFS TopSort(G){ for each u in V { // Initialization color[u] = white; } //end-for L = new linked_list; // L is an empty linked list for each u in V if (color[u] == white) TopVisit(u); return L; // L gives the final order } // end-TopSort TopVisit(u){ // Start a new search at u color[u] = gray; // Mark u visited for each v in Adj[u] { if (color[v] == white){ // if neighbor v undiscovered TopVisit(v); // …visit v } //end-if color[u] = black; // we are done with u Put u to the front of L; // on finishing add u to the list } //end-while } //end-TopVisit

Topological Sort - Example Example: Professor Bumstead’s order of dressing shorts pants belt shirt tie jacket socks shoes shorts (1/10) pants (2/9) shirt (11/14) socks (15/16) belt (3/6) shoes (7/8) jacket (4/5) tie (12/13) Final order: socks, shirt, tie, shorts, pants, shoes, belt, jacket Total Running Time: O(n+e)