Java Program to Check Whether a Graph is Bipartite using BFS

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


$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.