Skip to content

Commit 438865c

Browse files
committed
avoid conversion in maximum_cardinality_search_M
1 parent d05de5b commit 438865c

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

src/sage/graphs/traversals.pyx

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,16 +1808,18 @@ def maximum_cardinality_search_M(G, initial_vertex=None):
18081808
Traceback (most recent call last):
18091809
...
18101810
ValueError: vertex (17) is not a vertex of the graph
1811-
"""
1812-
cdef list int_to_vertex = list(G)
18131811
1814-
if initial_vertex is None:
1815-
initial_vertex = 0
1816-
elif initial_vertex in G:
1817-
initial_vertex = int_to_vertex.index(initial_vertex)
1818-
else:
1819-
raise ValueError("vertex ({0}) is not a vertex of the graph".format(initial_vertex))
1812+
Immutable graphs::
18201813
1814+
sage: G = graphs.RandomGNP(10, .7)
1815+
sage: G._backend
1816+
<sage.graphs.base.sparse_graph.SparseGraphBackend ...>
1817+
sage: H = Graph(G, immutable=True)
1818+
sage: H._backend
1819+
<sage.graphs.base.static_sparse_backend.StaticSparseBackend ...>
1820+
sage: G.maximum_cardinality_search_M() == H.maximum_cardinality_search_M()
1821+
True
1822+
"""
18211823
cdef int N = G.order()
18221824
if not N:
18231825
return ([], [], [])
@@ -1827,8 +1829,26 @@ def maximum_cardinality_search_M(G, initial_vertex=None):
18271829
# Copying the whole graph to obtain the list of neighbors quicker than by
18281830
# calling out_neighbors. This data structure is well documented in the
18291831
# module sage.graphs.base.static_sparse_graph
1832+
cdef list int_to_vertex
1833+
cdef StaticSparseCGraph cg
18301834
cdef short_digraph sd
1831-
init_short_digraph(sd, G, edge_labelled=False, vertex_list=int_to_vertex)
1835+
if isinstance(G, StaticSparseBackend):
1836+
cg = <StaticSparseCGraph> G._cg
1837+
sd = <short_digraph> cg.g
1838+
int_to_vertex = cg._vertex_to_labels
1839+
else:
1840+
int_to_vertex = list(G)
1841+
init_short_digraph(sd, G, edge_labelled=False, vertex_list=int_to_vertex)
1842+
1843+
if initial_vertex is None:
1844+
initial_vertex = 0
1845+
elif initial_vertex in G:
1846+
if isinstance(G, StaticSparseBackend):
1847+
initial_vertex = cg._vertex_to_int[initial_vertex]
1848+
else:
1849+
initial_vertex = int_to_vertex.index(initial_vertex)
1850+
else:
1851+
raise ValueError("vertex ({0}) is not a vertex of the graph".format(initial_vertex))
18321852

18331853
cdef MemoryAllocator mem = MemoryAllocator()
18341854
cdef int* alpha = <int*>mem.calloc(N, sizeof(int))
@@ -1840,7 +1860,8 @@ def maximum_cardinality_search_M(G, initial_vertex=None):
18401860
maximum_cardinality_search_M_short_digraph(sd, initial_vertex, alpha, alpha_inv, F, X)
18411861
sig_off()
18421862

1843-
free_short_digraph(sd)
1863+
if not isinstance(G, StaticSparseBackend):
1864+
free_short_digraph(sd)
18441865

18451866
cdef int u, v
18461867
return ([int_to_vertex[alpha[u]] for u in range(N)],

0 commit comments

Comments
 (0)