|
| 1 | +// This code is contributed by Avinash Prasad (GitHub : avinash11a) |
| 2 | +// C++ program to find out whether a given graph can be |
| 3 | +// converted to two Cliques or not. |
| 4 | +#include <bits/stdc++.h> |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +const int V = 5; |
| 8 | + |
| 9 | +// This function returns true if subgraph reachable from |
| 10 | +// src is Bipartite or not. |
| 11 | +bool isBipartiteUtil(int G[][V], int src, int colorArr[]) |
| 12 | +{ |
| 13 | + colorArr[src] = 1; |
| 14 | + |
| 15 | + // Create a queue (FIFO) of vertex numbers and enqueue |
| 16 | + // source vertex for BFS traversal |
| 17 | + queue <int> q; |
| 18 | + q.push(src); |
| 19 | + |
| 20 | + // Run while there are vertices in queue (Similar to BFS) |
| 21 | + while (!q.empty()) |
| 22 | + { |
| 23 | + // Dequeue a vertex from queue |
| 24 | + int u = q.front(); |
| 25 | + q.pop(); |
| 26 | + |
| 27 | + // Find all non-colored adjacent vertices |
| 28 | + for (int v = 0; v < V; ++v) |
| 29 | + { |
| 30 | + // An edge from u to v exists and destination |
| 31 | + // v is not colored |
| 32 | + if (G[u][v] && colorArr[v] == -1) |
| 33 | + { |
| 34 | + // Assign alternate color to this adjacent |
| 35 | + // v of u |
| 36 | + colorArr[v] = 1 - colorArr[u]; |
| 37 | + q.push(v); |
| 38 | + } |
| 39 | + |
| 40 | + // An edge from u to v exists and destination |
| 41 | + // v is colored with same color as u |
| 42 | + else if (G[u][v] && colorArr[v] == colorArr[u]) |
| 43 | + return false; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // If we reach here, then all adjacent vertices can |
| 48 | + // be colored with alternate color |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +// Returns true if a Graph G[][] is Bipartite or not. Note |
| 53 | +// that G may not be connected. |
| 54 | +bool isBipartite(int G[][V]) |
| 55 | +{ |
| 56 | + // Create a color array to store colors assigned |
| 57 | + // to all veritces. Vertex number is used as index in |
| 58 | + // this array. The value '-1' of colorArr[i] |
| 59 | + // is used to indicate that no color is assigned to |
| 60 | + // vertex 'i'. The value 1 is used to indicate first |
| 61 | + // color is assigned and value 0 indicates |
| 62 | + // second color is assigned. |
| 63 | + int colorArr[V]; |
| 64 | + for (int i = 0; i < V; ++i) |
| 65 | + colorArr[i] = -1; |
| 66 | + |
| 67 | + // One by one check all not yet colored vertices. |
| 68 | + for (int i = 0; i < V; i++) |
| 69 | + if (colorArr[i] == -1) |
| 70 | + if (isBipartiteUtil(G, i, colorArr) == false) |
| 71 | + return false; |
| 72 | + |
| 73 | + return true; |
| 74 | +} |
| 75 | + |
| 76 | +// Returns true if G can be divided into |
| 77 | +// two Cliques, else false. |
| 78 | +bool canBeDividedinTwoCliques(int G[][V]) |
| 79 | +{ |
| 80 | + // Find complement of G[][] |
| 81 | + // All values are complemented except |
| 82 | + // diagonal ones |
| 83 | + int GC[V][V]; |
| 84 | + for (int i=0; i<V; i++) |
| 85 | + for (int j=0; j<V; j++) |
| 86 | + GC[i][j] = (i != j)? !G[i][j] : 0; |
| 87 | + |
| 88 | + // Return true if complement is Bipartite |
| 89 | + // else false. |
| 90 | + return isBipartite(GC); |
| 91 | +} |
| 92 | + |
| 93 | +// Driver program to test above function |
| 94 | +int main() |
| 95 | +{ |
| 96 | + int G[][V] = {{0, 1, 1, 1, 0}, |
| 97 | + {1, 0, 1, 0, 0}, |
| 98 | + {1, 1, 0, 0, 0}, |
| 99 | + {0, 1, 0, 0, 1}, |
| 100 | + {0, 0, 0, 1, 0} |
| 101 | + }; |
| 102 | + |
| 103 | + canBeDividedinTwoCliques(G) ? cout << "Yes" : |
| 104 | + cout << "No"; |
| 105 | + return 0; |
| 106 | +} |
0 commit comments