Java Program to Check whether a Graph is Bipartite using DFS

This Java program is to check whether graph is bipartite using dfs. In the mathematical field of graph theory, a bipartite graph (or bigraph) is a graph whose vertices can be divided into two disjoint sets and such that every edge connects a vertex in to one in that is, and are each independent sets. Equivalently, a bipartite graph is a graph that does not contain any odd-length cycles.

Here is the source code of the Java program to check whether a graph is biparite using dfs. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. import java.util.Stack;
  4.  
  5. public class BipartiteDfs
  6. {
  7.     private int numberOfVertices;
  8.     private Stack<Integer> stack;
  9.  
  10.     public static final int NO_COLOR = 0;
  11.     public static final int RED = 1;
  12.     public static final int BLUE = 2;
  13.  
  14.     public BipartiteDfs(int numberOfVertices)
  15.     {
  16.         this.numberOfVertices = numberOfVertices;
  17.         stack = new Stack<Integer>();
  18.     }
  19.  
  20.     public boolean isBipartite(int adjacencyMartix[][], int source)
  21.     {
  22.         int[] colored = new int[numberOfVertices + 1];
  23.         for (int vertex = 1; vertex <= numberOfVertices; vertex++)
  24.         {
  25.             colored[vertex] = NO_COLOR;
  26.         }
  27.  
  28.         stack.push(source);
  29.         colored[source] = RED;
  30.         int element = source;
  31.         int neighbours = source;
  32.         while (!stack.empty())
  33.         {
  34.             element = stack.peek();
  35.             neighbours = element;
  36.             while (neighbours <= numberOfVertices)
  37.             {
  38.                 if (adjacencyMartix[element][neighbours] == 1&& colored[neighbours] == colored[element])
  39.                 {
  40.                     return false;
  41.                 }
  42.                 if (adjacencyMartix[element][neighbours] == 1 && colored[neighbours] == NO_COLOR)
  43.                 {
  44.                     colored[neighbours] = (colored[element] == RED) ? BLUE : RED;
  45.                     stack.push(neighbours);
  46.                     element = neighbours;
  47.                     neighbours = 1;
  48.                     continue;
  49.                 }
  50.                 neighbours++;
  51.             }
  52.             stack.pop();
  53.         }
  54.         return true;
  55.     }
  56.  
  57.     public static void main(String... arg)
  58.     {
  59.         int number_of_nodes, source;
  60.         Scanner scanner = null;
  61.  
  62.         try
  63.         {
  64.             System.out.println("Enter the number of nodes in the graph");
  65.             scanner = new Scanner(System.in);
  66.             number_of_nodes = scanner.nextInt();
  67.  
  68.             int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
  69.             System.out.println("Enter the adjacency matrix");
  70.             for (int i = 1; i <= number_of_nodes; i++)
  71.             {
  72.                 for (int j = 1; j <= number_of_nodes; j++)
  73.                 {	
  74.                     adjacency_matrix[i][j] = scanner.nextInt();
  75.                 }
  76.             }
  77.             for (int i = 1; i <= number_of_nodes; i++)
  78.             {
  79.                 for (int j = 1; j <= number_of_nodes; j++)
  80.                 {	
  81.                     if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
  82.                     {
  83.                         adjacency_matrix[j][i] = 1;
  84.                     }
  85.                 }
  86.             }			
  87.             System.out.println("Enter the source for the graph");
  88.             source = scanner.nextInt();
  89.  
  90.             BipartiteDfs bipartiteDfs = new BipartiteDfs(number_of_nodes);
  91.             if (bipartiteDfs.isBipartite(adjacency_matrix, source))
  92.             {
  93.                 System.out.println("The given graph is bipartite");
  94.             } else 
  95.             {
  96.                 System.out.println("The given graph is not bipartite");
  97.             }
  98.         } catch (InputMismatchException inputMismatch)
  99.         {
  100.             System.out.println("Wrong Input format");
  101.         }
  102.         scanner.close();
  103.     }
  104. }


$javac BipartiteBfs.java
$java BipartiteBfs
Enter the number of nodes in the graph
4
Enter the adjacency matrix
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
Enter the source for the graph
1
The given graph is bipartite

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

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.