VOOZH about

URL: https://www.geeksforgeeks.org/dsa/paranthesis-theorem/

⇱ Parenthesis Theorem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Parenthesis Theorem

Last Updated : 12 Jul, 2025

Parenthesis Theorem is used in DFS of graph. It states that the descendants in a depth-first-search tree have an interesting property. If v is a descendant of u, then the discovery time of v is later than the discovery time of u
In any DFS traversal of a graph g = (V, E), for any two vertices u and v exactly one of the following holds: 

  • The intervals [d[u], f[u]] and [d[v], f[v]] are entirely disjoint and neither u nor v is a descendant of the other in the depth-first forest.
  • The interval [d[u], f[u]] is contained within the interval [d[v], f[v]], and u is a descendant of v in a depth-first tree.
  • The interval [d[v], f[v]] is contained entirely within the interval [d[u], f[u]], and v is a descendant of u in a depth-first tree.

Classification of Edges: 
DFS traversal can be used to classify the edges of input graph G=(V, E). Four edge types can be defined in terms of a depth-first forest: 

  1. Tree Edge: It is an edge that is present in the tree obtained after applying DFS on the graph.
  2. Forward Edge: It is an edge (u, v) such that v is descendant but not part of the DFS tree.
  3. Back edge: It is an edge (u, v) such that v is the ancestor of edge u but not part of the DFS tree. The presence of the back edge indicates a cycle in a directed graph.
  4. Cross Edge: It is an edge that connects two-node such that they do not have any ancestor and a descendant relationship between them.

Given a graph of N vertices and M Edges, the task is to classify the M edges into Tree edges, Forward edges, Backward edges and Cross edges.

Examples:  

Input: N = 5, M = 7, arr[][] = { { 1, 2 }, { 1, 3 }, { 3, 4 }, { 1, 4 }, { 2, 5 }, { 5, 1 }, { 3, 2 } } } 
Output: 
{1, 2} -> Tree Edge 
{1, 3} -> Tree Edge 
{3, 4} -> Tree Edge 
{1, 4} -> Forward Edge 
{2, 5} -> Tree Edge 
{5, 1} -> Backward Edge 
{3, 2} -> Cross Edge 
Explanation: 
1. Green Edges: Tree Edge 
2. Blue Edges: Forward Edge 
3. Black Edges: Backward Edge 
4. Red Edges: Cross Edge 
Below is the given graph for the above information: 
 

👁 Image


Input: N = 5, M = 4, arr[][] = { { 1, 2 }, { 1, 3 }, { 3, 4 }, { 1, 4 } } 
Output: 
{1, 2} -> Tree Edge 
{1, 3} -> Tree Edge 
{3, 4} -> Tree Edge 
{1, 4} -> Forward Edge 
Explanation: 
1. Green Edges: Tree Edge 
2. Blue Edges: Forward Edge 
3. Black Edges: Backward Edge 
4. Red Edges: Cross Edge 
Below is the given graph for the above information: 
 

👁 Image

Approach: 

  1. Use DFS Traversal on the given graph to find discovery time and finishing time and parent of each node.
  2. By using Parenthesis Theorem classify the given edges on the below conditions: 
    • Tree Edge: For any Edge (U, V), if node U is the parent of node V, then (U, V) is the Tree Edge of the given graph.
    • Forward Edge: For any Edge (U, V), if discovery time and finishing time of node V fully overlaps with discovery time and finishing time of node U, then (U, V) is the Forward Edge of the given graph.
    • Backward Edge: For any Edge (U, V), if discovery time and finishing time of node U fully overlaps with discovery time and finishing time of node V, then (U, V) is the Backward Edge of the given graph.
    • Cross Edge: For any Edge (U, V), if discovery time and finishing time of node U doesn't overlaps with discovery time and finishing time of node V, then (U, V) is the Cross Edge of the given graph.

Below is the implementation of the above approach: 


Output: 
{1, 2} -> Tree Edge
{1, 3} -> Tree Edge
{3, 4} -> Tree Edge
{1, 4} -> Forward Edge
{2, 5} -> Tree Edge
{5, 1} -> Backward Edge
{3, 1} -> Backward Edge

 

Time Complexity: O(N), Where N is the total number of nodes in the graph.

Auxiliary Space: O(N)

Comment