Skip to content

Commit 02fee8f

Browse files
committed
avoid conversion in maximum_cardinality_search
1 parent 56edd95 commit 02fee8f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/sage/graphs/traversals.pyx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,17 +1432,27 @@ def maximum_cardinality_search(G, reverse=False, tree=False, initial_vertex=None
14321432
if N == 1:
14331433
return (list(G), DiGraph(G)) if tree else list(G)
14341434

1435-
cdef list int_to_vertex = list(G)
1435+
cdef list int_to_vertex
1436+
cdef StaticSparseCGraph cg
1437+
cdef short_digraph sd
1438+
if isinstance(G, StaticSparseBackend):
1439+
cg = <StaticSparseCGraph> G._cg
1440+
sd = <short_digraph> cg.g
1441+
int_to_vertex = cg._vertex_to_labels
1442+
else:
1443+
int_to_vertex = list(G)
1444+
init_short_digraph(sd, G, edge_labelled=False, vertex_list=int_to_vertex)
14361445

14371446
if initial_vertex is None:
14381447
initial_vertex = 0
14391448
elif initial_vertex in G:
1440-
initial_vertex = int_to_vertex.index(initial_vertex)
1449+
if isinstance(G, StaticSparseBackend):
1450+
initial_vertex = cg._vertex_to_int[initial_vertex]
1451+
else:
1452+
initial_vertex = int_to_vertex.index(initial_vertex)
14411453
else:
14421454
raise ValueError("vertex ({0}) is not a vertex of the graph".format(initial_vertex))
14431455

1444-
cdef short_digraph sd
1445-
init_short_digraph(sd, G, edge_labelled=False, vertex_list=int_to_vertex)
14461456
cdef uint32_t** p_vertices = sd.neighbors
14471457
cdef uint32_t* p_tmp
14481458
cdef uint32_t* p_end
@@ -1489,7 +1499,8 @@ def maximum_cardinality_search(G, reverse=False, tree=False, initial_vertex=None
14891499
pred[v] = u
14901500
p_tmp += 1
14911501

1492-
free_short_digraph(sd)
1502+
if not isinstance(G, StaticSparseBackend):
1503+
free_short_digraph(sd)
14931504

14941505
if len(alpha) < N:
14951506
raise ValueError("the input graph is not connected")

0 commit comments

Comments
 (0)