object appears in multiple graph cliques

2019-08-07 01:34发布

I have a piece of code that supposed to find cliques of nodes, while nodes are ids of django model objects:

    import networkx as nx
    final_groups = []
    graph = nx.Graph()
    for img_test in Same_Img_Test.objects.filter(id__in=test_ids, is_same=1):
        graph.add_edge(img_test.product_1.id, img_test.product_2.id)
    for x in nx.find_cliques(graph):
        final_groups.append(x)
        print x

I get this result:

[1293856L, 909760L]
[1293856L, 909730L]
[1293856L, 909797L]
[1293856L, 909767L]
[1293856L, 909741L]

my question id: how same id (1293856L) can occur in multiple cliques? isn't the result supposed to be something like:

[1293856L, 909760L, 909730L, 909797L, 909767L, 909741L]

what am I doing wrong?

EDIT: what I was looking for was nx.connected_components(graph) instead of nx.find_cliques(graph)

1条回答
再贱就再见
2楼-- · 2019-08-07 02:21

Yes, same ID can be present in multiple cliques (of same size or different size).

I think that you are showing just the cliques of size 2, may be your expected output would be there in down below.

[1293856L, 909760L, 909730L, 909797L, 909767L, 909741L] would come out as one of the clique only when each pair of these ID has an edge between them in the given graph.

查看更多
登录 后发表回答