
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find All Forward Edges in a Graph using C++
In this article, we will learn how to write an algorithm an C++ code to find all forward edges in a directed graph.
What is a Forward Edge?
A forward edge is an edge in a directed graph that points from a node to one of it's descendants in the depth-first search (DFS) tree. To understand this concept better, consider the image of a directed graph below:

In the above graph, the edge from node 2 to node 5 is a forward edge because to reach node 5 in DFS traversal, we need to move through node 2 -> node 4 -> node 5. In other words, forward edges in a graph is an edge that bypasses the actual path in the DFS tree.
Find Forward Edges in a Graph
We can slightly modify the DFS algorithm to find forward edges in a directed graph. We will keep track of the time when a node was first visited and the time when we finish DFS for the node. We will use two arrays, in_time and out_time, to store these times for each node.
- in-time: The time when a node is first visited during DFS.
- out-time: The time when we finish DFS for the node.
Then for any edge (u -> v), if in_time[u] < in_time[v] and out_time[u] > out_time[v], then (u -> v) is a forward edge.
Algorithm to Find Forward Edges in a Graph
Here is the algorithm to find all forward edges in a directed graph:
- Initialize the graph using an adjacency list.
- Initialize arrays in_time and out_time to store the in-time and out-time of each node.
- Now, perform a DFS traversal of the graph using a recursive function. Store in-time while entering the function and out-time when exiting the function.
- For each edge (u -> v), check if it is a forward edge using the conditions mentioned above.
- Store and print all forward edges found.
C++ Code to Find Forward Edges in a Graph
Here is a C++ code that implements the above algorithm to find all forward edges in a directed graph:
#include <iostream> #include <vector> using namespace std; vector<vector<int>> adj; vector<bool> visited; vector<int> inTime, outTime; int timer = 0; // DFS Function void dfs(int u) { visited[u] = true; inTime[u] = ++timer; for (int v : adj[u]) { if (!visited[v]) { dfs(v); // Tree Edge } else { // Check if it's a forward edge if (inTime[u] < inTime[v] && outTime[v] != 0) { cout << "Forward Edge: " << u << " -> " << v << endl; } } } outTime[u] = ++timer; } int main() { int V = 6; adj.resize(V); visited.resize(V, false); inTime.resize(V, 0); outTime.resize(V, 0); // Directed graph edges adj[0].push_back(1); adj[0].push_back(2); adj[1].push_back(3); adj[2].push_back(3); adj[3].push_back(4); adj[1].push_back(5); adj[0].push_back(4); // forward edge expected here for (int i = 0; i < V; ++i) { if (!visited[i]) dfs(i); } return 0; }
Output of the above code will be:
Forward Edge: 0 -> 4
Time and Space Complexity
Time Complexity: The time complexity of this algorithm is O(V + E), where V is the number of vertices and E is the number of edges in the graph. This is the same as the time complexity of DFS.
Space Complexity: The space complexity is also O(V) for storing the adjacency list and the visited, inTime, and outTime arrays.