Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph

This is a java program to check whether a graph is strongly connected or weakly connected. If graph has more than one connected components it is weakly connected. If graph has only one connected component it is strongly connected.

Here is the source code of the Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.graph;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Scanner;
  8. import java.util.Stack;
  9.  
  10. public class StronglyorWeaklyConnectedDigraphs
  11. {
  12.     private int                 V;
  13.     private int                 preCount;
  14.     private int[]               low;
  15.     private boolean[]           visited;
  16.     private List<Integer>[]     graph;
  17.     private List<List<Integer>> sccComp;
  18.     private Stack<Integer>      stack;
  19.  
  20.     /** function to get all strongly connected components **/
  21.     public List<List<Integer>> getSCComponents(List<Integer>[] graph)
  22.     {
  23.         V = graph.length;
  24.         this.graph = graph;
  25.         low = new int[V];
  26.         visited = new boolean[V];
  27.         stack = new Stack<Integer>();
  28.         sccComp = new ArrayList<>();
  29.         for (int v = 0; v < V; v++)
  30.             if (!visited[v])
  31.                 dfs(v);
  32.         return sccComp;
  33.     }
  34.  
  35.     /** function dfs **/
  36.     public void dfs(int v)
  37.     {
  38.         low[v] = preCount++;
  39.         visited[v] = true;
  40.         stack.push(v);
  41.         int min = low[v];
  42.         for (int w : graph[v])
  43.         {
  44.             if (!visited[w])
  45.                 dfs(w);
  46.             if (low[w] < min)
  47.                 min = low[w];
  48.         }
  49.         if (min < low[v])
  50.         {
  51.             low[v] = min;
  52.             return;
  53.         }
  54.         List<Integer> component = new ArrayList<Integer>();
  55.         int w;
  56.         do
  57.         {
  58.             w = stack.pop();
  59.             component.add(w);
  60.             low[w] = V;
  61.         }
  62.         while (w != v);
  63.         sccComp.add(component);
  64.     }
  65.  
  66.     @SuppressWarnings("unchecked")
  67.     public static void main(String[] args)
  68.     {
  69.         Scanner scan = new Scanner(System.in);
  70.         System.out.println("Enter number of Vertices");
  71.         /** number of vertices **/
  72.         int V = scan.nextInt();
  73.         /** make graph **/
  74.         List<Integer>[] g = new List[V];
  75.         for (int i = 0; i < V; i++)
  76.             g[i] = new ArrayList<Integer>();
  77.         /** accept all edges **/
  78.         System.out.println("Enter number of edges");
  79.         int E = scan.nextInt();
  80.         /** all edges **/
  81.         System.out.println("Enter the edges in the graph : <from> <to>");
  82.         for (int i = 0; i < E; i++)
  83.         {
  84.             int x = scan.nextInt();
  85.             int y = scan.nextInt();
  86.             g[x].add(y);
  87.         }
  88.         StronglyConnectedGraph t = new StronglyConnectedGraph();
  89.         System.out.print("The graph is : ");
  90.         /** print all strongly connected components **/
  91.         List<List<Integer>> scComponents = t.getSCComponents(g);
  92.         Iterator<List<Integer>> iterator = scComponents.iterator();
  93.         boolean weaklyConnected = false;
  94.         while (iterator.hasNext())
  95.         {
  96.             if (iterator.next().size() <= 1)
  97.             {
  98.                 weaklyConnected = true;
  99.             }
  100.         }
  101.         if (weaklyConnected == true)
  102.             System.out.println("Weakly Connected");
  103.         else
  104.             System.out.println("Strongly Connected");
  105.         scan.close();
  106.     }
  107. }

Output:

$ javac StronglyorWeaklyConnectedDigraphs.java
$ java StronglyorWeaklyConnectedDigraphs
 
Enter number of Vertices
 
6
Enter number of edges
7
Enter the edges in the graph : <from> <to>
0 1
1 2
1 3
3 4
4 5
5 3
5 2
The graph is : Weakly Connected

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.