Ticket #9862: trac_9862.patch

File trac_9862.patch, 3.1 KB (added by edward.scheinerman, 9 years ago)
  • sage/graphs/graph_generators.py

    # HG changeset patch
    # User Ed Scheinerman <ers@jhu.edu>
    # Date 1283798783 14400
    # Node ID 39d543c5808f1a74dcfbe00be04604381b7e4ae5
    # Parent  5b338f2e484f2065d3d30d47bc204d6e9ed13d12
    trac 9862 Reimplementation of the IntervalGraph generator to allow repeated intervals in the input.
    
    diff -r 5b338f2e484f -r 39d543c5808f sage/graphs/graph_generators.py
    a b  
    40634063        vertex, two vertices being adjacent if the two corresponding
    40644064        (closed) intervals intersect.
    40654065
     4066        The vertices are named 0, 1, 2, and so on. The intervals used
     4067        to create the graph are saved with the graph and can be
     4068        recovered using g.get_vertex() or g.get_vertices().
     4069
    40664070        INPUT:
    40674071
    40684072        - ``intervals`` -- the list of pairs `(a_i,b_i)`
     
    40704074
    40714075        NOTE:
    40724076
    4073         The intervals `(a_i,b_i)` must verify `a_i<b_i`.
     4077        The intervals `(a_i,b_i)` need not verify `a_i<b_i`.
    40744078
    40754079        EXAMPLE:
    40764080
     
    40794083
    40804084            sage: intervals = [(i,i+2) for i in range(9)]
    40814085
    4082         In the corresponding graph...::
     4086        In the corresponding graph... ::
    40834087
    40844088            sage: g = graphs.IntervalGraph(intervals)
    4085             sage: sorted(g.neighbors((3,5)))
    4086             [(1, 3), (2, 4), (4, 6), (5, 7)]
    4087 
    4088         And the clique number is as expected ::
    4089 
    4090             sage: g.clique_number()
    4091             3
     4089            sage: g.get_vertex(3)
     4090            (3, 5)
     4091            sage: neigh = g.neighbors(3)
     4092            sage: for v in neigh: print g.get_vertex(v)
     4093            (1, 3)
     4094            (2, 4)
     4095            (4, 6)
     4096            (5, 7)
     4097
     4098        The is_interval() method verifies that this graph is an interval
     4099        graph. ::
     4100
     4101            sage: g.is_interval()
     4102            True
     4103
     4104        The intervals in the list need not be distinct. ::
     4105       
     4106            sage: intervals = [ (1,2), (1,2), (1,2), (2,3), (3,4) ]
     4107            sage: g = graphs.IntervalGraph(intervals)
     4108            sage: g.clique_maximum()
     4109            [0, 1, 2, 3]
     4110            sage: g.get_vertices()
     4111            {0: (1, 2), 1: (1, 2), 2: (1, 2), 3: (2, 3), 4: (3, 4)}
     4112
    40924113        """
    40934114
    4094         intervals = sorted(intervals)
    4095 
     4115        n = len(intervals)
     4116        g = graph.Graph(vertices=range(n))
    40964117
    40974118        edges = []
    4098         while intervals:
    4099             x = intervals.pop(0)
    4100             for y in intervals:
    4101                 if y[0] <= x[1]:
    4102                     edges.append((x,y))
    4103                 else:
    4104                     break
    4105         g = graph.Graph(vertices=intervals)
     4119
     4120        for i in range(n-1):
     4121            I = intervals[i]
     4122            for j in range(i+1,n):
     4123                J = intervals[j]
     4124                if max(I) < min(J) or max(J) < min(I): continue
     4125                edges.append((i,j))
     4126               
    41064127        g.add_edges(edges)
     4128
     4129        rep = dict( zip(range(n),intervals) )
     4130        g.set_vertices(rep)
     4131
    41074132        return g
    41084133
     4134
    41094135    def RandomLobster(self, n, p, q, seed=None):
    41104136        """
    41114137        Returns a random lobster.