Ticket #11053: trac_11053.patch

File trac_11053.patch, 3.5 KB (added by ncohen, 10 years ago)
  • sage/graphs/base/c_graph.pyx

    # 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  
    31383138    cdef int *outneighbors
    31393139    cdef int o_n_size
    31403140    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
    31413170    for source in vertices:
     3171
     3172        # The source is seen
    31423173        bitset_set_first_n(seen, 0)
    31433174        bitset_add(seen, source)
    31443175
     3176        # Its parameters can already be set
    31453177        v_distances[source] = 0
    31463178        v_prec[source] = source
     3179
     3180        # and added to the queue
    31473181        waiting_list[0] = source
    31483182        waiting_beginning = 0
    31493183        waiting_end = 0
    31503184
     3185        # For as long as there are vertices left to explore
    31513186        while waiting_beginning <= waiting_end:
     3187
     3188            # We pick the first one
    31523189            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.
    31583197                if not bitset_in(seen, u):
    31593198                    v_distances[u] = v_distances[v]+1
    31603199                    v_prec[u] = v
    31613200                    bitset_add(seen, u)
    31623201                    waiting_end += 1
    31633202                    waiting_list[waiting_end] = u
    3164             sage_free(outneighbors)
     3203
    31653204            waiting_beginning += 1
    31663205
     3206        # Copying the information to dictionaries (very costly, comparatively)
     3207        # to be returned later
    31673208        tmp_distances = dict()
    31683209        tmp_prec = dict()
    31693210        for v in vertices:
     
    31853226    sage_free(waiting_list)
    31863227    sage_free(v_distances)
    31873228    sage_free(v_prec)
     3229    sage_free(p_vertices)
     3230    sage_free(p_edges)
    31883231
    31893232    return d_distances, d_prec
    31903233