Skip to content

Commit 0314fe6

Browse files
authored
Create Two Clique Problem
1 parent e72e563 commit 0314fe6

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

Two Clique Problem

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

0 commit comments

Comments
 (0)