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


Python DiGraph.edge_label方法代码示例

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


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

示例1: is_partial_cube

# 需要导入模块: from sage.graphs.digraph import DiGraph [as 别名]
# 或者: from sage.graphs.digraph.DiGraph import edge_label [as 别名]

#.........这里部分代码省略.........
    unionfind = DisjointSet(contracted.edges(labels = False))
    available = n-1

    # Main contraction loop in place of the original algorithm's recursion
    while contracted.order() > 1:
        # Find max degree vertex in contracted, and update label limit
        deg, root = max((contracted.out_degree(v), v) for v in contracted)
        if deg > available:
            return fail
        available -= deg

        # Set up bitvectors on vertices
        bitvec = {v:0 for v in contracted}
        neighbors = {}
        for i, neighbor in enumerate(contracted[root]):
            bitvec[neighbor] = 1 << i
            neighbors[1 << i] = neighbor

        # Breadth first search to propagate bitvectors to the rest of the graph
        for level in breadth_first_level_search(contracted, root):
            for v in level:
                for w in level[v]:
                    bitvec[w] |= bitvec[v]

        # Make graph of labeled edges and union them together
        labeled = Graph([contracted.vertices(), []])
        for v, w in contracted.edge_iterator(labels = False):
            diff = bitvec[v]^bitvec[w]
            if not diff or bitvec[w] &~ bitvec[v] == 0:
                continue    # zero edge or wrong direction
            if diff not in neighbors:
                return fail
            neighbor = neighbors[diff]
            unionfind.union(contracted.edge_label(v, w),
                            contracted.edge_label(root, neighbor))
            unionfind.union(contracted.edge_label(w, v),
                            contracted.edge_label(neighbor, root))
            labeled.add_edge(v, w)

        # Map vertices to components of labeled-edge graph
        component = {}
        for i, SCC in enumerate(labeled.connected_components()):
            for v in SCC:
                component[v] = i

        # generate new compressed subgraph
        newgraph = DiGraph()
        for v, w, t in contracted.edge_iterator():
            if bitvec[v] == bitvec[w]:
                vi = component[v]
                wi = component[w]
                if vi == wi:
                    return fail
                if newgraph.has_edge(vi, wi):
                    unionfind.union(newgraph.edge_label(vi, wi), t)
                else:
                    newgraph.add_edge(vi, wi, t)
        contracted = newgraph

    # Make a digraph with edges labeled by the equivalence classes in unionfind
    g = DiGraph({v: {w: unionfind.find((v, w)) for w in G[v]} for v in G})

    # Associates to a vertex the token that acts on it, an check that
    # no two edges on a single vertex have the same label
    action = {}
    for v in g:
开发者ID:saraedum,项目名称:sage-renamed,代码行数:70,代码来源:partial_cube.py

示例2: reduced_rauzy_graph

# 需要导入模块: from sage.graphs.digraph import DiGraph [as 别名]
# 或者: from sage.graphs.digraph.DiGraph import edge_label [as 别名]

#.........这里部分代码省略.........
        in the reduced Rauzy graph of order `n` whose label is the label of
        the path in `G_n`.
        
        .. NOTE::

            In the case of infinite recurrent non periodic words, this
            definition correspond to the following one that can be found in
            [1] and [2]  where a simple path is a path that begins with a
            special factor, ends with a special factor and contains no
            other vertices that are special:

            The reduced Rauzy graph of factors of length `n` is obtained
            from `G_n` by replacing each simple path `P=v_1 v_2 ...
            v_{\ell}` with an edge `v_1 v_{\ell}` whose label is the
            concatenation of the labels of the edges of `P`.

        EXAMPLES::

            sage: w = Word(range(10)); w
            word: 0123456789
            sage: g = w.reduced_rauzy_graph(3); g
            Looped multi-digraph on 2 vertices
            sage: g.vertices()
            [word: 012, word: 789]
            sage: g.edges()
            [(word: 012, word: 789, word: 3456789)]

        For the Fibonacci word::

            sage: f = words.FibonacciWord()[:100]
            sage: g = f.reduced_rauzy_graph(8);g
            Looped multi-digraph on 2 vertices
            sage: g.vertices()
            [word: 01001010, word: 01010010]
            sage: g.edges()
            [(word: 01001010, word: 01010010, word: 010), (word: 01010010, word: 01001010, word: 01010), (word: 01010010, word: 01001010, word: 10)]

        For periodic words::

            sage: from itertools import cycle
            sage: w = Word(cycle('abcd'))[:100]
            sage: g = w.reduced_rauzy_graph(3)
            sage: g.edges()
            [(word: abc, word: abc, word: dabc)]

        ::

            sage: w = Word('111')
            sage: for i in range(5) : w.reduced_rauzy_graph(i)
            Looped digraph on 1 vertex
            Looped digraph on 1 vertex
            Looped digraph on 1 vertex
            Looped multi-digraph on 1 vertex
            Looped multi-digraph on 0 vertices

        For ultimately periodic words::

            sage: sigma = WordMorphism('a->abcd,b->cd,c->cd,d->cd')
            sage: w = sigma.fixed_point('a')[:100]; w
            word: abcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd...
            sage: g = w.reduced_rauzy_graph(5)
            sage: g.vertices()
            [word: abcdc, word: cdcdc]
            sage: g.edges()
            [(word: abcdc, word: cdcdc, word: dc), (word: cdcdc, word: cdcdc, word: dc)]

        AUTHOR:

        Julien Leroy (March 2010): initial version

        REFERENCES:

        - [1] M. Bucci et al.  A. De Luca, A. Glen, L. Q. Zamboni, A
          connection between palindromic and factor complexity using
          return words," Advances in Applied Mathematics 42 (2009) 60-74.

        - [2] L'ubomira Balkova, Edita Pelantova, and Wolfgang Steiner.
          Sequences with constant number of return words. Monatsh. Math,
          155 (2008) 251-263.
        """
        from sage.graphs.all import DiGraph
        from copy import copy
        g = copy(self.rauzy_graph(n))      
        # Otherwise it changes the rauzy_graph function.
        l = [v for v in g if g.in_degree(v)==1 and g.out_degree(v)==1]
        if g.num_verts() !=0 and len(l)==g.num_verts():       
            # In this case, the Rauzy graph is simply a cycle.
            g = DiGraph()
            g.allow_loops(True)
            g.add_vertex(self[:n])
            g.add_edge(self[:n],self[:n],self[n:n+len(l)])
        else:
            g.allow_loops(True)
            g.allow_multiple_edges(True)
            for v in l:
                [i] = g.neighbors_in(v)
                [o] = g.neighbors_out(v)
                g.add_edge(i,o,g.edge_label(i,v)[0]*g.edge_label(v,o)[0])
                g.delete_vertex(v)
        return g
开发者ID:bgxcpku,项目名称:sagelib,代码行数:104,代码来源:nfactor_enumerable_word.py


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