Java Program to Check Whether Undirected Graph is Connected Using DFS Last Updated : 02 Feb, 2021 Comments Improve Suggest changes Like Article Like Report 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. Examples: Input: Output: Graph is connected Input: Output: Graph is disconnected Approach: 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. Code: Java // Java Program to check if // an undirected graph is connected or not // using DFS import java.util.*; public class checkConnectivity { // Graph class static class Graph{ int vertices; // Linked list for adjacency list of a vertex LinkedList<Integer> adjacencyList []; @SuppressWarnings("unchecked") public Graph(int vertices) { this.vertices = vertices; adjacencyList = new LinkedList[vertices]; for (int i = 0; i<vertices ; i++) { adjacencyList[i] = new LinkedList<>(); } } // Function for adding edges public void addEdge(int source, int dest) { adjacencyList[source].addFirst(dest); adjacencyList[dest].addFirst(source); } } // Function to check if the graph is connected or not public void isConnected(Graph graph){ int vertices = graph.vertices; LinkedList<Integer> adjacencyList [] = graph.adjacencyList; // Take a boolean visited array boolean[] visited = new boolean[vertices]; // Start the DFS from vertex 0 DFS(0, adjacencyList, visited); // Check if all the vertices are visited // Set connected to False if one node is unvisited boolean connected = true; for (int i = 0; i <visited.length ; i++) { if(!visited[i]){ connected = false; break; } } if(connected){ System.out.println("Graph is connected"); }else{ System.out.println("Graph is disconnected"); } } public void DFS(int source, LinkedList<Integer> adjacencyList [], boolean[] visited){ // Mark the vertex visited as True visited[source] = true; // Travel the adjacent neighbours for (int i = 0; i <adjacencyList[source].size() ; i++) { int neighbour = adjacencyList[source].get(i); if(visited[neighbour]==false){ // Call DFS from neighbour DFS(neighbour, adjacencyList, visited); } } } // Driver code public static void main(String[] args) { // Given graph 1 Graph graph = new Graph(5); graph.addEdge(0,1); graph.addEdge(0,4); graph.addEdge(1,4); graph.addEdge(1,3); graph.addEdge(3,4); graph.addEdge(2,1); graph.addEdge(2,3); // Check if it's connected System.out.print("Graph 1:- "); checkConnectivity c = new checkConnectivity(); c.isConnected(graph); // Given graph 2 graph = new Graph(5); graph.addEdge(0,1); graph.addEdge(0,4); graph.addEdge(1,4); graph.addEdge(1,3); graph.addEdge(3,4); // Check if it's connected System.out.print("Graph 2:- "); c = new checkConnectivity(); c.isConnected(graph); } } OutputGraph 1:- Graph is connected Graph 2:- Graph is disconnected Comment More infoAdvertise with us Next Article Java Program to Check Whether Undirected Graph is Connected Using DFS D debrc Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Check if a Steiner Tree of Size k Exists for a Graph A Steiner tree is a tree that connects a subset of the vertices in a graph with the minimum total weight. It is often used to find the shortest path between a set of points in a graph. Here is a Java program to check if a Steiner tree of size k exists for a given graph: Java import java.util.ArrayLi 5 min read Java Program to Detect Cycle in a Directed Graph Given a directed graph, check whether the graph contains a cycle. Your function should return true if the given graph contains at least one cycle; otherwise, it should return false.Example: Input: N = 4, E = 6 Output: Yes Explanation: The diagram clearly shows a cycle 0 -> 2 -> 0Input: N = 4, 7 min read Java Program to Find a Good Feedback Edge Set in a Graph Feedback edge set is a set of edges where F â E of a directed graph G, whose every cycle must contain at least one edge from F. In simple words, Feedback edge set is a set of edges whose removal from the graph makes the graph directed acyclic graph. Examples: Input: Output: Feedback Edge Set: ( 3 - 4 min read Java Program to Partition a Tree from a Given Element using DFS Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree. The only catch here is, unlike trees, graphs may contain cycles, a node may be visited twice. To avoid processing a node more than once, use a boolean visited array. DFS is a traversal method used to find th 4 min read Java Program to Find Minimum Number of Edges to Cut to Make the Graph Disconnected Given a connected graph, the task is to find the minimum number of edges to cut/remove to make the given graph disconnected.A graph is disconnected if there exists at least two vertices of the graph that are not connected by a path. Examples:Input:Output: Minimum Number of Edges to Remove = 2. We ca 4 min read Like