Check if Undirected Graph is Connected

Source Code (Explanation in above video)

package graph;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

class Graph {
  List<List<Integer>> graph;
  boolean visited[];
  int nodes;

  Graph(int nodes) {
    graph = new ArrayList<>();
    visited = new boolean[nodes];
    this.nodes = nodes;

    for (int i = 0; i < nodes; i++) {
      graph.add(i, new ArrayList<>());
    }
  }

  public void addEdge(int a, int b) {
    graph.get(a).add(b);
    graph.get(b).add(a);
  }
  
  public boolean ifGraphConnected() {
      int startIndex = 0;
      dfs(startIndex);
      
      for(int i = 0; i < visited.length; i++) {
        if(!visited[i]) {
          return false;
        }
      }
      
      return true;
  }

  public void dfs(int start) {
    Stack<Integer> stack = new Stack<>();
    
    stack.push(start);
    visited[starttrue;
    
    while(!stack.isEmpty()) {
      
      Integer node = stack.pop();
      
      List<Integer> neighboursList = graph.get(node);
      
      for(Integer neighbour: neighboursList) {
        if(!visited[neighbour]) {
          stack.push(neighbour);
          visited[neighbourtrue;
        }
      }
    }
  }
}

public class GraphProblems {

  public static void main(String[] args) {
    int nodes = 7;
    
    Graph a = new Graph(nodes);
    
    a.addEdge(01);
    a.addEdge(02);
    a.addEdge(13);
    a.addEdge(24);
    a.addEdge(35);
    a.addEdge(45);
    //a.addEdge(4, 6);

    System.out.println(a.ifGraphConnected());
  }

}

Leave a Reply

Your email address will not be published. Required fields are marked *