# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1377781348 7200
# Thu Aug 29 15:02:28 2013 +0200
# Node ID 3dbcf76e4a42e00a112c81f5960272e52b7bc29b
# Parent aeeddd6d16575acdd54eadb3b57fe9b070197e60
Test if a graph is distanceregular  reviewer's remarks
diff git a/sage/graphs/distances_all_pairs.pyx b/sage/graphs/distances_all_pairs.pyx
a

b


164  164  # The list of waiting vertices, the beginning and the end of the list 
165  165  
166  166  cdef unsigned short * waiting_list = <unsigned short *> sage_malloc(n*sizeof(unsigned short)) 
167   if waiting_list==NULL: 
 167  if waiting_list == NULL: 
168  168  raise MemoryError() 
169  169  cdef unsigned short waiting_beginning = 0 
170  170  cdef unsigned short waiting_end = 0 
171  171  
172  172  cdef int * degree = <int *> sage_malloc(n*sizeof(int)) 
173   if degree==NULL: 
 173  if degree == NULL: 
174  174  sage_free(waiting_list) 
175  175  raise MemoryError() 
176  176  
… 
… 

193  193  c_distances = distances 
194  194  else: 
195  195  c_distances = <unsigned short *> sage_malloc( n * sizeof(unsigned short)) 
196   if c_distances==NULL: 
 196  if c_distances == NULL: 
197  197  sage_free(waiting_list) 
198  198  sage_free(degree) 
199  199  raise MemoryError() 
… 
… 

309  309  
310  310  cdef unsigned int n = G.order() 
311  311  cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short)) 
312   if distances==NULL: 
 312  if distances == NULL: 
313  313  raise MemoryError() 
314  314  cdef unsigned short * predecessors = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short)) 
315   if predecessors==NULL: 
 315  if predecessors == NULL: 
316  316  sage_free(distances) 
317  317  raise MemoryError() 
318  318  all_pairs_shortest_path_BFS(G, predecessors, distances, NULL) 
… 
… 

401  401  
402  402  cdef unsigned int n = G.order() 
403  403  cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short)) 
404   if distances==NULL: 
 404  if distances == NULL: 
405  405  raise MemoryError() 
406  406  all_pairs_shortest_path_BFS(G, NULL, distances, NULL) 
407  407  
… 
… 

468  468  Tests if the graph is distanceregular 
469  469  
470  470  A graph `G` is distanceregular if there exist integers `d_1,...,d_n` such 
471   that for every vertex `v\in G` we have `d_i=\#\{u:d_G(u,v) =i\}`. 
 471  that for every vertex `v\in G` we have `d_i=\#\{u:d_G(u,v) =i\}`. Thus a 
 472  stronglyregular graph is also distanceregular, and a distanceregular 
 473  graph is necessarily regular too. 
472  474  
473  475  For more information on distanceregular graphs, see its associated 
474  476  :wikipedia:`wikipedia page <Distanceregular_graph>`. 
… 
… 

480  482  obtain `d_i` by doing ``dictionary.get(i,0)``). Set to ``False`` by 
481  483  default. 
482  484  
 485  .. SEEALSO:: 
 486  
 487  * :meth:`Graph.is_regular` 
 488  * :meth:`Graph.is_strongly_regular` 
 489  
483  490  EXAMPLES:: 
484  491  
485  492  sage: g = graphs.PetersenGraph() 
… 
… 

488  495  sage: g.is_distance_regular(parameters = True) 
489  496  {1: 3, 2: 6} 
490  497  
491   Cube graphs, which are not strongly regular, are a bit more interesting;; 
 498  Cube graphs, which are not strongly regular, are a bit more interesting:: 
492  499  
493  500  sage: graphs.CubeGraph(4).is_distance_regular(parameters = True) 
494  501  {1: 4, 2: 6, 3: 4, 4: 1} 
495  502  
 503  TESTS:: 
 504  
 505  sage: graphs.PathGraph(2).is_distance_regular(parameters = True) 
 506  {1: 1} 
 507  
496  508  """ 
497  509  cdef int i,l 
498  510  cdef int n = G.order() 
499  511  
500   if n <= 2: 
 512  if n <= 1: 
501  513  return {} if parameters else True 
502  514  
503  515  if not G.is_regular(): 
… 
… 

512  524  cdef unsigned short * d_array = <unsigned short *> sage_calloc(2*n, sizeof(unsigned short)) 
513  525  cdef unsigned short * d_tmp = d_array + n 
514  526  
515   if d_array==NULL: 
 527  if d_array == NULL: 
516  528  sage_free(distance_matrix) 
517  529  raise MemoryError() 
518  530  
519  531  # Filling d_array 
520  532  cdef unsigned short * pointer = distance_matrix 
521  533  for i in range(n): 
522   d_array[pointer[i]] += 1 
 534  if pointer[i] < n: 
 535  d_array[pointer[i]] += 1 
523  536  pointer += n 
524  537  
525  538  # For each of the n1 other vertices 
… 
… 

528  541  # We set d_tmp and fill it with the data from the l^th row 
529  542  memset(d_tmp, 0, n*sizeof(unsigned short)) 
530  543  for i in range(n): 
531   d_tmp[pointer[i]] += 1 
 544  if pointer[i] < n: 
 545  d_tmp[pointer[i]] += 1 
532  546  
533  547  # If d_tmp != d_array, we are done 
534  548  if memcmp(d_array, d_tmp, n*sizeof(unsigned short)) != 0: 
… 
… 

607  621  return {}, {} 
608  622  
609  623  cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short)) 
610   if distances==NULL: 
 624  if distances == NULL: 
611  625  raise MemoryError() 
612  626  cdef unsigned short * c_distances = distances 
613  627  cdef unsigned short * predecessor = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short)) 
614   if predecessor==NULL: 
 628  if predecessor == NULL: 
615  629  sage_free(distances) 
616  630  raise MemoryError() 
617  631  cdef unsigned short * c_predecessor = predecessor 
… 
… 

667  681  cdef unsigned int n = G.order() 
668  682  
669  683  cdef unsigned short * ecc = <unsigned short *> sage_malloc(n*sizeof(unsigned short)) 
670   if ecc==NULL: 
 684  if ecc == NULL: 
671  685  raise MemoryError() 
672  686  cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short)) 
673   if distances==NULL: 
 687  if distances == NULL: 
674  688  sage_free(ecc) 
675  689  raise MemoryError() 
676  690  all_pairs_shortest_path_BFS(G, NULL, distances, ecc) 
… 
… 

992  1006  
993  1007  # init dist 
994  1008  t_dist = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short)) 
995   if t_dist==NULL: 
 1009  if t_dist == NULL: 
996  1010  raise MemoryError() 
997  1011  dist = <unsigned short **> sage_malloc(n*sizeof(unsigned short *)) 
998   if dist==NULL: 
 1012  if dist == NULL: 
999  1013  sage_free(t_dist) 
1000  1014  raise MemoryError() 
1001  1015  dist[0] = t_dist 
… 
… 

1011  1025  if paths: 
1012  1026  # init prec 
1013  1027  t_prec = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short)) 
1014   if t_prec==NULL: 
 1028  if t_prec == NULL: 
1015  1029  sage_free(t_dist) 
1016  1030  sage_free(dist) 
1017  1031  raise MemoryError() 
1018  1032  prec = <unsigned short **> sage_malloc(n*sizeof(unsigned short *)) 
1019   if prec==NULL: 
 1033  if prec == NULL: 
1020  1034  sage_free(t_dist) 
1021  1035  sage_free(dist) 
1022  1036  sage_free(t_prec) 