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


Python Graph.neighbors方法代码示例

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


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

示例1: nauty

# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import neighbors [as 别名]

#.........这里部分代码省略.........
        - ``options`` (string) -- anything else that should be forwarded as
          input to Nauty's genbg. See its documentation for more information :
          `<http://cs.anu.edu.au/~bdm/nauty/>`_.

          .. NOTE::

              For genbg the *first class* elements are vertices, and *second
              class* elements are the hypergraph's sets.

        OUTPUT:

        A tuple of tuples.

        EXAMPLES:

        Small hypergraphs::

            sage: list(hypergraphs.nauty(4,2)) # optional - nauty
            [((), (0,), (1,), (0, 1))]

        Only connected ones::

            sage: list(hypergraphs.nauty(2,2, connected = True)) # optional - nauty
            [((0,), (0, 1))]

        Non-empty sets only::

            sage: list(hypergraphs.nauty(3,2, set_min_size = 1)) # optional - nauty
            [((0,), (1,), (0, 1))]

        The Fano Plane, as the only 3-uniform hypergraph with 7 sets and 7
        vertices::

            sage: fano = next(hypergraphs.nauty(7, 7, uniform=3, max_intersection=1)) # optional - nauty
            sage: print fano # optional - nauty
            ((0, 1, 2), (0, 3, 4), (0, 5, 6), (1, 3, 5), (2, 4, 5), (2, 3, 6), (1, 4, 6))

        The Fano Plane, as the only 3-regular hypergraph with 7 sets and 7
        vertices::

            sage: fano = next(hypergraphs.nauty(7, 7, regular=3, max_intersection=1)) # optional - nauty
            sage: print fano # optional - nauty
            ((0, 1, 2), (0, 3, 4), (0, 5, 6), (1, 3, 5), (2, 4, 5), (2, 3, 6), (1, 4, 6))
        """
        import subprocess
        from sage.misc.package import is_package_installed
        if not is_package_installed("nauty"):
            raise TypeError("The optional nauty spkg does not seem to be installed")

        nauty_input = options

        if connected:
            nauty_input += " -c"

        if not multiple_sets:
            nauty_input += " -z"

        if not max_intersection is None:
            nauty_input += " -Z"+str(max_intersection)

        # degrees and sizes
        if not regular is False:
            vertex_max_degree = vertex_min_degree = regular
        if vertex_max_degree is None:
            vertex_max_degree = number_of_sets
        if vertex_min_degree is None:
            vertex_min_degree = 0

        if not uniform is False:
            set_max_size = set_min_size = uniform
        if set_max_size is None:
            set_max_size = number_of_vertices
        if set_min_size is None:
            set_min_size = 0

        nauty_input += " -d"+str(vertex_min_degree)+":"+str(set_min_size)
        nauty_input += " -D"+str(vertex_max_degree)+":"+str(set_max_size)


        nauty_input +=  " "+str(number_of_vertices) +" "+str(number_of_sets)+" "

        sp = subprocess.Popen("genbg {0}".format(nauty_input), shell=True,
                              stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE, close_fds=True)

        if debug:
            yield sp.stderr.readline()

        gen = sp.stdout
        total = number_of_sets + number_of_vertices
        while True:
            try:
                s = next(gen)
            except StopIteration:
                raise StopIteration("Exhausted list of graphs from nauty geng")

            from sage.graphs.graph import Graph
            G = Graph(s[:-1], format='graph6')

            yield tuple( tuple( x for x in G.neighbors(v)) for v in range(number_of_vertices, total))
开发者ID:aaditya-thakkar,项目名称:sage,代码行数:104,代码来源:hypergraph_generators.py

示例2: _check_pbd

# 需要导入模块: from sage.graphs.graph import Graph [as 别名]
# 或者: from sage.graphs.graph.Graph import neighbors [as 别名]
def _check_pbd(B,v,S):
    r"""
    Checks that ``B`` is a PBD on ``v`` points with given block sizes ``S``.

    The points of the balanced incomplete block design are implicitely assumed
    to be `\{0, ..., v-1\}`.

    INPUT:

    - ``B`` -- a list of blocks

    - ``v`` (integer) -- number of points

    - ``S`` -- list of integers `\geq 2`.

    EXAMPLE::

        sage: designs.balanced_incomplete_block_design(40,4).blocks() # indirect doctest
        [[0, 1, 2, 12], [0, 3, 6, 9], [0, 4, 8, 10],
         [0, 5, 7, 11], [0, 13, 26, 39], [0, 14, 25, 28],
         [0, 15, 27, 38], [0, 16, 22, 32], [0, 17, 23, 34],
        ...
        sage: from sage.combinat.designs.bibd import _check_pbd
        sage: _check_pbd([[1],[]],1,[1,0])
        Traceback (most recent call last):
        ...
        RuntimeError: All integers of S must be >=2

    TESTS::

        sage: _check_pbd([[1,2]],2,[2])
        Traceback (most recent call last):
        ...
        RuntimeError: The PBD covers a point 2 which is not in {0, 1}
        sage: _check_pbd([[1,2]]*2,2,[2])
        Traceback (most recent call last):
        ...
        RuntimeError: The pair (1,2) is covered more than once
        sage: _check_pbd([],2,[2])
        Traceback (most recent call last):
        ...
        RuntimeError: The pair (0,1) is not covered
        sage: _check_pbd([[1,2],[1]],2,[2])
        Traceback (most recent call last):
        ...
        RuntimeError: A block has size 1 while S=[2]
    """
    from itertools import combinations
    from sage.graphs.graph import Graph

    for X in B:
        if len(X) not in S:
            raise RuntimeError("A block has size {} while S={}".format(len(X),S))

    if any(x < 2 for x in S):
        raise RuntimeError("All integers of S must be >=2")

    if v == 0 or v == 1:
        if B:
            raise RuntimeError("A PBD with v<=1 is expected to be empty.")

    g = Graph()
    g.add_vertices(range(v))
    m = 0
    for X in B:
        for i,j in combinations(X,2):
            g.add_edge(i,j)
            m_tmp = g.size()
            if m_tmp != m+1:
                raise RuntimeError("The pair ({},{}) is covered more than once".format(i,j))
            m = m_tmp

    if g.vertices() != range(v):
        from sage.sets.integer_range import IntegerRange
        p = (set(g.vertices())-set(range(v))).pop()
        raise RuntimeError("The PBD covers a point {} which is not in {}".format(p,IntegerRange(v)))

    if not g.is_clique():
        for p1 in g:
            if g.degree(p1) != v-1:
                break
        neighbors = g.neighbors(p1)+[p1]
        p2 = (set(g.vertices())-set(neighbors)).pop()
        raise RuntimeError("The pair ({},{}) is not covered".format(p1,p2))

    return B
开发者ID:Etn40ff,项目名称:sage,代码行数:88,代码来源:bibd.py


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