当前位置: 首页>>代码示例>>Python>>正文


Python networkx.empty_graph方法代码示例

本文整理汇总了Python中networkx.empty_graph方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.empty_graph方法的具体用法?Python networkx.empty_graph怎么用?Python networkx.empty_graph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx的用法示例。


在下文中一共展示了networkx.empty_graph方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: tree_decomp

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def tree_decomp(self):
        clusters = self.clusters
        graph = nx.empty_graph( len(clusters) )
        for atom, nei_cls in enumerate(self.atom_cls):
            if len(nei_cls) <= 1: continue
            inter = set(self.clusters[nei_cls[0]])
            for cid in nei_cls:
                inter = inter & set(self.clusters[cid])
            assert len(inter) >= 1

            if len(nei_cls) > 2 and len(inter) == 1: # two rings + one bond has problem!
                clusters.append([atom])
                c2 = len(clusters) - 1
                graph.add_node(c2)
                for c1 in nei_cls:
                    graph.add_edge(c1, c2, weight = 100)
            else:
                for i,c1 in enumerate(nei_cls):
                    for c2 in nei_cls[i + 1:]:
                        union = set(clusters[c1]) | set(clusters[c2])
                        graph.add_edge(c1, c2, weight = len(union))

        n, m = len(graph.nodes), len(graph.edges)
        assert n - m <= 1 #must be connected
        return graph if n - m == 1 else nx.maximum_spanning_tree(graph) 
开发者ID:wengong-jin,项目名称:hgraph2graph,代码行数:27,代码来源:mol_graph.py

