當前位置: 首頁>>代碼示例>>Python>>正文


Python networkx.minimum_spanning_edges方法代碼示例

本文整理匯總了Python中networkx.minimum_spanning_edges方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.minimum_spanning_edges方法的具體用法?Python networkx.minimum_spanning_edges怎麽用?Python networkx.minimum_spanning_edges使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在networkx的用法示例。


在下文中一共展示了networkx.minimum_spanning_edges方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_nan_weights

# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import minimum_spanning_edges [as 別名]
def test_nan_weights(self):
        # Edge weights NaN never appear in the spanning tree. see #2164
        G = self.G
        G.add_edge(0, 12, weight=float('nan'))
        edges = nx.minimum_spanning_edges(G, algorithm=self.algo,
                                          data=False, ignore_nan=True)
        actual = sorted((min(u, v), max(u, v)) for u, v in edges)
        expected = [(u, v) for u, v, d in self.minimum_spanning_edgelist]
        assert_edges_equal(actual, expected)
        # Now test for raising exception
        edges = nx.minimum_spanning_edges(G, algorithm=self.algo,
                                          data=False, ignore_nan=False)
        assert_raises(ValueError, list, edges)
        # test default for ignore_nan as False
        edges = nx.minimum_spanning_edges(G, algorithm=self.algo, data=False)
        assert_raises(ValueError, list, edges) 
開發者ID:holzschu,項目名稱:Carnets,代碼行數:18,代碼來源:test_mst.py

示例2: _min_cycle_basis

# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import minimum_spanning_edges [as 別名]
def _min_cycle_basis(comp, weight):
    cb = []
    # We  extract the edges not in a spanning tree. We do not really need a
    # *minimum* spanning tree. That is why we call the next function with
    # weight=None. Depending on implementation, it may be faster as well
    spanning_tree_edges = list(nx.minimum_spanning_edges(comp, weight=None,
                                                         data=False))
    edges_excl = [frozenset(e) for e in comp.edges()
                  if e not in spanning_tree_edges]
    N = len(edges_excl)

    # We maintain a set of vectors orthogonal to sofar found cycles
    set_orth = [set([edge]) for edge in edges_excl]
    for k in range(N):
        # kth cycle is "parallel" to kth vector in set_orth
        new_cycle = _min_cycle(comp, set_orth[k], weight=weight)
        cb.append(list(set().union(*new_cycle)))
        # now update set_orth so that k+1,k+2... th elements are
        # orthogonal to the newly found cycle, as per [p. 336, 1]
        base = set_orth[k]
        set_orth[k + 1:] = [orth ^ base if len(orth & new_cycle) % 2 else orth
                            for orth in set_orth[k + 1:]]
    return cb 
開發者ID:holzschu,項目名稱:Carnets,代碼行數:25,代碼來源:cycles.py

示例3: main

# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import minimum_spanning_edges [as 別名]
def main():
	# build up a graph
	filename = '../../florentine_families_graph.gpickle'
	G = nx.read_gpickle(filename)

	# Spanning tree
	mst = nx.minimum_spanning_tree(G) 
	out_file = 'florentine_families_graph_minimum_spanning_tree.png'
	PlotGraph.plot_graph(G, filename=out_file, colored_edges=mst.edges())

	edges = nx.minimum_spanning_edges(G, weight='weight', data=True)
	list_edges = list(edges)
	print(list_edges) 
開發者ID:sparkandshine,項目名稱:complex_network,代碼行數:15,代碼來源:spanning_tree.py

示例4: test_mst_edges

# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import minimum_spanning_edges [as 別名]
def test_mst_edges(self):
        edgelist=sorted(nx.minimum_spanning_edges(self.G))
        assert_equal(edgelist,self.tree_edgelist) 
開發者ID:SpaceGroupUCL,項目名稱:qgisSpaceSyntaxToolkit,代碼行數:5,代碼來源:test_mst.py

示例5: test_minimum_edges

# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import minimum_spanning_edges [as 別名]
def test_minimum_edges(self):
        edges = nx.minimum_spanning_edges(self.G, algorithm=self.algo)
        # Edges from the spanning edges functions don't come in sorted
        # orientation, so we need to sort each edge individually.
        actual = sorted((min(u, v), max(u, v), d) for u, v, d in edges)
        assert_edges_equal(actual, self.minimum_spanning_edgelist) 
開發者ID:holzschu,項目名稱:Carnets,代碼行數:8,代碼來源:test_mst.py

示例6: test_without_data

# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import minimum_spanning_edges [as 別名]
def test_without_data(self):
        edges = nx.minimum_spanning_edges(self.G, algorithm=self.algo,
                                          data=False)
        # Edges from the spanning edges functions don't come in sorted
        # orientation, so we need to sort each edge individually.
        actual = sorted((min(u, v), max(u, v)) for u, v in edges)
        expected = [(u, v) for u, v, d in self.minimum_spanning_edgelist]
        assert_edges_equal(actual, expected) 
開發者ID:holzschu,項目名稱:Carnets,代碼行數:10,代碼來源:test_mst.py

示例7: test_unicode_name

# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import minimum_spanning_edges [as 別名]
def test_unicode_name(self):
        """Tests that using a Unicode string can correctly indicate
        Borůvka's algorithm.

        """
        edges = nx.minimum_spanning_edges(self.G, algorithm=u'borůvka')
        # Edges from the spanning edges functions don't come in sorted
        # orientation, so we need to sort each edge individually.
        actual = sorted((min(u, v), max(u, v), d) for u, v, d in edges)
        assert_edges_equal(actual, self.minimum_spanning_edgelist) 
