Advanced Programming Worksheet, Summaries of Advanced Algorithms

Advanced Programming Worksheets

Typology: Summaries

2021/2022

Uploaded on 11/04/2022

tushar-shourie-1
tushar-shourie-1 🇮🇳

1 document

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Experiment Number: 6
Student Name: Tushar Shourie UID: 20BCS3664
Branch: Information Security Section & Group: 20BIS-2/A
Semester: 5th Date: 13-10-2022
Course Name: ADVANCED
PROGRAMMING LAB
Course Code: 20CSB-334
1.Aim/Overview of the practical :
Obtain the Topological ordering of vertices in a given digraph.
2.Task to be done : To obtain the topological ordering of vertices in a given
disgraph.
3. Algorithm:
Start.
Initialize a stack to store the nodes.
The visited nodes will be stored in an array.
The unvisited nodes will call the recursive function.
The topological sort function on the node will be called recursively.
Then, print the stack.
Stop.
4. Program Code:
#include <bits/stdc++.h>
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)
pf3

Partial preview of the text

Download Advanced Programming Worksheet and more Summaries Advanced Algorithms in PDF only on Docsity!

Experiment Number: 6

Student Name: Tushar Shourie UID: 20BCS Branch: Information Security Section & Group: 20BIS-2/A Semester: 5th Date: 13-10- Course Name: ADVANCED PROGRAMMING LAB Course Code: 20CSB-

1. Aim/Overview of the practical: Obtain the Topological ordering of vertices in a given digraph. 2. Task to be done : To obtain the topological ordering of vertices in a given disgraph. 3. Algorithm:

  • Start.
  • Initialize a stack to store the nodes.
  • The visited nodes will be stored in an array.
  • The unvisited nodes will call the recursive function.
  • The topological sort function on the node will be called recursively.
  • Then, print the stack.
  • Stop. 4. Program Code: #include <bits/stdc++.h> using namespace std; class Graph { int V; list* adj; void topologicalSortUtil(int v, bool visited[], stack& Stack); public: Graph(int V); void addEdge(int v, int w); void topologicalSort(); }; Graph::Graph(int V)

this->V = V; adj = new list[V]; } void Graph::addEdge(int v, int w) { adj[v].push_back(w); } void Graph::topologicalSortUtil(int v, bool visited[], stack& Stack) { visited[v] = true; list::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 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); g.addEdge(3, 1); cout << "Following is a Topological Sort of the given " "graph \n"; g.topologicalSort(); return 0; }