0% found this document useful (0 votes)
33 views26 pages

Topological Sorting

Uploaded by

ritikjain4560
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views26 pages

Topological Sorting

Uploaded by

ritikjain4560
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

B.

TECH V SEM
CSE
ACADEMIC YEAR: 2023-2024

Course Name: Design and Analysis of Algorithm


Topic: Topological Sort

Course code : CS 3102


Credits : 4
Mode of delivery : Pysical
Faculty : Mr. Sunila Kumar Patel
Email-id : [email protected]
Department of CSE/AIML 1
Assignment
quiz Assessment
Mid term examination criteria’s
End term Examination

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

• CS1501.1 Analyse the running times of algorithms using asymptotic analysis.


• CS1501.2 Demonstrate and Design algorithms using divide-and-conquer
paradigm to solve business problems hence enhance skills.

• CS1501.3 Illustrate the concept of greedy and dynamic-programming approach


to solve real life problems to enhance entrepreneurship capabilities.
• CS1501.4 Demonstrate the concept of backtracking
and branch & bound algorithms.
• CS1501.5 Synthesize and analyse various advanced algorithms concept such as
graphs, string matching, approximation algorithms and complexity classes to
enhance employability.

Department of CSE/AIML
6
Topological Sort

Department of CSE/AIML 7
Topological Sort

• Introduction.

• Definition of Topological Sort.

• Topological Sort is Not Unique.

• Topological Sort Algorithms.

• 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.

We wish to organize the tasks into a linear order that allows us to


complete them one at a time without violating any prerequisites. We can
model the problem using a DAG.

The graph is directed because one task is a prerequisite of another -- the


vertices have a directed relationship. It is acyclic because a cycle would
indicate a conflicting series of prerequisites that could not be completed
without violating at least one prerequisite.

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?

Yes! - Topological sort.


10
What is a DAG?
• A directed acyclic graph (DAG) is a directed graph without cycles.

Example:

• DAGs arrise in modeling many problems that involve prerequisite constraints


(construction projects, course prerequisites, document version control, compilers, etc.)

• Some properties of DAGs:

– 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.

– Every edge (w, v) in a DAG G has finishingTime[w] < finishingTime[v] in a DFS


traversal of G

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

• Topological sort is not unique.

• The following are all topological sort of the graph below:

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
}

• Running time is O(V+E), which is the running time for DFS.

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

▪ 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.

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}.

▪ map := { A=0, B=0, C=1, D=2, E=2, F=2 }


▪ queue := { B, A }
▪ ordering := { }
18
18
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. // B D
• ordering += v.
• Decrease the in-degree of all v's // C, D A
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 }
19
19
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. // A D
• ordering += v.
• Decrease the in-degree of all v's // D A
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 }
20
20
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. // C D
• ordering += v.
• Decrease the in-degree of all v's // E, F A
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 }
21
21
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 D
• ordering += v.
• Decrease the in-degree of all v's // F, E A
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 }
22
22
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. // F D
• ordering += v.
• Decrease the in-degree of all v's // none A
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 }
23
23
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. // E D
• ordering += v.
• Decrease the in-degree of all v's // none A
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 }
24
24
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!)


25
25
Topological Sort: applications
Few important applications of topological sort are-

Scheduling jobs from the given dependencies among jobs•

Instruction Scheduling•

Determining the order of compilation tasks to perform in make •

files

26

You might also like