Java Program to Check Whether Undirected Graph is Connected Using DFS
Given an undirected graph, the task is to check if the given graph is connected or not using DFS.
A connected graph is a graph that is connected in the sense of a topological space, i.e., there is always a path from any node to any other node in the graph. A graph that is not connected is said to be disconnected.
Output: Graph is connected
Output: Graph is disconnected
- Take a boolean visited  array.
- Start DFS(Depth First Search) from any of the vertexes and mark the visited vertices as True in the visited array.
- After completion of DFS check if all the vertices in the visited  array is marked as True.
- If yes then the graph is connected, or else the graph is not connected or disconnected.
Graph 1:- Graph is connected Graph 2:- Graph is disconnected
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.