Topological Sort (an application of DFS)

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
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.
Graphs – Depth First Search ORD DFW SFO LAX
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 11 DFS Properties of DFS Topological sort.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
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.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
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.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
Spring 2015 Lecture 10: Elementary Graph Algorithms
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.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Graphs.
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.
Chapter 22: Elementary Graph Algorithms
Properties and Applications of Depth-First Search Trees and Forests
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
CS138A Elementary Graph Algorithms Peter Schröder.
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
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 Minimum Spanning Tree
CSC 413/513: Intro to Algorithms
CS200: Algorithm Analysis
CSCE 411 Design and Analysis of Algorithms
Topological Sort.
Topological Sort.
Many slides here are based on E. Demaine , D. Luebke slides
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
BFS,DFS Topological Sort
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Applications of DFS Topological sort (for directed acyclic graph)
Graph Representation (23.1/22.1)
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
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Assignment #2 (Assignment due: Nov. 06, 2018) v1 v2 v3 v4 v5
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Topological Sort (an application of DFS) CSC263 Tutorial 9

Topological sort We have a set of tasks and a set of dependencies (precedence constraints) of form “task A must be done before task B” Topological sort: An ordering of the tasks that conforms with the given dependencies Goal: Find a topological sort of the tasks or decide that there is no such ordering

Examples Scheduling: When scheduling task graphs in distributed systems, usually we first need to sort the tasks topologically ...and then assign them to resources (the most efficient scheduling is an NP-complete problem) Or during compilation to order modules/libraries d c a g f b e

Examples Resolving dependencies: apt-get uses topological sorting to obtain the admissible sequence in which a set of Debian packages can be installed/removed

Topological sort more formally Suppose that in a directed graph G = (V, E) vertices V represent tasks, and each edge (u, v)∊E means that task u must be done before task v What is an ordering of vertices 1, ..., |V| such that for every edge (u, v), u appears before v in the ordering? Such an ordering is called a topological sort of G Note: there can be multiple topological sorts of G

Topological sort more formally Is it possible to execute all the tasks in G in an order that respects all the precedence requirements given by the graph edges? The answer is "yes" if and only if the directed graph G has no cycle! (otherwise we have a deadlock) Such a G is called a Directed Acyclic Graph, or just a DAG

Algorithm for TS TOPOLOGICAL-SORT(G): call DFS(G) to compute finishing times f[v] for each vertex v as each vertex is finished, insert it onto the front of a linked list return the linked list of vertices Note that the result is just a list of vertices in order of decreasing finish times f[]

Edge classification by DFS Edge (u,v) of G is classified as a: (1) Tree edge iff u discovers v during the DFS: P[v] = u If (u,v) is NOT a tree edge then it is a: (2) Forward edge iff u is an ancestor of v in the DFS tree (3) Back edge iff u is a descendant of v in the DFS tree (4) Cross edge iff u is neither an ancestor nor a descendant of v

Edge classification by DFS Tree edges Forward edges Back edges Cross edges a b c c The edge classification depends on the particular DFS tree!

Edge classification by DFS Tree edges Forward edges Back edges Cross edges Both are valid a a b b c c The edge classification depends on the particular DFS tree!

DAGs and back edges Can there be a back edge in a DFS on a DAG? NO! Back edges close a cycle! A graph G is a DAG <=> there is no back edge classified by DFS(G)

Back to topological sort TOPOLOGICAL-SORT(G): call DFS(G) to compute finishing times f[v] for each vertex v as each vertex is finished, insert it onto the front of a linked list return the linked list of vertices

Topological sort Call DFS(G) to compute the finishing times f[v] Let’s say we start the DFS from the vertex c d = ∞ f = ∞ a Next we discover the vertex d d = 1 f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ b c c d = ∞ f = ∞ d = ∞ f = ∞ d e d = ∞ f = ∞ f

Topological sort Call DFS(G) to compute the finishing times f[v] Let’s say we start the DFS from the vertex c d = ∞ f = ∞ a Next we discover the vertex d d = 1 f = ∞ d = ∞ f = ∞ b c c d = 2 f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ d d e d = ∞ f = ∞ f

Topological sort f Call DFS(G) to compute the finishing times f[v] as each vertex is finished, insert it onto the front of a linked list Let’s say we start the DFS from the vertex c d = ∞ f = ∞ a Next we discover the vertex d d = 1 f = ∞ d = ∞ f = ∞ b c c Next we discover the vertex f d = 2 f = ∞ d = ∞ f = ∞ f is done, move back to d d d e d = 3 f = 4 d = 3 f = ∞ f f f

Topological sort d f Call DFS(G) to compute the finishing times f[v] Let’s say we start the DFS from the vertex c d = ∞ f = ∞ a Next we discover the vertex d d = 1 f = ∞ d = ∞ f = ∞ b c c Next we discover the vertex f d = 2 f = 5 d = ∞ f = ∞ f is done, move back to d d d e d is done, move back to c d = 3 f = 4 f f d f

Topological sort d f Call DFS(G) to compute the finishing times f[v] Let’s say we start the DFS from the vertex c d = ∞ f = ∞ a Next we discover the vertex d d = 1 f = ∞ d = ∞ f = ∞ b c c Next we discover the vertex f d = 2 f = 5 d = ∞ f = ∞ f is done, move back to d d d e d is done, move back to c d = 3 f = 4 Next we discover the vertex e f f d f

