Maximal Clique Problem | Recursive Solution

01 Nov, 2021
Given a small graph with N nodes and E edges, the task is to find the maximum clique in the given graph. A clique is a complete subgraph of a given graph. This means that all nodes in the said subgraph are directly connected to each other, or there is an edge between any two nodes in the subgraph. The maximal clique is the complete subgraph of a given graph which contains the maximum number of nodes.Examples:  
Input: N = 4, edges[][] = {{1, 2}, {2, 3}, {3, 1}, {4, 3}, {4, 1}, {4, 2}} Output: 4Input: N = 5, edges[][] = {{1, 2}, {2, 3}, {3, 1}, {4, 3}, {4, 5}, {5, 3}} Output: 3  
 
Approach: The idea is to use recursion to solve the problem.  
When an edge is added to the present list, check that if by adding that edge to the present list, does it still form a clique or not.
The vertices are added until the list does not form a clique. Then, the list is backtracked to find a larger subset which forms a clique.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; const int MAX = 100; // Stores the verticesint store[MAX], n; // Graphint graph[MAX][MAX]; // Degree of the verticesint d[MAX]; // Function to check if the given set of// vertices in store array is a clique or notbool 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 all the sizes// of maximal cliquesint maxCliques(int i, int l){    // Maximal clique size    int max_ = 0;     // 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)) {             // Update max            max_ = max(max_, l);             // Check if another edge can be added            max_ = max(max_, maxCliques(j, l + 1));        }    }    return max_;} // Driver codeint main(){    int edges[][2] = { { 1, 2 }, { 2, 3 }, { 3, 1 },                       { 4, 3 }, { 4, 1 }, { 4, 2 } };    int size = sizeof(edges) / sizeof(edges[0]);    n = 4;     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]]++;    }     cout << maxCliques(0, 1);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{ static int MAX = 100, n; // Stores the verticesstatic int []store = new int[MAX]; // Graphstatic int [][]graph = new int[MAX][MAX]; // Degree of the verticesstatic int []d = new int[MAX]; // Function to check if the given set of// vertices in store array is a clique or notstatic 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 all the sizes// of maximal cliquesstatic int maxCliques(int i, int l){    // Maximal clique size    int max_ = 0;     // 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))        {             // Update max            max_ = Math.max(max_, l);             // Check if another edge can be added            max_ = Math.max(max_, maxCliques(j, l + 1));        }    }    return max_;} // Driver codepublic static void main(String[] args){    int [][]edges = { { 1, 2 }, { 2, 3 }, { 3, 1 },                    { 4, 3 }, { 4, 1 }, { 4, 2 } };    int size = edges.length;    n = 4;     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]]++;    }    System.out.print(maxCliques(0, 1));}} // This code is contributed by 29AjayKumar
# Python3 implementation of the approachMAX = 100;n = 0; # Stores the verticesstore = [0] * MAX; # Graphgraph = [[0 for i in range(MAX)] for j in range(MAX)]; # Degree of the verticesd = [0] * MAX; # Function to check if the given set of# vertices in store array is a clique or notdef is_clique(b):     # Run a loop for all set of edges    for i in range(1, b):        for j in range(i + 1, b):             # If any edge is missing            if (graph[store[i]][store[j]] == 0):                return False;         return True; # Function to find all the sizes# of maximal cliquesdef maxCliques(i, l):     # Maximal clique size    max_ = 0;     # Check if any vertices from i+1    # can be inserted    for j in range(i + 1, n + 1):         # 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)):             # Update max            max_ = max(max_, l);             # Check if another edge can be added            max_ = max(max_, maxCliques(j, l + 1));             return max_;     # Driver codeif __name__ == '__main__':    edges = [[ 1, 2 ],[ 2, 3 ],[ 3, 1 ],           [ 4, 3 ],[ 4, 1 ],[ 4, 2 ]];    size = len(edges);    n = 4;     for i in range(size):        graph[edges[i][0]][edges[i][1]] = 1;        graph[edges[i][1]][edges[i][0]] = 1;        d[edges[i][0]] += 1;        d[edges[i][1]] += 1;         print(maxCliques(0, 1)); # This code is contributed by PrinciRaj1992
// C# implementation of the approachusing System; class GFG{ static int MAX = 100, n; // Stores the verticesstatic int []store = new int[MAX]; // Graphstatic int [,]graph = new int[MAX,MAX]; // Degree of the verticesstatic int []d = new int[MAX]; // Function to check if the given set of// vertices in store array is a clique or notstatic 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 all the sizes// of maximal cliquesstatic int maxCliques(int i, int l){    // Maximal clique size    int max_ = 0;     // 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))        {             // Update max            max_ = Math.Max(max_, l);             // Check if another edge can be added            max_ = Math.Max(max_, maxCliques(j, l + 1));        }    }    return max_;} // Driver codepublic static void Main(String[] args){    int [,]edges = { { 1, 2 }, { 2, 3 }, { 3, 1 },                    { 4, 3 }, { 4, 1 }, { 4, 2 } };    int size = edges.GetLength(0);    n = 4;     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]]++;    }    Console.Write(maxCliques(0, 1));}} // This code is contributed by PrinciRaj1992
<script>    // Javascript implementation of the approach         let MAX = 100, n;       // Stores the vertices    let store = new Array(MAX);    store.fill(0);     // Graph    let graph = new Array(MAX);    for(let i = 0; i < MAX; i++)    {        graph[i] = new Array(MAX);        for(let j = 0; j < MAX; j++)        {            graph[i][j] = 0;        }    }     // Degree of the vertices    let d = new Array(MAX);    d.fill(0);     // Function to check if the given set of    // vertices in store array is a clique or not    function is_clique(b)    {         // Run a loop for all set of edges        for (let i = 1; i < b; i++)        {            for (let 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 all the sizes    // of maximal cliques    function maxCliques(i, l)    {        // Maximal clique size        let max_ = 0;         // Check if any vertices from i+1        // can be inserted        for (let 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))            {                 // Update max                max_ = Math.max(max_, l);                 // Check if another edge can be added                max_ = Math.max(max_, maxCliques(j, l + 1));            }        }        return max_;    }         let edges = [ [ 1, 2 ], [ 2, 3 ], [ 3, 1 ],                    [ 4, 3 ], [ 4, 1 ], [ 4, 2 ] ];    let size = edges.length;    n = 4;       for (let 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]]++;    }    document.write(maxCliques(0, 1)); // This code is contributed by suresh07.</script>
4
 
29AjayKumar
princiraj1992
joshi_arihant
suresh07
Technical Scripter 2019
Graph
Recursion
Technical Scripter
Recursion
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Find if there is a path between two vertices in a directed graph
Find if there is a path between two vertices in an undirected graph
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Topological Sorting
Detect Cycle in a Directed Graph
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction