# Find the number of cliques in a graph

In Graphic Schema Theory, a clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent, that is, they are connected by an edge of the graph. The number of clicks in a graph is the total number of clicks that can be found in the graph.

The mathematics behind cliques in a graph involves the concept of adjacency matrices and graph theory. An adjacency matrix is ​​a matrix representation of a graph where each row and column corresponds to a vertex in the graph, and the elements of the matrix indicate whether there is an edge between the vertices.

For exampleIn an undirected graph with 5 vertices, the adjacency matrix would be a 5×5 matrix where a 1 at position (i, j) indicates that there is an edge between vertex i and vertex j, and a 0 indicates that there is not. there is edge

1 1 1 0 0
1 1 1 0 0
1 1 1 1 1
0 0 1 1 1
0 0 1 1 1

To find the number of cliques in a graph using an adjacency matrix, you can use a graph algorithm such as Bron–Kerbosch algorithmwhich is an efficient method to enumerate all cliques in an undirected graph.

He Bron–Kerbosch algorithm it works by iterating over all possible subsets of the vertices and checking if they form a clique. It does this by using the adjacency matrix to check if each pair of vertices in the subset is adjacent (ie connected by an edge). If they are, then the subset forms a clique and is added to the clique list.

Example 1:

Consider the following graph:

Example 1

This graph has three cliques.: {1, 2, 3}, {3. 4. 5}Y {1, 2, 4, 5}.

To find the number of cliques in a graph, graph traversal algorithms such as depth-first search (DFS) or breadth-first search (BFS) can be used to visit all vertices and search for cliques at each vertex.

For example, you could start at vertex 1 and do a DFS to explore the graph. When you get to vertex 3, you know that vertices 1, 2, and 3 form a clique. You can then continue with the DFS to explore the remaining vertices and search for clicks at each vertex.

Alternatively, you could use a graphical algorithm designed specifically for finding cliques, such as the Bron–Kerbosch algorithm. This algorithm is an efficient method to enumerate all cliques in an undirected graph.

Example 2:

Consider a simple undirected graph with 4 vertices and 6 edges, as shown below: Example 2

To count the number of cliques in this graph, we can use the following formula:

Number of cliques = n * (n – 1) / 2 – m + 1 where n is the number of vertices in the graph and m is the number of edges. Substituting the values ​​for this plot, we get:
Number of cliques = 4 * (4 – 1) / 2 – 6 + 1 = 2

So there are 2 cliques in this graph.

This formula works because it counts the number of possible pairs of vertices in the graph (n * (n – 1) / 2)and then subtract the number of edges to account for the overcount. Finally, add 1 to account for the fact that a single vertex by itself is also considered a clique.

This formula only works for undirected graphs and may not give the correct result for graphs with multiple edges or auto-loops. It is also not practical for large graphs, as the time complexity of this approach is OR(n^2). However, it can be a useful tool to quickly count the number of cliques in small graphs.

### Approaches:

• Brute force search: One approach is to simply list all possible cliques on the graph and count them. This approach has a time complexity of OR(3^n)where north is the number of vertices in the graph, so it is only practical for very small graphs.
• Bron–Kerbosch algorithm: The Bron-Kerbosch algorithm is a pivot-based algorithm that uses a recursive approach to find all cliques in a graph. It has a time complexity of OR(3^(n/3))where north is the number of vertices in the graph, and a spatial complexity of In).
• Tomita Algorithm: The Tomita algorithm is another pivot-based algorithm that uses a recursive approach to find all the clicks on a chart. It has a time complexity of O(4^(n/4)), where n is the number of vertices in the graph, and a spatial complexity of In).
• Bron-Kerbosch pivot algorithm: The Bron-Kerbosch pivot algorithm is an improvement over the Bron-Kerbosch algorithm that uses a pivot element to prune the search space and reduce time complexity. It has a time complexity of OR(2^n)where n is the number of vertices in the graph and a spatial complexity of OR(n^2).
• Hybrid algorithm: The hybrid algorithm is a combination of the Bron-Kerbosch and Tomita algorithms that uses both pivot and recursion to find all cliques in a graph. It has a time complexity of OR(2^n)where n is the number of vertices in the graph and a spatial complexity of OR(n^2)
• approximation algorithms: Another approach is to use approximation algorithms, which are designed to find a good approximation of the number of clicks in a graph in polynomial time. These algorithms may not find all the cliques in the graph, but they can be useful in cases where the exact number of cliques is not necessary.
• Parallelization techniques: It is also possible to improve the performance of the above algorithms by using parallelization techniques, such as multi-threading or distributed computing, to divide the work among multiple processors. However, these approaches may have additional overhead costs and may not be suitable for all chart types.

The best approach for your specific problem will depend on the size of the graph and the desired time and space complexity. You must choose the approach that best suits your needs and constraints.

