Search code examples
pythonnetworkxgraphvizdotpydot

Find cluster/subgraph of nodes in dot


I have a dot file that contains a graph having different clusters, something like:

subgraph cluster_bb_5020 {
            style=filled fillcolor=white
            label="label  [5020]"
            NODE230 [label="668"]
            ....

I wonder how I can get the cluster number of nodes (e.g. here cluster of NODE230 is cluster_bb_5020) in the graph by networkx or pydot or pygraphviz? Or in general, how I can get cluster informations?

Thank you!


Solution

  • I found the answer:

    import pydot   
    graph = pydot.graph_from_dot_file('graph.dot')
    nodes_bb = {}
    for cluster in graph[0].get_subgraphs():
         if cluster.obj_dict['name'].startswith('cluster_bb_'):
              basic_block_id = cluster.obj_dict['name'].split('_')[-1]
                   for node in cluster.obj_dict['nodes'].keys():
                        nodes_bb[node] = str(basic_block_id)
    
    print nodes_bb
    

    However, pydot is really slow!!!