Topological Sorting
Topological Sorting
TECH V SEM
CSE
ACADEMIC YEAR: 2023-2024
Department of CSE/AIML 2
Course Information
• Course Handout
• Communicate through eMail
• Office hours
• To be communicated
• Grading policy
• Will be communicated as per university guidelines
Department of CSE/AIML
3
Syllabus
• Introduction: Fundamentals of Algorithms, Important Problem Types, Analysis
of algorithm efficiency. Analysis Framework: Asymptotic Notations and Basic
Efficiency Classes. Mathematical Analysis of Nonrecursive and Recursive
Algorithms: Brute force Techniques, Divide and Conquer. Decrease and
Conquer: Insertion Sort, Depth First Search, Breadth First Search, Topological
Sorting. Transform and Conquer: Presorting, BST, Heapsort. Space and Time
tradeoffs: Input Enhancement in String Matching. Dynamic Programming:
Warshall's and Floyd's Algorithms, The Knapsack Problem. Greedy Techniques:
Prim's, Kruskal's and Dijkstra's Algorithm, Huffman Trees. Coping with
limitations of algorithmic power. Backtracking: nQueens problem, Hamiltonian
Circuit Problem, Subset Sum Problem. Branch and Bound: Assignment
Problem, Knapsack Problem, TSP. P, NP, and NP-complete Problems.
Department of CSE/AIML
4
More Information
• Textbook
•Introduction to Algorithms 3rd ,Cormen, Leiserson, Rivest
and Stein, The MIT Press,
•Fundamentals of Computer Algorithms, 2nd, Sartaj Sahni,
Ellis Horowitz, Sanguthevar Rajasekaran
• Others
•Introduction to Design & Analysis Computer Algorithm 3rd,
Sara Baase, Allen Van Gelder, Adison-Wesley, 2000.
•Algorithms, Richard Johnsonbaugh, Marcus Schaefer, Prentice
Hall, 2004.
•Introduction to The Design and Analysis of Algorithms 2nd
Edition, Anany Levitin, Adison-Wesley, 2007.
Department of CSE/AIML
5
Course Objectives
Department of CSE/AIML
6
Topological Sort
Department of CSE/AIML 7
Topological Sort
• Introduction.
• An Example.
8
Topological sort: Introduction
Assume that we need to schedule a series of tasks, such as classes or
construction jobs, where we cannot start one task until after its
prerequisites are completed.
The process of laying out the vertices of a DAG in a linear order to meet
the prerequisite rules is called a topological sort.
9
Introduction
• There are many problems involving a set of tasks in
which some of the tasks must be done before others.
• For example, consider the problem of taking a course
only after taking its prerequisites.
• Is there any systematic way of linearly arranging the
courses in the order that they should be taken?
Example:
– Every DAG must have at least one vertex with in-degree zero and at least one
vertex with out-degree zero
• A vertex with in-degree zero is called a source vertex, a vertex with out-
degree zero is called a sink vertex.
11
Definition of Topological Sort
• Given a directed graph G = (V, E) a topological sort of G is an
ordering of V such that for any edge (u, v), u comes before v in
the ordering.
• Example1:
• Example2:
12
Definition of Topological Sort
• Example3: The graph in (a) can be topologically sorted as in (b)
(a) (b)
13
Topological Sort is not unique
s1 = {a, b, c, d, e, f, g, h, i}
s2 = {a, c, b, f, e, d, h, g, i}
s3 = {a, b, d, c, e, g, f, h, i}
s4 = {a, c, f, b, e, h, d, g, i}
etc.
14
Topological Sort Recursive Algorithms: DFS based algorithm
Topological-Sort(G)
{
1. Call dfsAllVertices on G to compute f[v] for each vertex v
2. If G contains a back edge (v, w) (i.e., if f[w] > f[v]) , report error ;
3. else, as each vertex is finished prepend it to a list; // or push in stack
4. Return the list; // list is a valid topological sort
}
Topological order: A C D B E H F G
15
Topological Sort Non- Recursive Algorithm: Queue based
We can implement topological sort using a queue instead of recursion, as follows.
• First visit all edges, counting the number of edges that lead to each vertex (i.e.,
count the number of prerequisites for each vertex). All vertices with no
prerequisites are placed on the queue.
• We then begin processing the queue. When Vertex vv is taken off of the queue,
it is printed, and all neighbors of vv (that is, all vertices that have vv as a
prerequisite) have their counts decremented by one.
• Place on the queue any neighbor whose count becomes zero. If the queue
becomes empty without printing all of the vertices, then the graph contains a
cycle (i.e., there is no possible ordering for the tasks that does not violate
some prerequisite).
16
Topological Sort Non- Recursive Algorithm:
Queue based
17
17
Topo sort example
• function topologicalSort():
C
▪ map := {each vertex → its in-degree}.
▪ queue := {all vertices with in-degree = 0}. F
B E
▪ 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 A
neighbors by 1 in the map.
• queue += {any neighbors whose in-degree is now 0}.
▪ 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}.
Instruction Scheduling•
files
26