I am trying to find maximal cliques for a set of items.
Currently I am using networkx library of python and using find_cliques() function to find all the maximal cliques as below:
import newtworkx as nx
G = nx.Graph()
E = [[1,2], [1,3], [1,4], [2,3], [2,4], [3,4], [2,6], [2,5], [5,6]]
G.add_edges_from(E)
#G.edges()
lst = list(nx.find_cliques(G))
lst
Out [] : [[2, 1, 3, 4], [2, 5, 6]]
But what I am actually expecting is to find maximal cliques and then remove the nodes which were in the maximal clique graph, and then again find maximal clique out of the nodes left after previous removal.
For the above example, I am expecting to get [2, 1, 3, 4] and then remove these nodes, so only 5 and 6 would be left, which will be another clique [5, 6] .
Update
We can use G.remove_node(), it removes the node as well as all the adjacent edges as expected.
G = nx.Graph()
E = [[1,2], [1,3], [1,4], [2,3], [2,4], [3,4], [2,6], [2,5], [5,6], [3,5], [5,7]]
G.add_edges_from(E)
list1 = list(nx.find_cliques(G))
#list1 gives [[2, 3, 1, 4], [2, 3, 5], [2, 6, 5], [7, 5]]
n = nx.number_of_nodes(G)
#n
[G.remove_node(nd) for nd in list1[0]]
list2 = list(nx.find_cliques(G))
#list2 gives [[5, 6], [5, 7]]
[G.remove_node(nd) for nd in list2[0]]
list3 = list(nx.find_cliques(G))
#list3 gives [[7]]
But every time the nodes are removed, new maximal cliques are found and stored in a new list and so on. How can it be run in the while loop until there is no edge left in graph G i.e number of nodes is 0 or 1.
[2, 1, 3, 4]
would still leave[5, 6]
, which is a technically a clique. – Supraliminal[G.remove_node(nd) for nd in lst[0]]
to remove the nodes? Following this, another call oflist(nx.find_cliques(G))
returns[[5, 6]]
for your example data, as expected. – SupraliminalGraph.remove_node
:"Removes the node n and all adjacent edges."
– SupraliminalNone
but it deletes the nodes (and corresponding edges) from the graphG
. When you calllist(nx.find_cliques(G))
again after the above loop, it returns[[5, 6]]
. – Supraliminal