# 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


4063  4063  vertex, two vertices being adjacent if the two corresponding 
4064  4064  (closed) intervals intersect. 
4065  4065  
 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  
4066  4070  INPUT: 
4067  4071  
4068  4072   ``intervals``  the list of pairs `(a_i,b_i)` 
… 
… 

4070  4074  
4071  4075  NOTE: 
4072  4076  
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`. 
4074  4078  
4075  4079  EXAMPLE: 
4076  4080  
… 
… 

4079  4083  
4080  4084  sage: intervals = [(i,i+2) for i in range(9)] 
4081  4085  
4082   In the corresponding graph...:: 
 4086  In the corresponding graph... :: 
4083  4087  
4084  4088  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  
4092  4113  """ 
4093  4114  
4094   intervals = sorted(intervals) 
4095   
 4115  n = len(intervals) 
 4116  g = graph.Graph(vertices=range(n)) 
4096  4117  
4097  4118  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(n1): 
 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  
4106  4127  g.add_edges(edges) 
 4128  
 4129  rep = dict( zip(range(n),intervals) ) 
 4130  g.set_vertices(rep) 
 4131  
4107  4132  return g 
4108  4133  
 4134  
4109  4135  def RandomLobster(self, n, p, q, seed=None): 
4110  4136  """ 
4111  4137  Returns a random lobster. 