Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. import java.util.LinkedList;
  2.  
  3. public class CheckBipartiteInAdjacencyList {
  4. enum Color{
  5. WHITE, RED, GREEN
  6. }
  7.  
  8. static class Graph{
  9. int vertices;
  10. LinkedList<Integer> [] adjList;
  11.  
  12. public Graph(int vertices){
  13. this.vertices = vertices;
  14.  
  15. adjList = new LinkedList[vertices];
  16. //Initialize lists
  17. for (int i = 0; i <vertices ; i++) {
  18. adjList[i] = new LinkedList<>();
  19. }
  20. }
  21.  
  22. public void addEdge(int source, int destination){
  23. //add forward edge
  24. adjList[source].addFirst(destination);
  25.  
  26. //add back edge
  27. adjList[destination].addFirst(source);
  28. }
  29.  
  30. public boolean isBipartite(Graph graph){
  31.  
  32. //check if graph is empty
  33. if(graph.vertices==0)
  34. return true;
  35.  
  36. //initialize colors for all vertices
  37. Color colors[] = new Color[vertices];
  38. //color all the vertices with white color
  39. for (int i = 0; i <colors.length ; i++) {
  40. colors[i] = Color.WHITE;
  41. }
  42.  
  43. //start coloring vertices , this code will handle the disconnected graph as well
  44. //color the first vertex with RED
  45. for (int i = 0; i <vertices; i++) {
  46. if(colors[i]==Color.WHITE) {
  47. boolean result = isBipartiteUtil(i, colors);
  48. if(result==false)
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54.  
  55. private boolean isBipartiteUtil(int source, Color [] colors){
  56.  
  57. //if is it first vertex, mark it RED
  58. if(source==0)
  59. colors[source]= Color.RED;
  60.  
  61. //travel all adjacent vertices
  62.  
  63. for (int i = 0; i <adjList[source].size() ; i++) {
  64. int vertex = adjList[source].get(i);
  65. if(colors[vertex]==Color.WHITE){
  66. //color is with the alternate color of
  67. if(colors[source]==Color.RED) {
  68. //if source is in red, make vertex green
  69. colors[vertex] = Color.GREEN;
  70. }
  71. else if (colors[source]==Color.GREEN) {
  72. //if source is in red, make vertex green
  73. colors[vertex] = Color.RED;
  74. }
  75.  
  76. //make recursive call for DFS
  77. return isBipartiteUtil(vertex, colors);
  78.  
  79. } else if(colors[source]==colors[vertex]){
  80. return false;
  81. }
  82. }
  83. // if here means graph is successfully colored in 2 color, red and green
  84. //graph is bipartite
  85. return true;
  86. }
  87. }
  88.  
  89. public static void main(String[] args) {
  90. Graph graph = new Graph(4);
  91. graph.addEdge(0,1);
  92. graph.addEdge(0,2);
  93. graph.addEdge(0,3);
  94. graph.addEdge(2,3);
  95. graph.addEdge(3,1);
  96.  
  97. boolean result = graph.isBipartite(graph);
  98. System.out.println("Graph is Bipartite: " + result);
  99. System.out.println("--------------------------");
  100.  
  101. Graph graph1 = new Graph(4);
  102. graph1.addEdge(0,1);
  103. graph1.addEdge(0,2);
  104. graph1.addEdge(2,3);
  105. graph1.addEdge(3,1);
  106.  
  107. result = graph1.isBipartite(graph1);
  108. System.out.println("Graph is Bipartite: " + result);
  109. System.out.println("--------------------------");
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement