21MCA2205 (Topology Sort)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Topological Sorting

Student Name: Divyanshi UID: 21MCA2205


Branch: MCA Section/Group: 2/A
Semester: 1st Date of Performance: 26/11/21
Subject Name: DAA Subject Code: 21CAP-606

1. Aim/Overview of the practical: Implement topological sort with stacks.

2. Task to be done: Write a program to demonstrate topological sort of n numbers with stacks.

3. Algorithm/Flowchart:
The topological sorting of a DAG is done in a order such that for every directed edge uv, vertex u comes
before v in the ordering. 5 has no incoming edge. 4 has no incoming edge, 2 and 0 have incoming edge
from 4 and 5 and 1 is placed at last.

Algorithm: Steps involved in finding the topological ordering of a DAG:


Step-1: Compute in-degree (number of incoming edges) for each of the vertex present in the DAG and
initialize the count of visited nodes as 0.
Step-2: Pick all the vertices with in-degree as 0 and add them into a queue (Enqueue operation)
Step-3: Remove a vertex from the queue (Dequeue operation) and then.
1. Increment count of visited nodes by 1.
2. Decrease in-degree by 1 for all its neighbouring nodes.
3. If in-degree of a neighbouring nodes is reduced to zero, then add it to the queue

Step 4: Repeat Step 3 until the queue is empty.


Step 5: If count of visited nodes is not equal to the number of nodes in the graph then the topological
sort is not possible for the given graph.
4. Dataset:
5 4
1
2 1
3

5. Code for experiment/practical:


#include <iostream>

#include <list>

#include <stack>

using namespace std;

class Graph {
int V;

list<int>* adj
void topologicalSortUtil(int v, bool visited[],
stack<int>& Stack);

public:
Graph(int V);

void addEdge(int v, int w);


void topologicalSort();
};

Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);

void Graph::topologicalSortUtil(int v, bool visited[],


stack<int>& Stack)
{
visited[v] = true;

list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
topologicalSortUtil(*i, visited, Stack);
Stack.push(v);
}

void Graph::topologicalSort()
{
stack<int> Stack;

bool* visited = new bool[V];


for (int i = 0; i < V; i++)
visited[i] = false;

for (int i = 0; i < V; i++)


if (visited[i] == false)
topologicalSortUtil(i, visited, Stack);

while (Stack.empty() == false) {


cout << Stack.top() << " ";
Stack.pop();
}
}

int main()
{
Graph g(6);
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);

g.addEdge(2, 3);

6. g.addEdge(3, 1);
7.
8. cout << "Following is a Topological Sort of the given "
9. "graph \n";
10. g.topologicalSort();
11.
12. return 0;
13. }

14. Result/Output/Writing Summary:


A Topological Ordering is an ordering of nodes in a directed graph where for each directed edge from
node A to node B, node A appears before node B in the ordering.

 Time Complexity: O(V+E).


The outer for loop will be executed V number of times and the inner for loop will be executed E
number of times.
 Auxiliary Space: O(V).
The queue needs to store all the vertices of the graph. So the space required is O(V)

Learning outcomes (What I have learnt):

1. the knowledge of basic data structures and their implementations.

2. apply appropriate data structures in problem solving.

Evaluation Grid:
Sr. No. Parameters Marks Obtained Maximum Marks
1. Demonstration and Performance 5
2. Worksheet 10
3. Post Lab Quiz 5

You might also like