Ticket #8284: trac_8284.patch

File trac_8284.patch, 5.3 KB (added by ncohen, 11 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1275999583 -7200
    # Node ID 1f6e22e68ddb753517cb9ad54ee58fa3e9de8423
    # Parent  556bb66e4c6dbb92a4ee37c1750d82a5c6298eeb
    trac 8284 -- IntervalGraph generator and a bug in is_chordal
    
    diff -r 556bb66e4c6d -r 1f6e22e68ddb sage/graphs/generic_graph.py
    a b  
    70917091            sage: g.is_chordal()
    70927092            True
    70937093   
     7094        The disjoint union of chordal graphs is still chordal::
     7095       
     7096            sage: (2*g).is_chordal()
     7097            True
     7098
     7099
    70947100        Of course, the Petersen Graph is not chordal as it has girth 5 ::
    70957101   
    70967102            sage: g = graphs.PetersenGraph()
     
    71117117          Pacific J. Math 1965
    71127118          Vol. 15, number 3, pages 835--855
    71137119        """
     7120
     7121        if not self.is_connected():
     7122            for gg in self.connected_components_subgraphs():
     7123                if not gg.is_chordal():
     7124                    return False
     7125
     7126            return True
     7127
    71147128   
    71157129        peo,t_peo = self.lex_BFS(tree=True)
    71167130   
     
    87778791                            if w not in seen:
    87788792                                queue.append((w, d+1))
    87798793
    8780     def lex_BFS(self,reverse=False,tree=False):
     8794    def lex_BFS(self,reverse=False,tree=False, initial_vertex = None):
    87818795        r"""
    87828796        Performs a Lex BFS on the graph.
    87838797   
     
    87898803   
    87908804        INPUT:
    87918805   
    8792         - ``reverse`` ( boolean ) -- whether to return the vertices
     8806        - ``reverse`` (boolean) -- whether to return the vertices
    87938807          in discovery order, or the reverse.
     8808
     8809          ``False`` by default.
     8810
     8811        - ``tree`` (boolean) -- whether to return the discovery
     8812          directed tree (each vertex being linked to the one that
     8813          saw it for the first time)
     8814
     8815          ``False`` by default.
     8816
     8817        - ``initial_vertex`` -- the first vertex to consider.
     8818
     8819          ``None`` by default.
    87948820   
    87958821        ALGORITHM:
    87968822   
     
    88218847            sage: g = graphs.PathGraph(3).lexicographic_product(graphs.CompleteGraph(2))
    88228848            sage: g.lex_BFS(reverse=True)
    88238849            [(2, 1), (2, 0), (1, 1), (1, 0), (0, 1), (0, 0)]
     8850
     8851
     8852        And the vertices at the end of the tree of discovery are, for
     8853        chordal graphs, simplicial vertices (their neighborhood is
     8854        a complete graph)::
     8855
     8856            sage: g = graphs.ClawGraph().lexicographic_product(graphs.CompleteGraph(2))
     8857            sage: v = g.lex_BFS()[-1]
     8858            sage: peo, tree = g.lex_BFS(initial_vertex = v,  tree=True)
     8859            sage: leaves = [v for v in tree if tree.in_degree(v) ==0]
     8860            sage: all([g.subgraph(g.neighbors(v)).is_clique() for v in leaves])
     8861            True
     8862
    88248863        """
    88258864        id_inv = dict([(i,v) for (v,i) in zip(self.vertices(),range(self.order()))])
    88268865        code = [[] for i in range(self.order())]
     
    88338872        pred = [-1]*self.order()
    88348873   
    88358874        add_element = (lambda y:value.append(id_inv[y])) if not reverse else (lambda y: value.insert(0,id_inv[y]))
     8875
     8876        # Should we take care of the first vertex we pick ?
     8877        first = True if initial_vertex is not None else False
     8878
    88368879   
    88378880        while vertices:
    8838             v = max(vertices,key=l)
     8881
     8882            if not first:
     8883                v = max(vertices,key=l)
     8884            else:
     8885                v = self.vertices().index(initial_vertex)
     8886                first = False
     8887
    88398888            vertices.remove(v)
    88408889            vector = m.column(v)
    88418890            for i in vertices:
  • sage/graphs/graph_generators.py

    diff -r 556bb66e4c6d -r 1f6e22e68ddb sage/graphs/graph_generators.py
    a b  
    37933793        from sage.misc.prandom import random
    37943794
    37953795        intervals = [tuple(sorted((random(), random()))) for i in range(n)]
    3796         intervals.sort()
     3796
     3797        return self.IntervalGraph(intervals)
     3798
     3799    def IntervalGraph(self,intervals):
     3800        r"""
     3801        Returns the graph corresponding to the given intervals.
     3802
     3803        An interval graph is built from a list `(a_i,b_i)_{1\leq i \leq n}`
     3804        of intervals : to each interval of the list is associated one
     3805        vertex, two vertices being adjacent if the two corresponding
     3806        (closed) intervals intersect.
     3807
     3808        INPUT:
     3809
     3810        - ``intervals`` -- the list of pairs `(a_i,b_i)`
     3811          defining the graph.
     3812
     3813        NOTE:
     3814
     3815        The intervals `(a_i,b_i)` must verify `a_i<b_i`.
     3816
     3817        EXAMPLE:
     3818
     3819        The following line creates the sequence of intervals
     3820        `(i, i+2)` for i in `[0, ..., 8]`::
     3821
     3822            sage: intervals = [(i,i+2) for i in range(9)]
     3823
     3824        In the corresponding graph...::
     3825
     3826            sage: g = graphs.IntervalGraph(intervals)
     3827            sage: sorted(g.neighbors((3,5)))
     3828            [(1, 3), (2, 4), (4, 6), (5, 7)]
     3829
     3830        And the clique number is as expected ::
     3831
     3832            sage: g.clique_number()
     3833            3
     3834        """
     3835
     3836        intervals = sorted(intervals)
     3837
     3838
    37973839        edges = []
    37983840        while intervals:
    3799             x = intervals.pop()
     3841            x = intervals.pop(0)
    38003842            for y in intervals:
    38013843                if y[0] <= x[1]:
    38023844                    edges.append((x,y))