Skip to content

Commit 56edd95

Browse files
committed
avoid conversion in lex_M_fast
1 parent c9dd1e8 commit 56edd95

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

src/sage/graphs/traversals.pyx

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ from libcpp.vector cimport vector
6969
from cysignals.signals cimport sig_on, sig_off
7070
from memory_allocator cimport MemoryAllocator
7171

72+
from sage.graphs.base.c_graph cimport CGraph, CGraphBackend
73+
from sage.graphs.base.static_sparse_backend cimport StaticSparseCGraph
74+
from sage.graphs.base.static_sparse_backend cimport StaticSparseBackend
7275
from sage.graphs.base.static_sparse_graph cimport init_short_digraph
7376
from sage.graphs.base.static_sparse_graph cimport free_short_digraph
7477
from sage.graphs.base.static_sparse_graph cimport out_degree
75-
from sage.graphs.base.c_graph cimport CGraph, CGraphBackend
7678
from sage.graphs.graph_decompositions.slice_decomposition cimport \
7779
extended_lex_BFS
7880

@@ -753,8 +755,7 @@ def lex_M(self, triangulation=False, labels=False, initial_vertex=None, algorith
753755
- ``labels`` -- boolean (default: ``False``); whether to return the labels
754756
assigned to each vertex
755757
756-
- ``initial_vertex`` -- (default: ``None``) the first vertex to
757-
consider
758+
- ``initial_vertex`` -- (default: ``None``); the first vertex to consider
758759
759760
- ``algorithm`` -- string (default: ``None``); one of the following
760761
algorithms:
@@ -820,6 +821,18 @@ def lex_M(self, triangulation=False, labels=False, initial_vertex=None, algorith
820821
sage: g.lex_M()
821822
[6, 4, 5, 3, 2, 1]
822823
824+
The ordering depends on the initial vertex::
825+
826+
sage: G = graphs.HouseGraph()
827+
sage: G.lex_M(algorithm='lex_M_slow', initial_vertex=0)
828+
[4, 3, 2, 1, 0]
829+
sage: G.lex_M(algorithm='lex_M_slow', initial_vertex=2)
830+
[1, 4, 3, 0, 2]
831+
sage: G.lex_M(algorithm='lex_M_fast', initial_vertex=0)
832+
[4, 3, 2, 1, 0]
833+
sage: G.lex_M(algorithm='lex_M_fast', initial_vertex=2)
834+
[1, 4, 3, 0, 2]
835+
823836
TESTS:
824837
825838
``'lex_M_fast'`` cannot return labels::
@@ -1127,6 +1140,18 @@ def lex_M_fast(G, triangulation=False, initial_vertex=None):
11271140
Traceback (most recent call last):
11281141
...
11291142
ValueError: 'foo' is not a graph vertex
1143+
1144+
Immutable graphs::
1145+
1146+
sage: from sage.graphs.traversals import lex_M_fast
1147+
sage: G = graphs.RandomGNP(10, .7)
1148+
sage: G._backend
1149+
<sage.graphs.base.sparse_graph.SparseGraphBackend ...>
1150+
sage: H = Graph(G, immutable=True)
1151+
sage: H._backend
1152+
<sage.graphs.base.static_sparse_backend.StaticSparseBackend ...>
1153+
sage: lex_M_fast(G) == lex_M_fast(H)
1154+
True
11301155
"""
11311156
if initial_vertex is not None and initial_vertex not in G:
11321157
raise ValueError("'{}' is not a graph vertex".format(initial_vertex))
@@ -1136,23 +1161,31 @@ def lex_M_fast(G, triangulation=False, initial_vertex=None):
11361161

11371162
# ==> Initialization
11381163

1139-
cdef list int_to_v = list(G)
11401164
cdef int i, j, k, v, w, z
11411165

1142-
if initial_vertex is not None:
1143-
# We put the initial vertex at first place in the ordering
1144-
i = int_to_v.index(initial_vertex)
1145-
int_to_v[0], int_to_v[i] = int_to_v[i], int_to_v[0]
1146-
1166+
cdef list int_to_v
1167+
cdef StaticSparseCGraph cg
11471168
cdef short_digraph sd
1148-
init_short_digraph(sd, G, edge_labelled=False, vertex_list=int_to_v)
1169+
if isinstance(G, StaticSparseBackend):
1170+
cg = <StaticSparseCGraph> G._cg
1171+
sd = <short_digraph> cg.g
1172+
int_to_v = cg._vertex_to_labels
1173+
else:
1174+
int_to_v = list(G)
1175+
init_short_digraph(sd, G, edge_labelled=False, vertex_list=int_to_v)
1176+
11491177
cdef uint32_t* p_tmp
11501178
cdef uint32_t* p_end
11511179

11521180
cdef int n = G.order()
11531181

11541182
cdef list unnumbered_vertices = list(range(n))
11551183

1184+
if initial_vertex is not None:
1185+
# We put the initial vertex at the first place
1186+
i = int_to_v.index(initial_vertex)
1187+
unnumbered_vertices[0], unnumbered_vertices[i] = unnumbered_vertices[i], unnumbered_vertices[0]
1188+
11561189
cdef MemoryAllocator mem = MemoryAllocator()
11571190
cdef int* label = <int*>mem.allocarray(n, sizeof(int))
11581191
cdef int* alpha = <int*>mem.allocarray(n, sizeof(int))
@@ -1237,7 +1270,8 @@ def lex_M_fast(G, triangulation=False, initial_vertex=None):
12371270
k += 2
12381271
label[w] = k
12391272

1240-
free_short_digraph(sd)
1273+
if not isinstance(G, StaticSparseBackend):
1274+
free_short_digraph(sd)
12411275

12421276
cdef list ordering = [int_to_v[alpha[i]] for i in range(n)]
12431277

0 commit comments

Comments
 (0)