# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1280798449 28800
# Node ID ef4ea6b9d33a657e96957eee6b1e16b6dd749f9c
# Parent 0b12ec9add58ab64d979b10238f140e1ee19c287
trac 9058  Computing the kcore of a graph
diff r 0b12ec9add58 r ef4ea6b9d33a sage/graphs/generic_graph.py
a

b


8618  8618  
8619  8619  ### Cores 
8620  8620  
8621   def cores(self, with_labels=False): 
 8621  def cores(self, k = None, with_labels=False): 
8622  8622  """ 
8623  8623  Returns the core number for each vertex in an ordered list. 
8624  8624  
… 
… 

8647  8647  
8648  8648  INPUT: 
8649  8649  
 8650   ``k`` (integer)  
 8651  
 8652  * If ``k = None`` (default), returns the core number for 
 8653  each vertex, according to the value of ``with_labels``. 
 8654  
 8655  * Otherwise, returns a pair ``(ordering, core)``, where 
 8656  ``core`` is the list of vertices in the `k`core of 
 8657  ``self``, and ``ordering`` is an elimination order for 
 8658  the others vertices such that each vertex is of degree 
 8659  strictly less than `k` when it is to be eliminated from 
 8660  the graph. 
8650  8661  
8651  8662   ``with_labels``  default False returns list as 
8652  8663  described above. True returns dict keyed by vertex labels. 
… 
… 

8681  8692  {0: 3, 1: 3, 2: 3, 3: 3, 4: 2, 5: 2, 6: 3, 7: 1, 8: 3, 9: 3, 10: 3, 11: 3, 12: 3, 13: 3, 14: 2, 15: 3, 16: 3, 17: 3, 18: 3, 19: 3} 
8682  8693  sage: [v for v,c in cores.items() if c>=2] # the vertices in the 2core 
8683  8694  [0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 
 8695  
 8696  Checking the 2core of a random lobster is indeed the empty set:: 
 8697  
 8698  sage: g = graphs.RandomLobster(20,.5,.5) 
 8699  sage: ordering, core = g.cores(2) 
 8700  sage: len(core) == 0 
 8701  True 
8684  8702  """ 
8685  8703  # compute the degrees of each vertex 
8686  8704  degrees=self.degree(labels=True) 
… 
… 

8701  8719  nbrs=dict((v,set(self.neighbors(v))) for v in self) 
8702  8720  # form vertex core building up from smallest 
8703  8721  for v in verts: 
 8722  
 8723  # If all the vertices have a degree larger than k, we can 
 8724  # return our answer if k != None 
 8725  if k is not None and core[v] >= k: 
 8726  return verts[:vert_pos[v]], verts[vert_pos[v]:] 
 8727  
8704  8728  for u in nbrs[v]: 
8705  8729  if core[u] > core[v]: 
8706  8730  nbrs[u].remove(v) 
… 
… 

8718  8742  bin_boundaries[core[u]]+=1 
8719  8743  core[u] = 1 
8720  8744  
 8745  if k is not None: 
 8746  return verts, [] 
 8747  
8721  8748  if with_labels: 
8722  8749  return core 
8723  8750  else: 