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


Python DiGraph.is_connected方法代码示例

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


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

示例1: random_cyclically_reduced_stallings_graph

# 需要导入模块: from sage.graphs.digraph import DiGraph [as 别名]
# 或者: from sage.graphs.digraph.DiGraph import is_connected [as 别名]
def random_cyclically_reduced_stallings_graph(n, r=2, verbose=False, merge=False):
    r"""
    Return a uniformly chosen Stallings graph of n vertices over r letters.

    INPUT:

    - ``n`` -- integer, size of graph
    - ``r`` -- integer (default: ``2``), number of generators of the free
      group
    - ``verbose`` -- bool (default: ``False``)

    .. NOTE::

        The probability that G is connected is 1 - 2^r / n^(r-1) +
        o(1/n^(r-1)) which is approx. 1

    OUTPUT:

        digraph, integer, integer

    EXAMPLES::

        sage: from slabbe import random_cyclically_reduced_stallings_graph
        sage: G,_,_ = random_cyclically_reduced_stallings_graph(20, 2)
        sage: G
        Looped multi-digraph on 20 vertices

    ::

        sage: random_cyclically_reduced_stallings_graph(20, 5)[0]
        Looped multi-digraph on 20 vertices

    With verbose output::

        sage: G = random_cyclically_reduced_stallings_graph(20, 2, verbose=True)   # random
        rejecting because graph is not connected
        rejecting because graph has a vertex of degree <=1
        rejecting because graph has a vertex of degree <=1
        rejecting because graph has a vertex of degree <=1

    For displaying purposes, the following merges the multiedges
    automatically::

        sage: G,_,_ = random_cyclically_reduced_stallings_graph(20, 2)
        sage: from slabbe import TikzPicture
        sage: tikz = TikzPicture.from_graph(G)
        sage: _ = tikz.pdf(view=False)

    AUTHORS:

    - Sébastien Labbé and Pascal Weil, Dec 14, 2017, Sage Thursdays at LaBRI
    """
    from sage.misc.latex import LatexExpr

    # reject statistics
    not_connected_count = 0
    has_degree_1_count = 0

    while True:
        injections = [random_partial_injection(n) for _ in range(r)]

        edges = []
        for i,injection in enumerate(injections):
            label = LatexExpr('a_{}'.format(i))
            edges.extend([(j,image_j,label) for (j,image_j) in enumerate(injection)
                                        if not image_j is None])

        G = DiGraph([range(n), edges], format='vertices_and_edges',
                loops=True, multiedges=True)

        if not G.is_connected():
            not_connected_count += 1
            if verbose:
                print("rejecting because graph is not connected")
            continue

        if not all(d>=2 for d in G.degree_iterator()):
            has_degree_1_count += 1
            if verbose:
                print("rejecting because graph has a vertex of degree <=1")
            continue

        return G, not_connected_count, has_degree_1_count
开发者ID:seblabbe,项目名称:slabbe,代码行数:85,代码来源:partial_injection.py


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