Ticket #5793: cliquer-4.patch

File cliquer-4.patch, 7.0 KB (added by ncohen, 12 years ago)
  • sage/graphs/cliquer.pxd

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1248164118 -7200
    # Node ID b302bba958c131e1bc1723a6af18d4388fe17cc0
    # Parent  20f9a6be873e92343d7d6c502a6a5bf709590e84
    - Cliquer now deals with graphs with vertex labels not in 0..n-1
    - Some functions of Graph could use cliquer and now can. ( some functions dealing with MAXIMAL cliques instead of MAXIMUM cliques can not use cliquer, though ! )
    - Some documentations have been rearranged
    - Cliquer's graph_print function is added to cliquer.pxd if needed
    
    diff -r 20f9a6be873e -r b302bba958c1 sage/graphs/cliquer.pxd
    a b  
    2323
    2424cdef extern from "cliquer/graph.h":
    2525     cdef graph_t * graph_new(int n)
     26     cdef void graph_print(graph_t *g)
    2627
  • sage/graphs/cliquer.pyx

    diff -r 20f9a6be873e -r b302bba958c1 sage/graphs/cliquer.pyx
    a b  
    77   
    88    EXAMPLES::
    99   
    10     sage: C=graphs.PetersenGraph()
    11     sage: max_clique(C)
    12     [7, 9]
     10          sage: C=graphs.PetersenGraph()
     11          sage: max_clique(C)
     12          [7, 9]
    1313    """
     14    graph,d = graph.relabel(inplace=False, return_map=True)
     15    d_inv = {}
     16    for v in d:
     17        d_inv[d[v]] = v
     18
    1419    cdef graph_t *g
    1520    g=graph_new(graph.order())
    1621    buf="p edges %d %d" %(graph.order(),graph.size())
     
    2833    cdef int i
    2934    for i in range(size):
    3035        b.append(list[i])
    31     return b
     36    return list_composition(b,d_inv)
    3237
    3338
    3439# computes all the maximum clique of a graph and return its list
     
    4247   
    4348    EXAMPLES::
    4449   
    45     sage: C=graphs.PetersenGraph()
    46     sage: all_max_clique(C)
    47     [[2, 7], [7, 9], [6, 8], [6, 9], [0, 4], [4, 9], [5, 7], [0, 5], [5, 8], [3, 4], [2, 3], [3, 8], [1, 6], [0, 1], [1, 2]]
     50          sage: C=graphs.PetersenGraph()
     51          sage: all_max_clique(C)
     52          [[2, 7], [7, 9], [6, 8], [6, 9], [0, 4], [4, 9], [5, 7], [0, 5], [5, 8], [3, 4], [2, 3], [3, 8], [1, 6], [0, 1], [1, 2]]
    4853    """
     54
     55    graph,d = graph.relabel(inplace=False, return_map=True)
     56    d_inv = {}
     57    for v in d:
     58        d_inv[d[v]] = v
     59
    4960    cdef graph_t *g
    5061    g=graph_new(graph.order())
    5162    buf="p edges %d %d" %(graph.order(),graph.size())
     
    6677        if(list[i]!=-1):
    6778                c.append(list[i])
    6879        else:
    69                 b.append(c)
     80                b.append(list_composition(c,d_inv))
    7081                c=[]
    7182    return b
    7283
     
    91102    sage: clique_number(G)
    92103    3
    93104    """
     105    graph=graph.relabel(inplace=False)
    94106    cdef graph_t *g
    95107    g=graph_new(graph.order())
    96108    buf="p edges %d %d" %(graph.order(),graph.size())
     
    103115    return sage_clique_number(g)
    104116
    105117
     118def list_composition(a,b):
     119    value=[]
     120    for i in a:
     121        value.append(b[i])
     122    return value
     123   
    106124
    107 
  • sage/graphs/graph.py

    diff -r 20f9a6be873e -r b302bba958c1 sage/graphs/graph.py
    a b  
    92469246        ( algorithm="cliquer" ) but you can also chose the networkx
    92479247        algorithm instead ( algorithm="networkx" )
    92489248
    9249         The parameter 'cliques' is an optional list of cliques that can be input if already computed.
    9250         ONLY USED BY NetworkX !!!
     9249        INPUT :
     9250
     9251        - ``cliques'' : This parameter is an optional list of cliques that can be input if already computed.         
     9252                        ONLY USED BY NetworkX !!!
    92519253       
    92529254        Currently only implemented for undirected graphs. Use
    92539255        to_undirected to convert a digraph to an undirected graph.
     
    92839285       
    92849286        sage: C=graphs.PetersenGraph()
    92859287        sage: C.maximum_clique()
    9286         [6, 8]
     9288        [7, 9]
    92879289        """
    92889290        from sage.graphs.cliquer import max_clique
    92899291        return max_clique(self)
     
    93169318       
    93179319        sage: C=graphs.PetersenGraph()
    93189320        sage: C.maximum_cliques()
    9319         [[1, 6], [6, 8], [5, 7], [5, 8], [3, 8], [4, 6], [4, 7], [2, 3], [1, 2], [2, 7], [0, 5], [0, 1]]
     9321        [[2, 7], [7, 9], [6, 8], [6, 9], [0, 4], [4, 9], [5, 7], [0, 5], [5, 8], [3, 4], [2, 3], [3, 8], [1, 6], [0, 1], [1, 2]]
    93209322        """
    93219323
    93229324        from sage.graphs.cliquer import all_max_clique
    93239325        return all_max_clique(self)
    93249326
    9325     def cliques_vertex_clique_number(self, vertices=None, with_labels=False, cliques=None):
     9327    def cliques_vertex_clique_number(self, algorithm="cliquer",vertices=None, with_labels=False, cliques=None):
    93269328        r"""
    93279329        Returns a list of sizes of the largest maximal cliques containing
    93289330        each vertex. (Returns a single value if only one input vertex).
     
    93329334       
    93339335        INPUT:
    93349336       
    9335        
     9337        -  ``algorithm'' - "cliquer" or "networkx"
     9338
    93369339        -  ``vertices`` - the vertices to inspect (default is
    93379340           entire graph)
     9341                        ONLY USED BY NetworkX !!!           
    93389342       
    93399343        -  ``with_labels`` - (boolean) default False returns
    93409344           list as above True returns a dictionary keyed by vertex labels
     9345                        ONLY USED BY NetworkX !!!
    93419346       
    93429347        -  ``cliques`` - list of cliques (if already
    93439348           computed)
    9344        
     9349                        ONLY USED BY NetworkX !!!       
    93459350       
    93469351        EXAMPLES::
    93479352       
     
    93519356            sage: E = C.cliques()
    93529357            sage: E
    93539358            [[4, 1, 2, 3], [4, 0]]
    9354             sage: C.cliques_vertex_clique_number(cliques=E)
     9359            sage: C.cliques_vertex_clique_number(cliques=E,algorithm="networkx")
    93559360            [2, 4, 4, 4, 4]
    93569361            sage: F = graphs.Grid2dGraph(2,3)
    9357             sage: F.cliques_vertex_clique_number(with_labels=True)
     9362            sage: F.cliques_vertex_clique_number(with_labels=True,algorithm="networkx")
    93589363            {(0, 1): 2, (1, 2): 2, (0, 0): 2, (1, 1): 2, (1, 0): 2, (0, 2): 2}
    93599364            sage: F.cliques_vertex_clique_number(vertices=[(0, 1), (1, 2)])
    93609365            [2, 2]
     
    93639368            sage: G.cliques_vertex_clique_number()
    93649369            [3, 3, 3, 3]
    93659370        """
    9366         import networkx.cliques
    9367         return networkx.cliques.node_clique_number(self.networkx_graph(copy=False), vertices, with_labels, cliques)
     9371
     9372        if algorithm=="cliquer":
     9373            from sage.graphs.cliquer import clique_number
     9374            if vertices==None:
     9375                vertices=self
     9376            value=[]
     9377            for v in vertices:
     9378                value.append(1+clique_number(self.subgraph(self.neighbors(v))))
     9379                self.subgraph(self.neighbors(v)).plot()
     9380            return value
     9381        elif algorithm=="networkx":
     9382            import networkx.cliques
     9383            return networkx.cliques.node_clique_number(self.networkx_graph(copy=False), vertices, with_labels, cliques)
     9384        else:
     9385            raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.")
     9386
    93689387       
    93699388    def cliques_number_of(self, vertices=None, cliques=None, with_labels=False):
    93709389        """