3. BUSINESS PLAN
TOPOLOGICAL SORT
Topological Sort is a linear ordering of the vertices in such a way that, if there is an
edge in the DAG going from vertex ‘u’ to vertex ‘v’,
then ‘u’ comes before ‘v’ in the ordering.
•Topological Sorting is possible if and only if the graph is a directed acyclic graph.
•There may exist multiple different topological orderings for a given directed
acyclic graph.
A directed acyclic graph (DAG) is a conceptuational series of representation of a
series of activities. The order of the activities is depicted by a graph, which is
visually presented as a set of circles, each one representing an activity, some of
which are connected by lines, which represent the flow from one activity to
another
4. BUSINESS PLAN
APPLICATION OF TOPOLGICAL SORT
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
•Data Serialization
5. Example - TOPOLOGICAL SORT
Consider the following directed acyclic graph –
• For this graph, following 4 different topological ordering are possible
• 1 2 3 4 5 6
• 1 2 3 4 6 5
• 1 3 2 4 5 6
• 1 3 2 4 6 5
6. Alogorithm:
• Topological Sorting or Kahn's algorithm is an algorithm that orders a directed
acylic graph in a way such that each node appears before all the nodes it
points to in the returned order, i.e. if we have a --> b, a must appear
before b in the topological order.
It's main usage is to detect cycles in directed graphs, since no topological order
is possible for a graph that contains a cycle. Some of it's uses are: deadlock
detection in OS, Course schedule problem etc.
7. Problem :
Find the number of different topological orderings possible for the given graph-
Step-1:
• Write in-degree of each vertex-
Step-2:
• Vertex-1 has the least in-degree.
• So, remove vertex-1 and its associated edges.
• Now, update the in-degree of other vertices.
8. Step-03:
• There are two vertices with the least in-degree. So, following 2 cases are possible-
• In case-01,
• Remove vertex-2 and its associated edges.
• Then, update the in-degree of other vertices.
• In case-02,
• Remove vertex-3 and its associated edges.
• Then, update the in-degree of other vertices.
Step-4:
• In case-01,
• Remove vertex-3 since it has the least in-
degree.
• Then, update the in-degree of other vertices.
In case-02,
• Remove vertex-2 since it has the least in-
degree.
• Then, update the in-degree of other vertices