Count the number of Prime Cliques in an undirected graph

Given a graph with N nodes and E edges, the task is to count the number of clique having their size as a prime number or prime number of nodes in the given graph.

A clique is a complete subgraph of a given graph.

Examples:



Input: N = 5, edges[] = { {1, 2}, {2, 3}, {3, 1}, {4, 3}, {4, 5}, {5, 3} }

Output: 8
Explanation:
In the given undirected graph, 1->2->3 and 3->4->5 are the two complete subgraphs, both of them are of size 3 which is a prime.
Also, 1-2, 2->3, 3->1, 4->3, 4->5 and 5->3 are complete subgraphs of size 2.
Hence there are 8 prime cliques.

Approach: To solve the problem mentioned above, the main idea is to use recursion. All the vertices whose degree is greater than or equal to (K-1) are found and checked which subset of K vertices form a clique. When another edge is added to the present list, it is checked if by adding that edge, the list still forms a clique or not. The following steps can be followed to compute the result:

  • To check if the clique size is prime or not, the idea is to use Sieve Of eratosthenes. Create a sieve which will help us to identify if the size is prime or not in O(1) time.
  • Form a recursive function with three parameters starting node, length of the present set of nodes and prime array (to check the prime numbers).
  • The starting index resembles that no node can be added to the present set less than that index. So a loop is run from that index to n.
  • Find that after adding a node to the present set, the set of nodes remains a clique. If yes, that node is added, then the current clique size is checked, if it is prime then the answer is increased by 1 and then the recursive function is called with parameters index of new added node + 1, length of current set + 1 and the prime array.
  • The vertices are added until the list does not form a clique. In the end, the answer containing the number of prime cliques is printed.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to Count the number
// of Prime Cliques in an undirected graph
  
#include <bits/stdc++.h>
using namespace std;
  
const int MAX = 100;
  
// Stores the vertices
int store[MAX], n;
  
// Graph
int graph[MAX][MAX];
  
// Degree of the vertices
int d[MAX];
  
// To store the count of prime cliques
int ans;
  
// Function to create
// Sieve to check primes
void SieveOfEratosthenes(
    bool prime[], int p_size)
{
    // false here indicates
    // that it is not prime
    prime[0] = false;
    prime[1] = false;
  
    for (int p = 2; p * p <= p_size; p++) {
  
        // Condition if prime[p]
        // is not changed,
        // then it is a prime
        if (prime[p]) {
  
            // Update all multiples of p,
            // set them to non-prime
            for (int i = p * 2; i <= p_size; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to check
// if the given set of
// vertices in store array
// is a clique or not
bool is_clique(int b)
{
  
    // Run a loop for all set of edges
    for (int i = 1; i < b; i++) {
        for (int j = i + 1; j < b; j++)
  
            // If any edge is missing
            if (graph[store[i]][store[j]] == 0)
                return false;
    }
    return true;
}
  
// Function to find the count of
// all the cliques having prime size
void primeCliques(int i, int l,
                  bool prime[])
{
    // Check if any vertices from i+1
    // can be inserted
    for (int j = i + 1; j <= n; j++) {
  
        // Add the vertex to store
        store[l] = j;
  
        // If the graph is not
        // a clique of size k then
        // it cannot be a clique
        // by adding another edge
        if (is_clique(l + 1)) {
  
            // increase the count of
            // prime cliques if the size
            // of current clique is prime
            if (prime[l])
                ans++;
  
            // Check if another edge
            // can be added
            primeCliques(j, l + 1, prime);
        }
    }
}
  
// Driver code
int main()
{
    int edges[][2] = { { 1, 2 },
                       { 2, 3 },
                       { 3, 1 },
                       { 4, 3 },
                       { 4, 5 },
                       { 5, 3 } };
  
    int size = sizeof(edges)
               / sizeof(edges[0]);
    n = 5;
  
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
  
    SieveOfEratosthenes(prime, n + 1);
  
    for (int i = 0; i < size; i++) {
        graph[edges[i][0]][edges[i][1]] = 1;
        graph[edges[i][1]][edges[i][0]] = 1;
        d[edges[i][0]]++;
        d[edges[i][1]]++;
    }
  
    ans = 0;
    primeCliques(0, 1, prime);
  
    cout << ans << "\n";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to Count the number
// of Prime Cliques in an undirected graph
import java.io.*; 
import java.util.*; 
  
class GFG {
      
static final int MAX = 100;
  
// Stores the vertices
static int[] store = new int[MAX];
static int n;
  
// Graph
static int[][] graph = new int[MAX][MAX];
  
// Degree of the vertices
static int[] d = new int[MAX];
  
// To store the count of prime cliques
static int ans;
  
// Function to create
// Sieve to check primes
static void SieveOfEratosthenes(boolean prime[], 
                                int p_size)
{
      
    // False here indicates
    // that it is not prime
    prime[0] = false;
    prime[1] = false;
  
    for(int p = 2; p * p <= p_size; p++)
    {
          
       // Condition if prime[p]
       // is not changed,
       // then it is a prime
       if (prime[p])
       {
             
           // Update all multiples of p,
           // set them to non-prime
            for(int i = p * 2; i <= p_size; i += p)
               prime[i] = false;
       }
    }
}
  
// Function to check
// if the given set of
// vertices in store array
// is a clique or not
static boolean is_clique(int b)
{
  
    // Run a loop for all set of edges
    for(int i = 1; i < b; i++)
    {
       for(int j = i + 1; j < b; j++)
         
          // If any edge is missing
          if (graph[store[i]][store[j]] == 0)
             return false;
    }
    return true;
}
  
// Function to find the count of
// all the cliques having prime size
static void primeCliques(int i, int l, 
                         boolean prime[])
{
      
    // Check if any vertices from i+1
    // can be inserted
    for(int j = i + 1; j <= n; j++)
    {
         
       // Add the vertex to store
       store[l] = j;
         
       // If the graph is not
       // a clique of size k then
       // it cannot be a clique
       // by adding another edge
       if (is_clique(l + 1))
       {
             
           // Increase the count of
           // prime cliques if the size
           // of current clique is prime
           if (prime[l])
               ans++;
                 
           // Check if another edge
           // can be added
           primeCliques(j, l + 1, prime);
       }
    }
}
      
// Driver code 
public static void main(String[] args) 
    int edges[][] = { { 1, 2 },
                      { 2, 3 },
                      { 3, 1 },
                      { 4, 3 },
                      { 4, 5 },
                      { 5, 3 } };
  
    int size = edges.length;
    n = 5;
  
    boolean[] prime = new boolean[n + 1];
    Arrays.fill(prime, true);
  
    SieveOfEratosthenes(prime, n);
  
    for(int i = 0; i < size; i++)
    {
       graph[edges[i][0]][edges[i][1]] = 1;
       graph[edges[i][1]][edges[i][0]] = 1;
       d[edges[i][0]]++;
       d[edges[i][1]]++;
    }
      
    ans = 0;
    primeCliques(0, 1, prime);
  
    System.out.println(ans);
}
  
// This code is contributed by coder001

chevron_right


Output:

8

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : coder001