Both edges from e are cross edges Topological sort Call DFS(G) to compute the finishing times f[v] Time = 6 Time = 7 Let’s say we start the DFS from the vertex c d = ∞ f = ∞ a Next we discover the vertex d d = 1 f = ∞ d = ∞ f = ∞ b c Next we discover the vertex f Both edges from e are cross edges d = 2 f = 5 d = 6 f = ∞ f is done, move back to d d d e e d is done, move back to c d = 3 f = 4 Next we discover the vertex e f f e is done, move back to c e d f

Topological sort Call DFS(G) to compute the finishing times f[v] Time = 8 Time = 7 Let’s say we start the DFS from the vertex c d = ∞ f = ∞ a Just a note: If there was (c,f) edge in the graph, it would be classified as a forward edge (in this particular DFS run) Next we discover the vertex d d = 1 f = ∞ d = ∞ f = ∞ b c Next we discover the vertex f d = 2 f = 5 d = 6 f = 7 f is done, move back to d d d e e d is done, move back to c d = 3 f = 4 Next we discover the vertex e f f e is done, move back to c c e d f c is done as well

Topological sort Call DFS(G) to compute the finishing times f[v] Time = 9 Time = 10 Let’s now call DFS visit from the vertex a d = ∞ f = ∞ d = 9 f = ∞ a a Next we discover the vertex c, but c was already processed => (a,c) is a cross edge d = 1 f = 8 d = ∞ f = ∞ b c d = 2 f = 5 d = 6 f = 7 d d e e Next we discover the vertex b d = 3 f = 4 f f c e d f

b is done as (b,d) is a cross edge => now move back to c Topological sort Call DFS(G) to compute the finishing times f[v] Time = 10 Time = 11 Let’s now call DFS visit from the vertex a d = 9 f = ∞ a a Next we discover the vertex c, but c was already processed => (a,c) is a cross edge d = 1 f = 8 d = 10 f = ∞ d = 10 f = 11 b b c d = 2 f = 5 d = 6 f = 7 d d e e Next we discover the vertex b b is done as (b,d) is a cross edge => now move back to c d = 3 f = 4 f f b c e d f

b is done as (b,d) is a cross edge => now move back to c Topological sort Call DFS(G) to compute the finishing times f[v] Time = 12 Time = 11 Let’s now call DFS visit from the vertex a d = 9 f = ∞ a a Next we discover the vertex c, but c was already processed => (a,c) is a cross edge d = 1 f = 8 d = 10 f = 11 b b c d = 2 f = 5 d = 6 f = 7 d d e e Next we discover the vertex b b is done as (b,d) is a cross edge => now move back to c d = 3 f = 4 f f a is done as well b c e d f

b is done as (b,d) is a cross edge => now move back to c Topological sort Call DFS(G) to compute the finishing times f[v] Time = 11 Time = 13 Let’s now call DFS visit from the vertex a d = 9 f = 12 WE HAVE THE RESULT! return the linked list of vertices a a Next we discover the vertex c, but c was already processed => (a,c) is a cross edge d = 1 f = 8 d = 10 f = 11 b b c d = 2 f = 5 d = 6 f = 7 d d e e Next we discover the vertex b b is done as (b,d) is a cross edge => now move back to c d = 3 f = 4 f f a is done as well a b c e d f

Topological sort a b c e d f Time = 13 Time = 11 The linked list is sorted in decreasing order of finishing times f[] d = 9 f = 12 a a d = 1 f = 8 d = 10 f = 11 Try yourself with different vertex order for DFS visit b b c d = 2 f = 5 d = 6 f = 7 d d e e Note: If you redraw the graph so that all vertices are in a line ordered by a valid topological sort, then all edges point „from left to right“ d = 3 f = 4 f f a b c e d f

Time complexity of TS(G) Running time of topological sort: Θ(n + m) where n=|V| and m=|E| Why? Depth first search takes Θ(n + m) time in the worst case, and inserting into the front of a linked list takes Θ(1) time

Proof of correctness Theorem: TOPOLOGICAL-SORT(G) produces a topological sort of a DAG G The TOPOLOGICAL-SORT(G) algorithm does a DFS on the DAG G, and it lists the nodes of G in order of decreasing finish times f[] We must show that this list satisfies the topological sort property, namely, that for every edge (u,v) of G, u appears before v in the list Claim: For every edge (u,v) of G: f[v] < f[u] in DFS

“For every edge (u,v) of G, f[v] < f[u] in this DFS” Proof of correctness “For every edge (u,v) of G, f[v] < f[u] in this DFS” The DFS classifies (u,v) as a tree edge, a forward edge or a cross-edge (it cannot be a back-edge since G has no cycles): If (u,v) is a tree or a forward edge ⇒ v is a descendant of u ⇒ f[v] < f[u] If (u,v) is a cross-edge

“For every edge (u,v) of G: f[v] < f[u] in this DFS” Proof of correctness “For every edge (u,v) of G: f[v] < f[u] in this DFS” If (u,v) is a cross-edge: as (u,v) is a cross-edge, by definition, neither u is a descendant of v nor v is a descendant of u: d[u] < f[u] < d[v] < f[v] or d[v] < f[v] < d[u] < f[u] Q.E.D. of Claim since (u,v) is an edge, v is surely discovered before u's exploration completes f[v] < f[u]

Proof of correctness TOPOLOGICAL-SORT(G) lists the nodes of G from highest to lowest finishing times By the Claim, for every edge (u,v) of G: f[v] < f[u] ⇒ u will be before v in the algorithm's list Q.E.D of Theorem