# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1301174132 3600
# Node ID efb11aad1cc4823d83a158727550e94b906875df
# Parent f8bdf65336cfdc59771dca5141e5b96d72c1d57b
trac 11053  improving shortest path all pairs through BFS computations
diff r f8bdf65336cf r efb11aad1cc4 sage/graphs/base/c_graph.pyx
a

b


3138  3138  cdef int *outneighbors 
3139  3139  cdef int o_n_size 
3140  3140  cdef int i 
 3141  
 3142  # Copying the whole graph to obtain the list of neighbors quicker than by 
 3143  # calling out_neighbors 
 3144  
 3145  # The edges are stored in the vector p_edges. This vector contains, from 
 3146  # left to right The list of the first vertex's outneighbors, then the 
 3147  # second's, then the third's, ... 
 3148  # 
 3149  # The outneighbors of vertex i are enumerated from 
 3150  # 
 3151  # p_vertices[i] to p_vertices[i+1]  1 
 3152  # (if p_vertices[i] is equal to p_vertices[i+1], then i has no outneighbours) 
 3153  
 3154  cdef short ** p_vertices = <short **> sage_malloc(n*sizeof(short *)) 
 3155  cdef short * p_edges = <short *> sage_malloc(cg.num_arcs*sizeof(short)) 
 3156  cdef short * p_next = p_edges 
 3157  
 3158  for v in vertices: 
 3159  outneighbors = <int *>sage_malloc(cg.out_degrees[v] * sizeof(int)) 
 3160  cg.out_neighbors_unsafe(v, outneighbors, cg.out_degrees[v]) 
 3161  p_vertices[v] = p_next 
 3162  
 3163  for 0<= i < cg.out_degrees[v]: 
 3164  p_next[0] = <short> outneighbors[i] 
 3165  p_next = p_next + 1 
 3166  
 3167  sage_free(outneighbors) 
 3168  
 3169  # We run n different BFS taking each vertex as a source 
3141  3170  for source in vertices: 
 3171  
 3172  # The source is seen 
3142  3173  bitset_set_first_n(seen, 0) 
3143  3174  bitset_add(seen, source) 
3144  3175  
 3176  # Its parameters can already be set 
3145  3177  v_distances[source] = 0 
3146  3178  v_prec[source] = source 
 3179  
 3180  # and added to the queue 
3147  3181  waiting_list[0] = source 
3148  3182  waiting_beginning = 0 
3149  3183  waiting_end = 0 
3150  3184  
 3185  # For as long as there are vertices left to explore 
3151  3186  while waiting_beginning <= waiting_end: 
 3187  
 3188  # We pick the first one 
3152  3189  v = waiting_list[waiting_beginning] 
3153   o_n_size = cg.out_degrees[v] 
3154   outneighbors = <int *>sage_malloc(o_n_size * sizeof(int)) 
3155   o_n_size = cg.out_neighbors_unsafe(v, outneighbors, o_n_size) 
3156   for 0 <= i < o_n_size: 
3157   u = outneighbors[i] 
 3190  
 3191  # Iterating over all the outneighbors u of v 
 3192  for 0 <= i < cg.out_degrees[v]: 
 3193  u = p_vertices[v][i] 
 3194  
 3195  # If we notice one of these neighbors is not seen yet, we set 
 3196  # its parameters and add it to the queue to be explored later. 
3158  3197  if not bitset_in(seen, u): 
3159  3198  v_distances[u] = v_distances[v]+1 
3160  3199  v_prec[u] = v 
3161  3200  bitset_add(seen, u) 
3162  3201  waiting_end += 1 
3163  3202  waiting_list[waiting_end] = u 
3164   sage_free(outneighbors) 
 3203  
3165  3204  waiting_beginning += 1 
3166  3205  
 3206  # Copying the information to dictionaries (very costly, comparatively) 
 3207  # to be returned later 
3167  3208  tmp_distances = dict() 
3168  3209  tmp_prec = dict() 
3169  3210  for v in vertices: 
… 
… 

3185  3226  sage_free(waiting_list) 
3186  3227  sage_free(v_distances) 
3187  3228  sage_free(v_prec) 
 3229  sage_free(p_vertices) 
 3230  sage_free(p_edges) 
3188  3231  
3189  3232  return d_distances, d_prec 
3190  3233  