VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-a-graph-is-strongly-unilaterally-or-weakly-connected/

⇱ Check if a graph is Strongly, Unilaterally or Weakly connected - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if a graph is Strongly, Unilaterally or Weakly connected

Last Updated : 15 Jul, 2025

Given an unweighted directed graph G as a path matrix, the task is to find out if the graph is Strongly Connected or Unilaterally Connected or Weakly Connected. 

  • Strongly Connected: A graph is said to be strongly connected if every pair of vertices(u, v) in the graph contains a path between each other. In an unweighted directed graph G, every pair of vertices u and v should have a path in each direction between them i.e., bidirectional path. The elements of the path matrix of such a graph will contain all 1's.
  • Unilaterally Connected: A graph is said to be unilaterally connected if it contains a directed path from u to v OR a directed path from v to u for every pair of vertices u, v. Hence, at least for any pair of vertices, one vertex should be reachable form the other. Such a path matrix would rather have upper triangle elements containing 1's OR lower triangle elements containing 1's.
  • Weakly Connected: A directed graph is weakly connected if there is a path between every two vertices in the underlying undirected graph (i.e, the graph formed when the direction of the edges are removed).

Examples: 

Input: Below is the given graph with path matrix: 
 

👁 Image


Output: Strongly Connected Graph 
Input: Below is the given graph with path matrix: 
 

👁 Image


Output: Unilaterally Connected Graph
Input: Below is the given graph with path matrix: 
 

👁 Image


Output: Weakly Connected Graph 
 

Approach: 

  1. For the graph to be Strongly Connected, traverse the given path matrix using the approach discussed in this article check whether all the values in the cell are 1 or not. If yes then print "Strongly Connected Graph" else check for the other two graphs.
  2. For the graph to be Unilaterally Connected, traverse the given path matrix using the approach discussed in this article and check the following: 
    • If all the values above the main diagonal are 1s and all the values other than that are 0s.
    • If all the values below the main diagonal are 1s and all the values other than that are 0s.
  3. If one of the above two conditions satisfies then the given graph is Unilaterally Connected else the graph is Weakly Connected Graph.

Below is the implementation of the above approach:
 


Output
Unilaterally Connected

Time Complexity: O(N2) 
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: