object appears in multiple graph cliques

回眸只為那壹抹淺笑 提交于 2019-12-11 04:26:19

问题


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:


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.



来源:https://stackoverflow.com/questions/53817086/object-appears-in-multiple-graph-cliques

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!