对象出现在多个图形集团中

object appears in multiple graph cliques

我有一段代码可以找到节点的集团,而节点是 Django 模型对象的 ID:

    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

我得到这个结果:

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

我的问题 id:同一个 id (1293856L) 如何出现在多个 cliques 中? 结果不应该是这样的:

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

我做错了什么?

编辑: 我要找的是 nx.connected_components(graph) 而不是 nx.find_cliques(graph)

是的,相同的 ID 可以出现在多个派系中(大小相同或不同)。

我认为您只展示了大小为 2 的派系,可能您预期的输出会在下方。

[1293856L, 909760L, 909730L, 909797L, 909767L, 909741L] 仅当这些 ID 中的每一对在给定图中都具有它们之间的边时,才会作为集团之一出现。