開發者ID:holzschu,項目名稱:Carnets,代碼行數:12,代碼來源:test_mst.py

示例8: test_multigraph_keys_min

# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import minimum_spanning_edges [as 別名]
def test_multigraph_keys_min(self):
        """Tests that the minimum spanning edges of a multigraph
        preserves edge keys.

        """
        G = nx.MultiGraph()
        G.add_edge(0, 1, key='a', weight=2)
        G.add_edge(0, 1, key='b', weight=1)
        min_edges = nx.minimum_spanning_edges
        mst_edges = min_edges(G, algorithm=self.algo, data=False)
        assert_edges_equal([(0, 1, 'b')], list(mst_edges)) 
開發者ID:holzschu,項目名稱:Carnets,代碼行數:13,代碼來源:test_mst.py

示例9: calc_minimum_spanning_tree

# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import minimum_spanning_edges [as 別名]
def calc_minimum_spanning_tree(input_network_shp, output_network_folder, building_nodes_shp, output_edges, output_nodes,
                               weight_field, type_mat_default, pipe_diameter_default):
    # read shapefile into networkx format into a directed graph
    graph = nx.read_shp(input_network_shp)

    # transform to an undirected graph
    iterator_edges = graph.edges(data=True)

    G = nx.Graph()
    # plant = (11660.95859999981, 37003.7689999986)
    for (x, y, data) in iterator_edges:
        G.add_edge(x, y, weight=data[weight_field])
    # calculate minimum spanning tree of undirected graph

    mst_non_directed = nx.minimum_spanning_edges(G, data=False)

    # transform back directed graph and save:
    mst_directed = nx.DiGraph()
    mst_directed.add_edges_from(mst_non_directed)
    nx.write_shp(mst_directed, output_network_folder)

    # populate fields Type_mat, Name, Pipe_Dn
    mst_edges = gdf.from_file(output_edges)
    mst_edges['Type_mat'] = type_mat_default
    mst_edges['Pipe_DN'] = pipe_diameter_default
    mst_edges['Name'] = ["PIPE" + str(x) for x in mst_edges['FID']]
    mst_edges.drop("FID", axis=1, inplace=True)
    mst_edges.crs = gdf.from_file(input_network_shp).crs  # to add coordinate system
    mst_edges.to_file(output_edges, driver='ESRI Shapefile')

    # populate fields Building, Type, Name
    mst_nodes = gdf.from_file(output_nodes)

    buiding_nodes_df = gdf.from_file(building_nodes_shp)
    mst_nodes.crs = buiding_nodes_df.crs  # to add same coordinate system
    buiding_nodes_df['coordinates'] = buiding_nodes_df['geometry'].apply(
        lambda x: (round(x.coords[0][0], 4), round(x.coords[0][1], 4)))
    mst_nodes['coordinates'] = mst_nodes['geometry'].apply(
        lambda x: (round(x.coords[0][0], 4), round(x.coords[0][1], 4)))
    names_temporary = ["NODE" + str(x) for x in mst_nodes['FID']]

    new_mst_nodes = mst_nodes.merge(buiding_nodes_df, suffixes=['', '_y'], on="coordinates", how='outer')
    new_mst_nodes.fillna(value="NONE", inplace=True)
    new_mst_nodes['Building'] = new_mst_nodes['Name']

    new_mst_nodes['Name'] = names_temporary
    new_mst_nodes['Type'] = new_mst_nodes['Building'].apply(lambda x: 'CONSUMER' if x != "NONE" else x)
    new_mst_nodes.drop(["FID", "coordinates", 'floors_bg', 'floors_ag', 'height_bg', 'height_ag', 'geometry_y'], axis=1,
                       inplace=True)
    new_mst_nodes.to_file(output_nodes, driver='ESRI Shapefile') 
開發者ID:architecture-building-systems,項目名稱:CityEnergyAnalyst,代碼行數:52,代碼來源:minimum_spanning_tree.py

示例10: minimum_spanning_tree

# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import minimum_spanning_edges [as 別名]
def minimum_spanning_tree(G, weight='weight'):
    """Return a minimum spanning tree or forest of an undirected
    weighted graph.

    A minimum spanning tree is a subgraph of the graph (a tree) with
    the minimum sum of edge weights.

    If the graph is not connected a spanning forest is constructed.  A
    spanning forest is a union of the spanning trees for each
    connected component of the graph.

    Parameters
    ----------
    G : NetworkX Graph

    weight : string
       Edge data key to use for weight (default 'weight').

    Returns
    -------
    G : NetworkX Graph
       A minimum spanning tree or forest.

    Examples
    --------
    >>> G=nx.cycle_graph(4)
    >>> G.add_edge(0,3,weight=2) # assign weight 2 to edge 0-3
    >>> T=nx.minimum_spanning_tree(G)
    >>> print(sorted(T.edges(data=True)))
    [(0, 1, {}), (1, 2, {}), (2, 3, {})]

    Notes
    -----
    Uses Kruskal's algorithm.

    If the graph edges do not have a weight attribute a default weight of 1
    will be used.
    """
    T = nx.Graph(nx.minimum_spanning_edges(G, weight=weight, data=True))
    # Add isolated nodes
    if len(T) != len(G):
        T.add_nodes_from([n for n, d in G.degree().items() if d == 0])
    # Add node and graph attributes as shallow copy
    for n in T:
        T.node[n] = G.node[n].copy()
    T.graph = G.graph.copy()
    return T 
開發者ID:SpaceGroupUCL,項目名稱:qgisSpaceSyntaxToolkit,代碼行數:49,代碼來源:mst.py


注:本文中的networkx.minimum_spanning_edges方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。