
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Check Whether Topological Sorting can be Performed in a Graph
In a Directed Acyclic Graph, we can sort vertices in linear order using topological sort.
Topological sort is only work on Directed Acyclic Graph. In a Directed Acyclic Graph (DAG), there can be more than one topological sort.
In the following C++ program, we shall perform topological sort to check existence of a cycle in a graph.
Algorithms
For function Topo_Sort
Begin Define function Topo_Sort() Declare x to the integer datatype, vstd[] of the Boolean array and Stack as a stack. Pass them as parameter. Initialize vstd[x] = true to mark the current node as vstd. Declare an iterator i. for (i = a[x].begin(); i != a[x].end(); ++i) if (!vstd[*i]) then Call Topo_Sort(*i, vstd, Stack) function. Call push() function to insert values into stack. End.
Example
#include<iostream> #include <list> #include <stack> using namespace std; class grph { // Class to represent a graph int ver; list<int> *a; // Pointer to an array containing adjacency listsList void Topo_Sort(int x, bool vstd[], stack<int> &Stack); // A function used by TopologicalSort public: grph(int ver); // Constructor of grpf void Insert_Edge(int x, int y); // to insert an edge to graph void Topol_Sort(); // prints a Topological Sort of the complete graph }; grph::grph(int ver) { this->ver = ver; a = new list<int>[ver]; } void grph::Insert_Edge(int x, int y) { a[x].push_back(y); // Add y to x’s list. } // A recursive function used by Topol_Sort void grph::Topo_Sort(int x, bool vstd[], stack<int> &Stack) { vstd[x] = true; // Mark the current node as vstd. list<int>::iterator i; for (i = a[x].begin(); i != a[x].end(); ++i) if (!vstd[*i]) Topo_Sort(*i, vstd, Stack); // Push current vertex to stack which stores result Stack.push(x); } void grph::Topol_Sort() { stack<int> Stack; // Mark all the vertices as not vstd bool *vstd = new bool[ver]; for (int i = 0; i < ver; i++) vstd[i] = false; for (int i = 0; i < ver; i++) if (vstd[i] == false) Topo_Sort(i, vstd, Stack); while (Stack.empty() == false) { cout << Stack.top() << " "; Stack.pop(); } } int main() { grph g(6); // Create a graph given in the above diagram g.Insert_Edge(5, 2); g.Insert_Edge(5, 0); g.Insert_Edge(4, 0); g.Insert_Edge(4, 1); g.Insert_Edge(2, 3); g.Insert_Edge(3, 1); cout << "Topological Sort of the graph is: \n"; g.Topol_Sort(); return 0; }
Output
Topological Sort of the graph is: 5 4 2 3 1 0
- Related Articles
- C++ Program to Check Cycle in a Graph using Topological Sort
- C++ Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
- Topological Sorting
- Topological sorting using Javascript DFS
- C++ Program to Check Whether Graph is DAG
- C++ Program to Check whether Graph is a Bipartite using BFS
- C++ Program to Check whether Graph is a Bipartite using DFS
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Cycle
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Path
- C++ Program to Check Whether a Graph is Strongly Connected or Not
- C++ Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
- C++ Program to Check Whether an Undirected Graph Contains a Eulerian Path
- Program to check whether given graph is bipartite or not in Python
- C++ Program to Check Whether a Hamiltonian Cycle or Path Exists in a Given Graph
- Program to check whether one point can be converted to another or not in Python

Advertisements