### Observation behind the approaches:

Different approaches to finding the number of cliques in a graph are based on different observations and techniques. Here are some observations behind these approaches:

• Brute force search: This approach simply lists all possible cliques on the graph and counts them. It is based on the observation that a clique is a subset of the vertices of the graph that are all connected to each other.
• Bron-Kerbosch algorithm: The Bron-Kerbosch algorithm is based on the observation that a clique can be found by starting with a vertex and then expanding to include its neighbors, as long as they are all connected to each other. The algorithm uses a pivot element to prune the search space and reduce time complexity.
• Tomita Algorithm: Tomita’s algorithm is based on the observation that a clique can be found by starting with a vertex and then expanding to include its neighbors, as long as they are all connected to each other. The algorithm uses a pivot element to prune the search space and reduce time complexity.
• Bron-Kerbosch pivot algorithm: The Bron-Kerbosch pivot algorithm is an improvement over the Bron-Kerbosch algorithm that uses a pivot element to prune the search space and reduce time complexity. It is based on the observation that many cliques in a graph share common vertices, and by using a pivot element to focus the search on these vertices, it is possible to reduce the time complexity of the algorithm.
• Hybrid algorithm: The hybrid algorithm combines the Bron-Kerbosch and Tomita algorithms and uses both rotation and recursion to find all clicks in a graph. It is based on the observation that both algorithms are effective at finding clicks, and by combining them, it is possible to find all clicks in a chart more efficiently.
• approximation algorithms: Approximation algorithms are based on the observation that it is often enough to find a good approximation of the number of cliques in a graph, rather than the exact number. These algorithms use techniques such as random sampling or graph partitioning to find a good approximation in polynomial time.
• Parallelization techniques: Parallelization techniques are based on the observation that it is often possible to improve the performance of an algorithm by dividing the work among multiple processors. These techniques can be used to speed up the execution of the above algorithms by dividing the search space among multiple threads or processes.

Here is a simple algorithm in Python to find the number of cliques in an undirected graph:

## python3

 `from` `collections ``import` `defaultdict` ` ` ` ``def` `find_cliques(graph):` `    ``cliques ``=` `[]` `    ``visited ``=` `set``()`` ` `    ``def` `dfs(node, clique):``        ``visited.add(node)``        ``clique.add(node)` ` ` `        ``for` `neighbor ``in` `graph[node]:``            ``if` `neighbor ``not` `in` `visited:` `                ``dfs(neighbor, clique)` ` ``    ``for` `node ``in` `graph:` `        ``if` `node ``not` `in` `visited:``            ``clique ``=` `set``()` `            ``dfs(node, clique)` `            ``if` `len``(clique) > ``1``:``                ``cliques.append(clique)` ` ` `    ``return` `cliques`` ` ` ` `graph ``=` `{``    ``'A'``: [``'B'``, ``'C'``, ``'D'``],` `    ``'B'``: [``'A'``, ``'C'``, ``'D'``],``    ``'C'``: [``'A'``, ``'B'``, ``'D'``],` `    ``'D'``: [``'A'``, ``'B'``, ``'C'``],``    ``'E'``: [``'F'``],` `    ``'F'``: [``'E'``]``}` ` ``cliques ``=` `find_cliques(graph)` `print``(f``'Number of cliques: {len(cliques)}'``)``print``(f``'Cliques: {cliques}'``)`
Production

```Number of cliques: 2
Cliques: [{'D', 'B', 'C', 'A'}, {'E', 'F'}]```

The complexity of time of the find_cliques function provided in the code is OR(n+m)where north is the number of vertices in the graph and subway is the number of edges. This is because the function performs a depth-down search (DFS) on the graph, which takes time proportional to the number of vertices and
The Auxiliar space of this function is In) because it uses an array to store the vertices visited and a list to store the clicks, both of which have sizes proportional to the number of vertices in the graph.

Note: that the time and space complexity of this function can be different if the graph is represented differently, for example as an adjacency matrix instead of an adjacency list. The time and space complexity of an algorithm can also depend on the specific implementation and the specific input.

### Temporal and spatial complexity of each approach:

• He Bron–Kerbosch algorithm and the Tomita Algorithm Both are pivot-based algorithms that use a recursive approach to find all the clicks on a chart.
• He Bron-Kerbosch pivot algorithm is an improvement over the Bron-Kerbosch algorithm that reduces time complexity by using a pivot element to prune the search space.
• He hybrid algorithm is a combination of the Bron-Kerbosch and Tomita algorithms that uses pivot and recursion to find all clicks in a chart.
• It is worth noting that the time complexity of these algorithms can be improved by using parallelization techniques or fuzzy algorithms, but these approaches may not find all cliques in the graph and may have additional overhead costs.

source

Scroll to Top