简体   繁体   中英

NetworkX: efficiently inducing clique given vertices

Say, I have a list of vertices [1, 2, 3, 4, 5] , and they form a clique, is there a fast way to induce edges between them in a networkx graph? The straightforward way I can think of is:

from itertools import combinations

def induceClique(nxGraph, vertexList):
    for i,j in combinations(vertexList, 2):
        nxGraph.add_edge(i, j)
    return nxGraph

The function is alright, and you do have to add all possible pairs of edges to make sure there is the desired clique, but operating from Python it can get slow when handling long vertex lists. Maybe there is a an inbuilt function for doing this more efficiently as part of the networkx package, perhaps written in C?

生成所有边,然后一次实例化它们:

nxGraph.add_edges_from(combinations(vertexList, 2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM