简体   繁体   中英

Python-IGraph / Networkx: Find clusters of specific nodes in connected graph

i need to find clusters of nodes in a connected graph that meet several conditions:

  • the nodes have a specific set of incident edges (no details needed here I can do that)

  • a cluster of such nodes is considered a cluster if the nodes meet above condition and are not more then x edges / neighbors apart

I could write this as a new function but am wondering if there is something already existing in the networkx or python-igraph libraries?

Kind regards!

I believe there is nothing to get what you specify directly (in networkx) as your needs are very specific but there are definitely some helping functions. This is the simplest way I can think of using Networkx.

Condition 1 Compare the list of specific incident edges to g.edges(node):

cluster_nodes = []
for node in G:
    if set(specific_edges) == set (g.edges(node)): # the set is used to make sure that order is ignored.
        cluster_nodes.append(node)

Condition 2 Given x as the maximum accepted distance between any 2 nodes in the cluster

for node in cluster_nodes:
    for n in cluster_nodes:
        if (nx.shortest_path(G,node,n) > x):
            cluster_node.remove(node)

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