示例2: test_eccentricity

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def test_eccentricity(self):
        assert_equal(networkx.eccentricity(self.G,1),6)
        e=networkx.eccentricity(self.G)
        assert_equal(e[1],6)
        sp=networkx.shortest_path_length(self.G)
        e=networkx.eccentricity(self.G,sp=sp)
        assert_equal(e[1],6)
        e=networkx.eccentricity(self.G,v=1)
        assert_equal(e,6)
        e=networkx.eccentricity(self.G,v=[1,1])  #This behavior changed in version 1.8 (ticket #739)
        assert_equal(e[1],6)
        e=networkx.eccentricity(self.G,v=[1,2])
        assert_equal(e[1],6)
        # test against graph with one node
        G=networkx.path_graph(1)
        e=networkx.eccentricity(G)
        assert_equal(e[0],0)
        e=networkx.eccentricity(G,v=0)
        assert_equal(e,0)
        assert_raises(networkx.NetworkXError, networkx.eccentricity, G, 1)
        # test against empty graph
        G=networkx.empty_graph()
        e=networkx.eccentricity(G)
        assert_equal(e,{}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:26,代码来源:test_distance_measures.py

示例3: ladder_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def ladder_graph(n,create_using=None):
    """Return the Ladder graph of length n.

    This is two rows of n nodes, with
    each pair connected by a single edge.

    Node labels are the integers 0 to 2*n - 1.

    """
    if create_using is not None and create_using.is_directed():
        raise nx.NetworkXError("Directed Graph not supported")
    G=empty_graph(2*n,create_using)
    G.name="ladder_graph_(%d)"%n
    G.add_edges_from([(v,v+1) for v in range(n-1)])
    G.add_edges_from([(v,v+1) for v in range(n,2*n-1)])
    G.add_edges_from([(v,v+n) for v in range(n)])
    return G 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:19,代码来源:classic.py

示例4: test_path_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def test_path_graph(self):
        p=path_graph(0)
        assert_true(is_isomorphic(p, null_graph()))
        assert_equal(p.name, 'path_graph(0)')

        p=path_graph(1)
        assert_true(is_isomorphic( p, empty_graph(1)))
        assert_equal(p.name, 'path_graph(1)')

        p=path_graph(10)
        assert_true(is_connected(p))
        assert_equal(sorted(list(p.degree().values())),
                     [1, 1, 2, 2, 2, 2, 2, 2, 2, 2])
        assert_equal(p.order()-1, p.size())

        dp=path_graph(3, create_using=DiGraph())
        assert_true(dp.has_edge(0,1))
        assert_false(dp.has_edge(1,0))

        mp=path_graph(10, create_using=MultiGraph())
        assert_true(mp.edges()==p.edges()) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:23,代码来源:test_classic.py

示例5: test_wheel_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def test_wheel_graph(self):
        for n, G in [(0, null_graph()), (1, empty_graph(1)),
                     (2, path_graph(2)), (3, complete_graph(3)),
                     (4, complete_graph(4))]:
            g=wheel_graph(n)
            assert_true(is_isomorphic( g, G))

        assert_equal(g.name, 'wheel_graph(4)')

        g=wheel_graph(10)
        assert_equal(sorted(list(g.degree().values())),
                     [3, 3, 3, 3, 3, 3, 3, 3, 3, 9])

        assert_raises(networkx.exception.NetworkXError,
                      wheel_graph, 10, create_using=DiGraph())

        mg=wheel_graph(10, create_using=MultiGraph())
        assert_equal(mg.edges(), g.edges()) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:20,代码来源:test_classic.py

示例6: from_edgelist

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def from_edgelist(edgelist, create_using=None):
    """Returns a graph from a list of edges.

    Parameters
    ----------
    edgelist : list or iterator
      Edge tuples

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
        Graph type to create. If graph instance, then cleared before populated.

    Examples
    --------
    >>> edgelist = [(0, 1)] # single edge (0,1)
    >>> G = nx.from_edgelist(edgelist)

    or

    >>> G = nx.Graph(edgelist) # use Graph constructor

    """
    G = nx.empty_graph(0, create_using)
    G.add_edges_from(edgelist)
    return G 
开发者ID:holzschu,项目名称:Carnets,代码行数:26,代码来源:convert.py

示例7: test_empty_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def test_empty_graph(self):
        G = nx.empty_graph()
        vpos = nx.random_layout(G, center=(1, 1))
        assert_equal(vpos, {})
        vpos = nx.circular_layout(G, center=(1, 1))
        assert_equal(vpos, {})
        vpos = nx.planar_layout(G, center=(1, 1))
        assert_equal(vpos, {})
        vpos = nx.bipartite_layout(G, G)
        assert_equal(vpos, {})
        vpos = nx.spring_layout(G, center=(1, 1))
        assert_equal(vpos, {})
        vpos = nx.fruchterman_reingold_layout(G, center=(1, 1))
        assert_equal(vpos, {})
        vpos = nx.spectral_layout(G, center=(1, 1))
        assert_equal(vpos, {})
        vpos = nx.shell_layout(G, center=(1, 1))
        assert_equal(vpos, {}) 
开发者ID:holzschu,项目名称:Carnets,代码行数:20,代码来源:test_layout.py

示例8: test_eccentricity

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def test_eccentricity(self):
        assert_equal(networkx.eccentricity(self.G, 1), 6)
        e = networkx.eccentricity(self.G)
        assert_equal(e[1], 6)
        sp = dict(networkx.shortest_path_length(self.G))
        e = networkx.eccentricity(self.G, sp=sp)
        assert_equal(e[1], 6)
        e = networkx.eccentricity(self.G, v=1)
        assert_equal(e, 6)
        # This behavior changed in version 1.8 (ticket #739)
        e = networkx.eccentricity(self.G, v=[1, 1])
        assert_equal(e[1], 6)
        e = networkx.eccentricity(self.G, v=[1, 2])
        assert_equal(e[1], 6)
        # test against graph with one node
        G = networkx.path_graph(1)
        e = networkx.eccentricity(G)
        assert_equal(e[0], 0)
        e = networkx.eccentricity(G, v=0)
        assert_equal(e, 0)
        assert_raises(networkx.NetworkXError, networkx.eccentricity, G, 1)
        # test against empty graph
        G = networkx.empty_graph()
        e = networkx.eccentricity(G)
        assert_equal(e, {}) 
开发者ID:holzschu,项目名称:Carnets,代码行数:27,代码来源:test_distance_measures.py

示例9: cycle_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def cycle_graph(n, create_using=None):
    """Returns the cycle graph $C_n$ of cyclically connected nodes.

    $C_n$ is a path with its two end-nodes connected.

    Parameters
    ----------
    n : int or iterable container of nodes
        If n is an integer, nodes are from `range(n)`.
        If n is a container of nodes, those nodes appear in the graph.
    create_using : NetworkX graph constructor, optional (default=nx.Graph)
       Graph type to create. If graph instance, then cleared before populated.

    Notes
    -----
    If create_using is directed, the direction is in increasing order.

    """
    n_orig, nodes = n
    G = empty_graph(nodes, create_using)
    G.add_edges_from(pairwise(nodes))
    G.add_edge(nodes[-1], nodes[0])
    return G 
开发者ID:holzschu,项目名称:Carnets,代码行数:25,代码来源:classic.py

示例10: dorogovtsev_goltsev_mendes_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def dorogovtsev_goltsev_mendes_graph(n, create_using=None):
    """Returns the hierarchically constructed Dorogovtsev-Goltsev-Mendes graph.

    n is the generation.
    See: arXiv:/cond-mat/0112143 by Dorogovtsev, Goltsev and Mendes.

    """
    G = empty_graph(0, create_using)
    if G.is_directed():
        raise NetworkXError("Directed Graph not supported")
    if G.is_multigraph():
        raise NetworkXError("Multigraph not supported")

    G.add_edge(0, 1)
    if n == 0:
        return G
    new_node = 2         # next node to be added
    for i in range(1, n + 1):  # iterate over number of generations.
        last_generation_edges = list(G.edges())
        number_of_edges_in_last_generation = len(last_generation_edges)
        for j in range(0, number_of_edges_in_last_generation):
            G.add_edge(new_node, last_generation_edges[j][0])
            G.add_edge(new_node, last_generation_edges[j][1])
            new_node += 1
    return G 
开发者ID:holzschu,项目名称:Carnets,代码行数:27,代码来源:classic.py

示例11: ladder_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def ladder_graph(n, create_using=None):
    """Returns the Ladder graph of length n.

    This is two paths of n nodes, with
    each pair connected by a single edge.

    Node labels are the integers 0 to 2*n - 1.

    """
    G = empty_graph(2 * n, create_using)
    if G.is_directed():
        raise NetworkXError("Directed Graph not supported")
    G.add_edges_from(pairwise(range(n)))
    G.add_edges_from(pairwise(range(n, 2 * n)))
    G.add_edges_from((v, v + n) for v in range(n))
    return G 
开发者ID:holzschu,项目名称:Carnets,代码行数:18,代码来源:classic.py

示例12: tree_decomp

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def tree_decomp(self):
        clusters = self.clusters
        graph = nx.empty_graph( len(clusters) )
        for atom, nei_cls in enumerate(self.atom_cls):
            if len(nei_cls) <= 1: continue
            bonds = [c for c in nei_cls if len(clusters[c]) == 2]
            rings = [c for c in nei_cls if len(clusters[c]) > 4] #need to change to 2

            if len(nei_cls) > 2 and len(bonds) >= 2:
                clusters.append([atom])
                c2 = len(clusters) - 1
                graph.add_node(c2)
                for c1 in nei_cls:
                    graph.add_edge(c1, c2, weight = 100)

            elif len(rings) > 2: #Bee Hives, len(nei_cls) > 2 
                clusters.append([atom]) #temporary value, need to change
                c2 = len(clusters) - 1
                graph.add_node(c2)
                for c1 in nei_cls:
                    graph.add_edge(c1, c2, weight = 100)
            else:
                for i,c1 in enumerate(nei_cls):
                    for c2 in nei_cls[i + 1:]:
                        inter = set(clusters[c1]) & set(clusters[c2])
                        graph.add_edge(c1, c2, weight = len(inter))

        n, m = len(graph.nodes), len(graph.edges)
        assert n - m <= 1 #must be connected
        return graph if n - m == 1 else nx.maximum_spanning_tree(graph) 
开发者ID:wengong-jin,项目名称:hgraph2graph,代码行数:32,代码来源:mol_graph.py

示例13: test_input_not_clique

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def test_input_not_clique(self, dim):
        """Tests if function raises a ``ValueError`` when input is not a clique"""
        with pytest.raises(ValueError, match="Input subgraph is not a clique"):
            clique.grow([0, 1], nx.empty_graph(dim)) 
开发者ID:XanaduAI,项目名称:strawberryfields,代码行数:6,代码来源:test_clique.py

示例14: test_input_not_subgraph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def test_input_not_subgraph(self, dim):
        """Test if function raises a ``ValueError`` when input is not a subgraph"""
        with pytest.raises(ValueError, match="Input is not a valid subgraph"):
            clique.grow([dim + 1], nx.empty_graph(dim)) 
开发者ID:XanaduAI,项目名称:strawberryfields,代码行数:6,代码来源:test_clique.py

示例15: test_input_valid_subgraph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import empty_graph [as 别名]
def test_input_valid_subgraph(self, dim):
        """Tests if function raises a ``ValueError`` when input is not a clique"""
        with pytest.raises(ValueError, match="Input is not a valid subgraph"):
            clique.swap([0, dim], nx.empty_graph(dim)) 
开发者ID:XanaduAI,项目名称:strawberryfields,代码行数:6,代码来源:test_clique.py


注:本文中的networkx.empty_graph方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。