2016-04-13 15 views

Antwort

0

So landete ich mit

def as_spanning_trees(G): 
    """ 
    For a given graph with multiple sub graphs, find the components 
    and draw a spanning tree. 

    Returns a new Graph with components as spanning trees (i.e. without cycles). 

    Parameters 
    --------- 
    G:  - networkx.Graph 
    """ 

    G2 = nx.Graph() 
    # We find the connected constituents of the graph as subgraphs 
    graphs = nx.connected_component_subgraphs(G, copy=False) 

    # For each of these graphs we extract the spanning tree, removing the cycles 
    for g in graphs: 
     T = nx.minimum_spanning_tree(g) 
     G2.add_edges_from(T.edges()) 
     G2.add_nodes_from(T.nodes()) 

    return G2 
+1

'T = nx.minimum_spanning_tree (G)' gibt Ihnen das gleiche Ergebnis - ein Minimum Spanning Wald. – Aric

+0

Ah ja, ich hätte wirklich die Dokumente lesen sollen. – JoelKuiper

Verwandte Themen