0% found this document useful (0 votes)
46 views

Write Up ADSL Assignment 3 AVO Topological Sort

The document discusses topological sorting of a graph and provides an explanation of what topological sorting is, why it is only possible for directed acyclic graphs, an algorithm to perform topological sorting, example of topological sorting, analogy to explain it, time complexity analysis and practice problems to write test cases and find number of topological orderings.

Uploaded by

shreeyan patil
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)
46 views

Write Up ADSL Assignment 3 AVO Topological Sort

The document discusses topological sorting of a graph and provides an explanation of what topological sorting is, why it is only possible for directed acyclic graphs, an algorithm to perform topological sorting, example of topological sorting, analogy to explain it, time complexity analysis and practice problems to write test cases and find number of topological orderings.

Uploaded by

shreeyan patil
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/ 3

Assignment Number: 3

Problem statement:

Activity on vertex(AOV) network: Sandy is a well-organized person. Every


day he makes a list of things which need to be done and enumerates them
from 1 to n. However, some things need to be done before others. Write a
C++ code to find out whether Sandy can solve all his duties and if so, print
the correct order.

Objectives:
 Understand the traversal of graph with dependencies of
nodes
 To know the concept of topological ordering
 To practically achieve topological ordering

Theory:

 What is topological ordering or sorting:

Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering


of vertices such that for every directed edge u-v, vertex u comes before v in
the ordering.

Note: Topological Sorting for a graph is not possible if the graph is not
a DAG.

DAGs are a special type of graphs in which each edge is directed such that
no cycle exists in the graph, before understanding why Topological sort
only exists for DAGs, lets first answer two questions:

1. Why Topological Sort is not possible for graphs with undirected


edges?

This is due to the fact that undirected edge between two


vertices u and v means, there is an edge from u to v as well as from v to u.
Because of this both the nodes u and v depend upon each other and none
of them can appear before the other in the topological ordering without
creating a contradiction.

2. Why Topological Sort is not possible for graphs having cycles?

Imagine a graph with 3 vertices and edges = {1 to 2 , 2 to 3, 3 to 1} forming


a cycle. Now if we try to topologically sort this graph starting from any
vertex, it will always create a contradiction to our definition. All the
vertices in a cycle are indirectly dependent on each other hence topological
sorting fails.

Example:

Output:5 4 2 3 1 0

Explanation:
The first vertex in topological sorting is always a vertex with an in-degree
of 0 (a vertex with no incoming edges). A topological sorting of the
following graph is “5 4 2 3 1 0”. There can be more than one topological
sorting for a graph. Another topological sorting of the following graph is “4
5 2 3 1 0”.

Analogy:

Think of it like building a house:


You can't build the roof (C) until the walls (B) are up.
You can't build the walls (B) until the foundation (A) is laid.
Therefore, the topological order would be A -> B -> C.

Algorithm:
1. Compute the indegree of all vertices.

2. Find a vertex u with indegree 0 and store it in the ordering.


If there is no any such vertex present then there is a cycle therefore,
we cannot find the order.

3. Remove u and all its edges (u,v) from the graph.

4. Update the indegree of remaining nodes.


5. Repeat step 2 to 4 while there are the vertices to process.

Time complexity:

Discuss the time complexity of following function with respect to best case
and worst case considering both ways of implementing a graph.

Test Cases:

Write test cases for the following graphs-

Practice problem:

List & Find the number of different topological orderings possible for the
given graph-

You might also like