Ticket #14619: 14619-rev.patch

File 14619-rev.patch, 6.3 KB (added by ncohen, 6 years ago)
  • sage/graphs/distances_all_pairs.pyx

    # 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 distance-regular -- reviewer's remarks
    
    diff --git a/sage/graphs/distances_all_pairs.pyx b/sage/graphs/distances_all_pairs.pyx
    a b  
    164164    # The list of waiting vertices, the beginning and the end of the list
    165165
    166166    cdef unsigned short * waiting_list = <unsigned short *> sage_malloc(n*sizeof(unsigned short))
    167     if waiting_list==NULL:
     167    if waiting_list == NULL:
    168168        raise MemoryError()
    169169    cdef unsigned short waiting_beginning = 0
    170170    cdef unsigned short waiting_end = 0
    171171
    172172    cdef int * degree = <int *> sage_malloc(n*sizeof(int))
    173     if degree==NULL:
     173    if degree == NULL:
    174174        sage_free(waiting_list)
    175175        raise MemoryError()
    176176
     
    193193        c_distances = distances
    194194    else:
    195195        c_distances = <unsigned short *> sage_malloc( n * sizeof(unsigned short))
    196         if c_distances==NULL:
     196        if c_distances == NULL:
    197197            sage_free(waiting_list)
    198198            sage_free(degree)
    199199            raise MemoryError()
     
    309309
    310310    cdef unsigned int n = G.order()
    311311    cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short))
    312     if distances==NULL:
     312    if distances == NULL:
    313313        raise MemoryError()
    314314    cdef unsigned short * predecessors = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short))
    315     if predecessors==NULL:
     315    if predecessors == NULL:
    316316        sage_free(distances)
    317317        raise MemoryError()
    318318    all_pairs_shortest_path_BFS(G, predecessors, distances, NULL)
     
    401401
    402402    cdef unsigned int n = G.order()
    403403    cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short))
    404     if distances==NULL:
     404    if distances == NULL:
    405405        raise MemoryError()
    406406    all_pairs_shortest_path_BFS(G, NULL, distances, NULL)
    407407
     
    468468    Tests if the graph is distance-regular
    469469
    470470    A graph `G` is distance-regular 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    strongly-regular graph is also distance-regular, and a distance-regular
     473    graph is necessarily regular too.
    472474
    473475    For more information on distance-regular graphs, see its associated
    474476    :wikipedia:`wikipedia page <Distance-regular_graph>`.
     
    480482      obtain `d_i` by doing ``dictionary.get(i,0)``). Set to ``False`` by
    481483      default.
    482484
     485    .. SEEALSO::
     486
     487        * :meth:`Graph.is_regular`
     488        * :meth:`Graph.is_strongly_regular`
     489
    483490    EXAMPLES::
    484491
    485492        sage: g = graphs.PetersenGraph()
     
    488495        sage: g.is_distance_regular(parameters = True)
    489496        {1: 3, 2: 6}
    490497
    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::
    492499
    493500        sage: graphs.CubeGraph(4).is_distance_regular(parameters = True)
    494501        {1: 4, 2: 6, 3: 4, 4: 1}
    495502
     503    TESTS::
     504
     505        sage: graphs.PathGraph(2).is_distance_regular(parameters = True)
     506        {1: 1}
     507
    496508    """
    497509    cdef int i,l
    498510    cdef int n = G.order()
    499511
    500     if n <= 2:
     512    if n <= 1:
    501513        return {} if parameters else True
    502514
    503515    if not G.is_regular():
     
    512524    cdef unsigned short * d_array = <unsigned short *> sage_calloc(2*n, sizeof(unsigned short))
    513525    cdef unsigned short * d_tmp   = d_array + n
    514526
    515     if d_array==NULL:
     527    if d_array == NULL:
    516528        sage_free(distance_matrix)
    517529        raise MemoryError()
    518530
    519531    # Filling d_array
    520532    cdef unsigned short * pointer = distance_matrix
    521533    for i in range(n):
    522         d_array[pointer[i]] += 1
     534        if pointer[i] < n:
     535            d_array[pointer[i]] += 1
    523536    pointer += n
    524537
    525538    # For each of the n-1 other vertices
     
    528541        # We set d_tmp and fill it with the data from the l^th row
    529542        memset(d_tmp, 0, n*sizeof(unsigned short))
    530543        for i in range(n):
    531             d_tmp[pointer[i]] += 1
     544            if pointer[i] < n:
     545                d_tmp[pointer[i]] += 1
    532546
    533547        # If d_tmp != d_array, we are done
    534548        if memcmp(d_array, d_tmp, n*sizeof(unsigned short)) != 0:
     
    607621        return {}, {}
    608622
    609623    cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short))
    610     if distances==NULL:
     624    if distances == NULL:
    611625        raise MemoryError()
    612626    cdef unsigned short * c_distances = distances
    613627    cdef unsigned short * predecessor = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short))
    614     if predecessor==NULL:
     628    if predecessor == NULL:
    615629        sage_free(distances)
    616630        raise MemoryError()
    617631    cdef unsigned short * c_predecessor = predecessor
     
    667681    cdef unsigned int n = G.order()
    668682
    669683    cdef unsigned short * ecc = <unsigned short *> sage_malloc(n*sizeof(unsigned short))
    670     if ecc==NULL:
     684    if ecc == NULL:
    671685        raise MemoryError()
    672686    cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short))
    673     if distances==NULL:
     687    if distances == NULL:
    674688        sage_free(ecc)
    675689        raise MemoryError()
    676690    all_pairs_shortest_path_BFS(G, NULL, distances, ecc)
     
    9921006
    9931007    # init dist
    9941008    t_dist = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short))
    995     if t_dist==NULL:
     1009    if t_dist == NULL:
    9961010        raise MemoryError()
    9971011    dist = <unsigned short **> sage_malloc(n*sizeof(unsigned short *))
    998     if dist==NULL:
     1012    if dist == NULL:
    9991013        sage_free(t_dist)
    10001014        raise MemoryError()
    10011015    dist[0] = t_dist
     
    10111025    if paths:
    10121026        # init prec
    10131027        t_prec = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short))
    1014         if t_prec==NULL:
     1028        if t_prec == NULL:
    10151029            sage_free(t_dist)
    10161030            sage_free(dist)
    10171031            raise MemoryError()
    10181032        prec = <unsigned short **> sage_malloc(n*sizeof(unsigned short *))
    1019         if prec==NULL:
     1033        if prec == NULL:
    10201034            sage_free(t_dist)
    10211035            sage_free(dist)
    10221036            sage_free(t_prec)