
- 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 Create a Random Linear Extension for a DAG
Here we will see how to create Random Linear Extension of a Directed Acyclic Graph (DAG). The Linear extension is basically the topological sorting of DAG. Let us consider the graph is like below −
The topological sorting for a directed acyclic graph is the linear ordering of vertices. For every edge u-v of a directed graph, the vertex u will come before vertex v in the ordering.
As we know that the source vertex will come after the destination vertex, so we need to use a stack to store previous elements. After completing all nodes, we can simply display them from stack.
Input
0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 1 | 0 | 0 |
0 | 1 | 0 | 0 | 0 | 0 |
1 | 1 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 0 | 0 | 0 |
Output
Nodes after topological sorted order − 5 4 2 3 1 0
Algorithm
topoSort(u, visited, stack)
Input − The start vertex u, An array to keep track which node is visited or not. A stack to store nodes.
Output − Sorting the vertices in topological sequence in the stack.
Begin mark u as visited for all vertices v which is adjacent with u, do if v is not visited, then topoSort(c, visited, stack) done push u into stack End
performTopologicalSorting(Graph)
Input − The given directed acyclic graph.
Output − Sequence of nodes.
Begin initially mark all nodes as unvisited for all nodes v of the graph, do if v is not visited, then topoSort(i, visited, stack) done pop and print all elements from the stack End
Example
#include<iostream> #include<stack> #define NODE 6 using namespace std; int graph[NODE][NODE] = { {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0}, {0, 1, 0, 0, 0, 0}, {1, 1, 0, 0, 0, 0}, {1, 0, 1, 0, 0, 0} }; void topoSort(int u, bool visited[], stack<int> &stk) { visited[u] = true; //set as the node v is visited for(int v = 0; v<NODE; v++) { if(graph[u][v]){ //for allvertices v adjacent to u if(!visited[v]) topoSort(v, visited, stk); } } stk.push(u); //push starting vertex into the stack } void performTopologicalSort() { stack<int> stk; bool vis[NODE]; for(int i = 0; i<NODE; i++) vis[i] = false; //initially all nodes are unvisited for(int i = 0; i<NODE; i++) if(!vis[i]) //when node is not visited topoSort(i, vis, stk); while(!stk.empty()) { cout << stk.top() << " "; stk.pop(); } } main() { cout << "Nodes after topological sorted order: "; performTopologicalSort(); }
Output
Nodes after topological sorted order: 5 4 2 3 1 0
- Related Articles
- C++ Program to Create a Random Graph Using Random Edge Generation
- C++ Program to Implement the linear congruential generator for Pseudo Random Number Generation
- Java Program to create random BigInteger within a given range
- Java Program to Create random strings
- Golang program to create random strings
- How to create a random vector for a range of values in R?
- C++ Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
- C++ Program to Check Whether Graph is DAG
- Write a program for Linear Search in Python
- C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges
- How to create a random number between a range JavaScript
- Python Program for Linear Search
- C# program to get the extension of a file in C#
- How to create a predictive linear regression line for a range of independent variable in base R?
- Program to find length of the longest path in a DAG without repeated nodes in Python
