Java Program to Check if a Directed Graph is Weakly Connected or Not using DFS

This is a java program to test whether a directed graph is weakly connected or not. The graph is weakly connected if it has more than one connected component.

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

Output:

$ javac WeaklyConnectedGraph.java
$ java WeaklyConnectedGraph
 
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? : true

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.