0% found this document useful (0 votes)
3 views9 pages

Lecture 15 Topological Sort

Topological sorting is a method for arranging vertices in a directed acyclic graph (DAG) such that no vertex appears before its predecessors, useful for tasks with dependencies like course prerequisites. The topological sort is not unique, as multiple valid sequences can exist for the same graph. An algorithm for topological sorting involves selecting vertices with zero in-degrees and visiting them while removing their edges until all vertices are processed.

Uploaded by

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

Lecture 15 Topological Sort

Topological sorting is a method for arranging vertices in a directed acyclic graph (DAG) such that no vertex appears before its predecessors, useful for tasks with dependencies like course prerequisites. The topological sort is not unique, as multiple valid sequences can exist for the same graph. An algorithm for topological sorting involves selecting vertices with zero in-degrees and visiting them while removing their edges until all vertices are processed.

Uploaded by

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

CSE408

Topological Sorting

Lecture #
Topological Sorting
• 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?
Topological Sorting
Definition of Topological Sort
• Topological sort is a method of arranging the
vertices in a directed acyclic graph (DAG), as a
sequence, such that no vertex appear in the
sequence before its predecessor.

• The graph in (a) can be topologically sorted as


in (b)
Definition of Topological Sort
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.
Topological Sort Algorithm
• One way to find a topological sort is to consider
in-degrees of the vertices.
• The first vertex must have in-degree zero -- every
DAG must have at least one vertex with in-
degree zero.
• The Topological sort algorithm is:
Algorithm

int topologicalOrderTraversal( ){
int numVisitedVertices = 0;
while(there are more vertices to be visited){
if(there is no vertex with in-degree 0)
break;
else{
select a vertex v that has in-degree 0;
visit v;(print v)
numVisitedVertices++;
delete v and all its emanating edges;
}
}

return numVisitedVertices;
}
Topological Sort Example
1 2 3 0 2
A B C D E

F G H I J
1 0 2 2 0

D G A B F H J E I C

You might also like