Ticket #14619: 14619.patch

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

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1369056422 -7200
    #      Mon May 20 15:27:02 2013 +0200
    # Node ID aeeddd6d16575acdd54eadb3b57fe9b070197e60
    # Parent  d0943a747f6dd975386c499f666609c9b1e3d8c5
    Test that a graph is distance-regular
    
    diff --git a/sage/graphs/distances_all_pairs.pyx b/sage/graphs/distances_all_pairs.pyx
    a b  
    463463    sage_free(distances)
    464464    return d
    465465
     466def is_distance_regular(G, parameters = False):
     467    r"""
     468    Tests if the graph is distance-regular
     469
     470    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\}`.
     472
     473    For more information on distance-regular graphs, see its associated
     474    :wikipedia:`wikipedia page <Distance-regular_graph>`.
     475
     476    INPUT:
     477
     478    - ``parameters`` (boolean) -- whether to replace ``True`` answers with a
     479      dictionary associating `d_i` to an integer `i>0` if `d_i>0` (one can then
     480      obtain `d_i` by doing ``dictionary.get(i,0)``). Set to ``False`` by
     481      default.
     482
     483    EXAMPLES::
     484
     485        sage: g = graphs.PetersenGraph()
     486        sage: g.is_distance_regular()
     487        True
     488        sage: g.is_distance_regular(parameters = True)
     489        {1: 3, 2: 6}
     490
     491    Cube graphs, which are not strongly regular, are a bit more interesting;;
     492
     493        sage: graphs.CubeGraph(4).is_distance_regular(parameters = True)
     494        {1: 4, 2: 6, 3: 4, 4: 1}
     495
     496    """
     497    cdef int i,l
     498    cdef int n = G.order()
     499
     500    if n <= 2:
     501        return {} if parameters else True
     502
     503    if not G.is_regular():
     504        return False
     505
     506    cdef unsigned short * distance_matrix = c_distances_all_pairs(G)
     507
     508    # - d_array is the vector of d_i corresponding to the first vertex
     509    #
     510    # - d_tmp is a vector that we use to check that d_array is the same for
     511    #   every vertex v
     512    cdef unsigned short * d_array = <unsigned short *> sage_calloc(2*n, sizeof(unsigned short))
     513    cdef unsigned short * d_tmp   = d_array + n
     514
     515    if d_array==NULL:
     516        sage_free(distance_matrix)
     517        raise MemoryError()
     518
     519    # Filling d_array
     520    cdef unsigned short * pointer = distance_matrix
     521    for i in range(n):
     522        d_array[pointer[i]] += 1
     523    pointer += n
     524
     525    # For each of the n-1 other vertices
     526    for l in range(1,n):
     527
     528        # We set d_tmp and fill it with the data from the l^th row
     529        memset(d_tmp, 0, n*sizeof(unsigned short))
     530        for i in range(n):
     531            d_tmp[pointer[i]] += 1
     532
     533        # If d_tmp != d_array, we are done
     534        if memcmp(d_array, d_tmp, n*sizeof(unsigned short)) != 0:
     535            sage_free(distance_matrix)
     536            sage_free(d_array)
     537            return False
     538
     539        pointer += n
     540
     541    cdef dict dict_parameters
     542    if parameters:
     543        dict_parameters = {i:d_array[i] for i in range(n) if i and d_array[i] > 0}
     544
     545    sage_free(distance_matrix)
     546    sage_free(d_array)
     547
     548    if parameters:
     549        return dict_parameters
     550    else:
     551        return True
     552
    466553###################################
    467554# Both distances and predecessors #
    468555###################################
  • sage/graphs/graph.py

    diff --git a/sage/graphs/graph.py b/sage/graphs/graph.py
    a b  
    5353    :meth:`~Graph.is_long_antihole_free` | Tests whether ``self`` contains an induced anticycle of length at least 5.
    5454    :meth:`~Graph.is_weakly_chordal` | Tests whether ``self`` is weakly chordal.
    5555    :meth:`~Graph.is_strongly_regular` | Tests whether ``self`` is strongly regular.
     56    :meth:`~Graph.is_distance_regular` | Tests whether ``self`` is distance-regular.
    5657    :meth:`~Graph.is_tree` | Return True if the graph is a tree.
    5758    :meth:`~Graph.is_forest` | Return True if the graph is a forest, i.e. a disjoint union of trees.
    5859    :meth:`~Graph.is_overfull` | Tests whether the current graph is overfull.
     
    61926193import sage.graphs.graph_decompositions.graph_products
    61936194Graph.is_cartesian_product = types.MethodType(sage.graphs.graph_decompositions.graph_products.is_cartesian_product, None, Graph)
    61946195
     6196import sage.graphs.distances_all_pairs
     6197Graph.is_distance_regular = types.MethodType(sage.graphs.distances_all_pairs.is_distance_regular, None, Graph)
     6198
    61956199# From Python modules
    61966200import sage.graphs.line_graph
    61976201Graph.is_line_graph = sage.graphs.line_graph.is_line_graph