
- 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 Find All Forward Edges in a Graph
In this section, we shall consider a C++ program to find all forward edges in a graph.
Algorithms
For topo function
Begin Declare function topo() Declare pointer v, m[][5] and i of the integer datatype. x = new Node_Inf. x->n = i. x->S_Time = c. Call function Push_Node(x). v[i] = 1. for (int j = 0; j < 5; j++) if (m[i][j] == 0) then continue; else if (m[i][j] == 1 && v[j] == 1 && !Exist_in_Stack(j)) then if (x->S_Time < srch_Node(j)) then Print "Forward Edge is between ". Print the values of forward edges. continue; else if (m[i][j] == 1 && v[j] == 0) then c++; Print "Forward Edge is between " Print the values of forward edges. Call topo(v,m,j) function. c++. x = pop() x->L_Time = c. Call Store_Node(x) function to store values into nodes. Return. End.
Example
#include<iostream> #include<conio.h> using namespace std; struct Node_Inf { int n; int L_Time, S_Time; } *x = NULL, *y = NULL; struct Node_1 { Node_Inf *ptn; Node_1 *nxt; } *tp = NULL, *p = NULL, *npr = NULL; struct Node_2 { Node_2 *lnk; Node_Inf *ptn1; } *hd = NULL, *m = NULL, *n = NULL, *npr1 = NULL; int c = 0; void Push_Node(Node_Inf *ptr) { npr = new Node_1; npr->ptn = ptr; npr->nxt = NULL; if (tp == NULL) { tp = npr; } else { npr->nxt = tp; tp = npr; } } Node_Inf *pop() { if (tp == NULL) { cout<<"underflow\n"; } else { p = tp; tp = tp->nxt; return(p->ptn); delete(p); } } void Store_Node(Node_Inf *ptr1) { npr1 = new Node_2; npr1->ptn1 = ptr1; npr1->lnk = NULL; if (c == 0) { hd = npr1; m = hd; m->lnk = NULL; c++; } else { m = hd; npr1->lnk = m; hd = npr1; } } int srch_Node(int j) { Node_2 *t = hd; while (t != NULL) { if ((t->ptn1)->n == j) { break; } else { t = t->lnk; continue; } } return (t->ptn1)->L_Time; } int Exist_in_Stack(int j) { int flag = 0; p = tp; while (p != NULL) { if ((p->ptn)->n == j) { flag = 1; return flag; } p = p->nxt; } return flag; } void topo(int *v, int m[][5], int i) { x = new Node_Inf; x->n = i; x->S_Time = c; Push_Node(x); v[i] = 1; for (int j = 0; j < 5; j++) { if (m[i][j] == 0) continue; else if (m[i][j] == 1 && v[j] == 1 && !Exist_in_Stack(j)) { if (x->S_Time < srch_Node(j)) { cout<<"\nForward Edge is between "<<i<<" and "<<j<<endl; } continue; } else if (m[i][j] == 1 && v[j] == 0) { c++; cout<<"\nForward Edge is between "<<i<<" and "<<j<<endl; topo(v,m,j); } } c++; x = pop(); x->L_Time = c; Store_Node(x); return; } int main() { int v[5],m[5][5]; for (int i = 0; i < 5; i++) v[i] = 0; for (int i = 0; i < 5; i++) { cout<<" Enter the values of matrix::"<<i + 1<<endl; for(int j = 0; j < 5; j++) { cin>>m[i][j]; } } topo(v,m,0); getch(); }
Output
Enter the values of matrix:1 0 1 0 1 0 Enter the values of matrix:2 1 0 0 1 0 Enter the values of matrix:3 1 0 0 0 1 Enter the values of matrix:4 0 1 1 0 0 Enter the values of matrix:5 1 1 0 0 0 Forward Edge is between 0 and 1 Forward Edge is between 1 and 3 Forward Edge is between 3 and 2 Forward Edge is between 2 and 4 Forward Edge is between 0 and 3
- Related Articles
- Program to Find Out the Edges that Disconnect the Graph in Python
- Program to find the diameter, cycles and edges of a Wheel Graph in C++
- C++ Program to find out the number of bridge edges in a given graph
- Program to find out the critical and pseudo-critical edges in a graph in Python
- C++ Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected\n
- C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges
- Edges and Vertices of Graph
- C++ Program to Generate a Random Directed Acyclic Graph DAC for a Given Number of Edges
- Python Program to Find All Nodes Reachable from a Node using BFS in a Graph
- Python Program to Find All Connected Components using DFS in an Undirected Graph
- Python Program to Find All Connected Components using BFS in an Undirected Graph
- Maximum number of edges in Bipartite graph in C++
- C++ Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
- Count number of edges in an undirected graph in C++
- Program to find out the sum of minimum cost within a graph among all vertices in Python

Advertisements