Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/graphs/cliquer.pyx
4079 views
1
r"""
2
Cliquer: routines for finding cliques in graphs
3
4
This module defines functions based on Cliquer, an exact
5
branch-and-bound algorithm developed by Patric R. J. Ostergard and
6
written by Sampo Niskanen.
7
8
AUTHORS:
9
10
- Nathann Cohen (2009-08-14): Initial version
11
12
- Jeroen Demeyer (2011-05-06): Make cliquer interruptible (#11252)
13
14
Methods
15
-------
16
"""
17
18
#*****************************************************************************
19
# Copyright (C) 2009 Nathann Cohen <[email protected]>
20
#
21
# Distributed under the terms of the GNU General Public License (GPL)
22
# as published by the Free Software Foundation; either version 2 of
23
# the License, or (at your option) any later version.
24
# http://www.gnu.org/licenses/
25
#*****************************************************************************
26
27
28
include "../ext/interrupt.pxi"
29
30
31
def max_clique(graph):
32
"""
33
Returns the vertex set of a maximum complete subgraph.
34
35
Currently only implemented for undirected graphs. Use
36
to_undirected to convert a digraph to an undirected graph.
37
38
EXAMPLES::
39
40
sage: C=graphs.PetersenGraph()
41
sage: max_clique(C)
42
[7, 9]
43
44
TEST::
45
46
sage: g = Graph()
47
sage: g.clique_maximum()
48
[]
49
"""
50
if graph.order() == 0:
51
return graph.vertices()
52
53
graph,d = graph.relabel(inplace=False, return_map=True)
54
d_inv = {}
55
for v in d:
56
d_inv[d[v]] = v
57
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+1,v+1)
66
parse_input(buf,g)
67
68
cdef int* list
69
cdef int size
70
sig_on()
71
size = sage_clique_max(g, &list)
72
sig_off()
73
b = []
74
cdef int i
75
for i in range(size):
76
b.append(list[i])
77
return list_composition(b,d_inv)
78
79
80
# computes all the maximum clique of a graph and return its list
81
82
def all_max_clique(graph):
83
"""
84
Returns the vertex sets of *ALL* the maximum complete subgraphs.
85
86
Currently only implemented for undirected graphs. Use
87
to_undirected to convert a digraph to an undirected graph.
88
89
EXAMPLES::
90
91
sage: C=graphs.PetersenGraph()
92
sage: all_max_clique(C)
93
[[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]]
94
"""
95
96
graph,d = graph.relabel(inplace=False, return_map=True)
97
d_inv = {}
98
for v in d:
99
d_inv[d[v]] = v
100
101
cdef graph_t *g
102
g=graph_new(graph.order())
103
buf="p edges %d %d" %(graph.order(),graph.size())
104
parse_input(buf,g)
105
106
for e in graph.edge_iterator():
107
(u,v,w)=e
108
buf=' e %d %d' %(u+1,v+1)
109
parse_input(buf,g)
110
111
cdef int* list
112
cdef int size
113
sig_on()
114
size = sage_all_clique_max(g, &list)
115
sig_off()
116
b = []
117
c=[]
118
cdef int i
119
for i in range(size):
120
if(list[i]!=-1):
121
c.append(list[i])
122
else:
123
b.append(list_composition(c,d_inv))
124
c=[]
125
return b
126
127
128
#computes the clique number of a graph
129
130
def clique_number(graph):
131
"""
132
Returns the size of the largest clique of the graph (clique
133
number).
134
135
Currently only implemented for undirected graphs. Use
136
to_undirected to convert a digraph to an undirected graph.
137
138
EXAMPLES::
139
140
sage: C = Graph('DJ{')
141
sage: clique_number(C)
142
4
143
sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]})
144
sage: G.show(figsize=[2,2])
145
sage: clique_number(G)
146
3
147
"""
148
graph=graph.relabel(inplace=False)
149
cdef graph_t *g
150
g=graph_new(graph.order())
151
buf="p edges %d %d" %(graph.order(),graph.size())
152
parse_input(buf,g)
153
154
for e in graph.edge_iterator():
155
(u,v,w)=e
156
buf=' e %d %d' %(u+1,v+1)
157
parse_input(buf,g)
158
159
cdef int c
160
sig_on()
161
c = sage_clique_number(g)
162
sig_off()
163
return c
164
165
166
def list_composition(a,b):
167
"""
168
Composes a list ``a`` with a map ``b``.
169
170
EXAMPLES::
171
172
sage: from sage.graphs.cliquer import list_composition
173
sage: list_composition([1,3,'a'], {'a':'b', 1:2, 2:3, 3:4})
174
[2, 4, 'b']
175
176
"""
177
value=[]
178
for i in a:
179
value.append(b[i])
180
return value
181
182
183
184