Ticket #5793: cliquer.patch

File cliquer.patch, 7.6 KB (added by ncohen, 12 years ago)

Cliquer, from the beginning to the end, with the good directory's name !

  • module_list.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1247858635 -7200
    # Node ID 9faa9ef48dee6bbbedc7fc544df1dfdf63f53234
    # Parent  6c7354ace3b6e0bf4c33ddb730531bd23a9fb92c
    Cliquer for SAGE
    
    diff -r 6c7354ace3b6 -r 9faa9ef48dee module_list.py
    a b  
    222222    Extension('sage.graphs.chrompoly',
    223223              sources = ['sage/graphs/chrompoly.pyx']),
    224224
     225    Extension('sage.graphs.cliquer',
     226              sources = ['sage/graphs/cliquer.pyx'],
     227              libraries = ['cliquer']),
     228
    225229    Extension('sage.graphs.graph_fast',
    226230              sources = ['sage/graphs/graph_fast.pyx'],
    227231              libraries = ['gmp']),
  • new file sage/ext/cliquer.pxi

    diff -r 6c7354ace3b6 -r 9faa9ef48dee sage/ext/cliquer.pxi
    - +  
     1cdef extern from "cliquer/graph.h":
     2    struct graph_t:
     3       pass
     4
     5cdef extern from "cliquer/cliquer.h":
     6    struct clique_options:
     7       pass
     8
     9
     10cdef extern from "cliquer/reorder.h":
     11     cdef int *reorder_by_greedy_coloring(graph_t *g, bool weighted)
     12     cdef int *reorder_by_degree(graph_t *g, bool weighted)
     13
     14cdef extern from "cliquer/cliquer.h":
     15     bool clique_print_time(intlevel, int i, int n, int max, double cputime, double realtime, clique_options *opts)
     16
     17
     18cdef extern from "cliquer/cl.h":
     19     cdef int sage_clique_max(graph_t *g, int ** list)
     20     cdef int sage_all_clique_max(graph_t *g, int ** list)
     21     cdef int sage_clique_number(graph_t *g)
     22     cdef void parse_input(char *str,graph_t *g)
     23
     24cdef extern from "cliquer/graph.h":
     25     cdef graph_t * graph_new(int n)
     26
  • sage/graphs/all.py

    diff -r 6c7354ace3b6 -r 9faa9ef48dee sage/graphs/all.py
    a b  
    66import graph_list as graphs_list
    77import sage.graphs.graph_fast
    88import graph_coloring
    9 from graph_database import graph_db_info
    10  No newline at end of file
     9from sage.graphs.cliquer import *
     10from graph_database import graph_db_info
  • new file sage/graphs/cliquer.pyx

    diff -r 6c7354ace3b6 -r 9faa9ef48dee sage/graphs/cliquer.pyx
    - +  
     1include '../ext/cliquer.pxi'
     2
     3# computes the maximum clique of a graph and return the list of its members
     4
     5def max_clique(graph):
     6    cdef graph_t *g
     7    g=graph_new(graph.order())
     8    buf="p edges %d %d" %(graph.order(),graph.size())
     9    parse_input(buf,g)
     10
     11    for e in graph.edge_iterator():
     12          (u,v,w)=e
     13          buf=' e %d %d' %(u,v)
     14          parse_input(buf,g)
     15
     16
     17    cdef int* list
     18    cdef int size
     19    size=sage_clique_max(g, &list)
     20    b = []
     21    cdef int i
     22    for i in range(size):
     23        b.append(list[i])
     24    return b
     25
     26
     27# computes all the maximum clique of a graph and return its list
     28
     29def all_max_clique(graph):
     30    cdef graph_t *g
     31    g=graph_new(graph.order())
     32    buf="p edges %d %d" %(graph.order(),graph.size())
     33    parse_input(buf,g)
     34
     35    for e in graph.edge_iterator():
     36          (u,v,w)=e
     37          buf=' e %d %d' %(u,v)
     38          parse_input(buf,g)
     39
     40    cdef int* list
     41    cdef int size
     42    size=sage_all_clique_max(g, &list)
     43    b = []
     44    c=[]
     45    cdef int i
     46    for i in range(size):
     47        if(list[i]!=-1):
     48                c.append(list[i])
     49        else:
     50                b.append(c)
     51                c=[]
     52    return b
     53
     54
     55#computes the clique number of a graph
     56
     57def clique_number(graph):
     58    cdef graph_t *g
     59    g=graph_new(graph.order())
     60    buf="p edges %d %d" %(graph.order(),graph.size())
     61    parse_input(buf,g)
     62
     63    for e in graph.edge_iterator():
     64          (u,v,w)=e
     65          buf=' e %d %d' %(u,v)
     66          parse_input(buf,g)
     67    return sage_clique_number(g)
  • sage/graphs/graph.py

    diff -r 6c7354ace3b6 -r 9faa9ef48dee sage/graphs/graph.py
    a b  
    92389238        from bipartite_graph import BipartiteGraph
    92399239        return BipartiteGraph(networkx.cliques.make_clique_bipartite(self.networkx_graph(copy=False), **kwds))
    92409240
    9241     def clique_number(self, cliques=None):
     9241    def clique_number(self,algorithm="cliquer"):
    92429242        """
    92439243        Returns the size of the largest clique of the graph (clique
    9244         number).
     9244        number). By default, the Cliquer algorithm is used
     9245        ( algorithm="Cliquer" ) but you can also chose the networkx
     9246        algorithm instead ( algorithm="networkx" )
    92459247       
    92469248        Currently only implemented for undirected graphs. Use
    92479249        to_undirected to convert a digraph to an undirected graph.
    92489250       
    9249         INPUT:
    9250        
    9251        
    9252         -  ``cliques`` - list of cliques (if already
    9253            computed)
    9254        
    9255        
    92569251        EXAMPLES::
    92579252       
    92589253            sage: C = Graph('DJ{')
    92599254            sage: C.clique_number()
    92609255            4
    9261             sage: E = C.cliques()
    9262             sage: E
    9263             [[4, 1, 2, 3], [4, 0]]
    9264             sage: C.clique_number(cliques=E)
    9265             4
    92669256            sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]})
    92679257            sage: G.show(figsize=[2,2])
    92689258            sage: G.clique_number()
    92699259            3
    92709260        """
    9271         import networkx.cliques
    9272         return networkx.cliques.graph_clique_number(self.networkx_graph(copy=False), cliques)
    9273        
     9261        if algorithm=="networkx":
     9262            import networkx.cliques
     9263            return networkx.cliques.graph_clique_number(self.networkx_graph(copy=False), cliques)
     9264       
     9265        from sage.graphs.cliquer import clique_number
     9266        return clique_number(self)
     9267
     9268    def maximum_clique(self):
     9269        """
     9270        Returns the vertex set of a maximum complete subgraph.
     9271       
     9272        Currently only implemented for undirected graphs. Use
     9273        to_undirected to convert a digraph to an undirected graph.
     9274       
     9275        EXAMPLES::
     9276       
     9277        sage: C=graphs.PetersenGraph()
     9278        sage: C.maximum_cliq()
     9279        C.maximum_clique   C.maximum_cliques 
     9280        sage: C.maximum_clique()
     9281        [7, 9]
     9282        """
     9283        from sage.graphs.cliquer import max_clique
     9284        return max_clique(self)
     9285
     9286    def maximum_independent_set(self):
     9287        """
     9288        Returns the vertex set of a independent (stable) subgraph.
     9289       
     9290        Currently only implemented for undirected graphs. Use
     9291        to_undirected to convert a digraph to an undirected graph.
     9292       
     9293        EXAMPLES::
     9294       
     9295        sage: C=graphs.PetersenGraph()
     9296        sage: C.maximum_independent_set()
     9297        [0, 3, 6, 7]
     9298
     9299        """
     9300        from sage.graphs.cliquer import max_clique
     9301        return max_clique(self.complement())
     9302
     9303    def maximum_cliques(self):
     9304        """
     9305        Returns the vertex set of *ALL* the maximum complete subgraphs.
     9306       
     9307        Currently only implemented for undirected graphs. Use
     9308        to_undirected to convert a digraph to an undirected graph.
     9309       
     9310        EXAMPLES::
     9311       
     9312        sage: C=graphs.PetersenGraph()
     9313        sage: C.maximum_cliques()
     9314       
     9315        [[2, 7],
     9316        [7, 9],
     9317        [6, 8],
     9318        [6, 9],
     9319        [0, 4],
     9320        [4, 9],
     9321        [5, 7],
     9322        [0, 5],
     9323        [5, 8],
     9324        [3, 4],
     9325        [2, 3],
     9326        [3, 8],
     9327        [1, 6],
     9328        [0, 1],
     9329        [1, 2]]
     9330        """
     9331
     9332        from sage.graphs.cliquer import all_max_clique
     9333        return all_max_clique(self)
     9334
    92749335    def cliques_vertex_clique_number(self, vertices=None, with_labels=False, cliques=None):
    92759336        r"""
    92769337        Returns a list of sizes of the largest maximal cliques containing