Created
December 1, 2016 19:20
-
-
Save ZacharyJacobCollins/15e174ad0c28758694178a72b39816a6 to your computer and use it in GitHub Desktop.
Lab 4 314 tell if a graph is bipartite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Bipartite | |
{ | |
private int colors = 2; | |
private int n; | |
/** | |
* Check to ensure that we can move to the desired position within the adjacency matrix | |
* | |
* @param vertex | |
* @param adjacencyMatrix | |
* @param colored | |
* @param color | |
* @return | |
*/ | |
private boolean canTraverse(int vertex,int[][] adjacencyMatrix, int [] colored, int color) { | |
for (int destination = 1; destination <= n; destination++) { | |
if (adjacencyMatrix[vertex][destination] == 1 && colored[destination] == color) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* Check the given adjacency graph for bipartiteness | |
* @param adjacencyMatrix | |
* @return | |
*/ | |
public boolean checkBipartite(int adjacencyMatrix[][]) { | |
boolean bipartite = true; | |
n = adjacencyMatrix[1].length - 1; | |
int[] colored = new int[n + 1]; | |
for (int vertex = 1; vertex <= n; vertex++) { | |
colored[vertex] = 0; | |
} | |
if (!checkBipartiteHelper(adjacencyMatrix, colored, 1)) { | |
bipartite = false; | |
} | |
return bipartite; | |
} | |
/** | |
* Helper function for checkBipartite, checking the graph for bipartitness | |
* | |
* @param adjacencyMatrix | |
* @param colored | |
* @param vertex | |
* @return | |
*/ | |
private boolean checkBipartiteHelper(int adjacencyMatrix[][], int[] colored, int vertex) { | |
if (vertex > n) { | |
return true; | |
} | |
for (int colorNum = 1; colorNum <= this.colors; colorNum++) { | |
if (canTraverse(vertex, adjacencyMatrix, colored, colorNum)) { | |
colored[vertex] = colorNum; | |
if (checkBipartiteHelper(adjacencyMatrix, colored, vertex + 1)) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
} | |
return false; | |
} | |
public static void main(String... arg) { | |
int V; | |
Scanner input = new Scanner(System.in); | |
System.out.println("Enter the number of vertices in the graph"); | |
V = input.nextInt(); | |
int matrix[][] = new int[V + 1][V + 1]; | |
System.out.println("Enter the adjacency matrix"); | |
for (int i = 1; i <= V; i++) { | |
for (int j = 1; j <= V; j++) { | |
matrix[i][j] = input.nextInt(); | |
} | |
} | |
for (int i = 1; i <= V; i++) { | |
for (int j = 1; j <= V; j ++) { | |
if (matrix[i][j] == 1 && matrix[j][i] == 0) { | |
matrix[j][i] = 1; | |
} | |
} | |
} | |
Bipartite bipartite = new Bipartite(); | |
if (bipartite.checkBipartite(matrix)) { | |
System.out.println("Bipartite"); | |
} | |
else { | |
System.out.println("Not bipartite"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment