Skip to content

Commit d05de5b

Browse files
committed
use pairing heap in maximum_cardinality_search
1 parent 97b0d9b commit d05de5b

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

src/sage/graphs/traversals.pyx

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@ from collections import deque
6363

6464
from libc.string cimport memset
6565
from libc.stdint cimport uint32_t
66-
from libcpp.queue cimport priority_queue
67-
from libcpp.pair cimport pair
6866
from libcpp.vector cimport vector
6967
from cysignals.signals cimport sig_on, sig_off
7068
from memory_allocator cimport MemoryAllocator
7169

70+
from sage.data_structures.pairing_heap cimport PairingHeap_of_n_integers
7271
from sage.graphs.base.c_graph cimport CGraph, CGraphBackend
7372
from sage.graphs.base.static_sparse_backend cimport StaticSparseCGraph
7473
from sage.graphs.base.static_sparse_backend cimport StaticSparseBackend
@@ -1388,9 +1387,9 @@ def maximum_cardinality_search(G, reverse=False, tree=False, initial_vertex=None
13881387
sage: G.maximum_cardinality_search(initial_vertex=0)
13891388
[3, 2, 1, 0]
13901389
sage: G.maximum_cardinality_search(initial_vertex=1)
1391-
[0, 3, 2, 1]
1390+
[3, 2, 0, 1]
13921391
sage: G.maximum_cardinality_search(initial_vertex=2)
1393-
[0, 1, 3, 2]
1392+
[0, 3, 1, 2]
13941393
sage: G.maximum_cardinality_search(initial_vertex=3)
13951394
[0, 1, 2, 3]
13961395
sage: G.maximum_cardinality_search(initial_vertex=3, reverse=True)
@@ -1475,27 +1474,18 @@ def maximum_cardinality_search(G, reverse=False, tree=False, initial_vertex=None
14751474

14761475
cdef int i, u, v
14771476
for i in range(N):
1478-
weight[i] = 0
1479-
seen[i] = False
14801477
pred[i] = i
14811478

1482-
# We emulate a heap with decrease key operation using a priority queue.
1483-
# A vertex can be inserted multiple times (up to its degree), but only the
1484-
# first extraction (with maximum weight) matters. The size of the queue will
1485-
# never exceed O(m).
1486-
cdef priority_queue[pair[int, int]] pq
1487-
pq.push((0, initial_vertex))
1479+
# We emulate a max-heap data structure using a min-heap with negative values
1480+
cdef PairingHeap_of_n_integers P = PairingHeap_of_n_integers(N)
1481+
P.push(initial_vertex, 0)
14881482

14891483
# The ordering alpha is feed in reversed order and revert afterword
14901484
cdef list alpha = []
14911485

1492-
while not pq.empty():
1493-
_, u = pq.top()
1494-
pq.pop()
1495-
if seen[u]:
1496-
# We use a lazy decrease key mode, so u can be several times in pq
1497-
continue
1498-
1486+
while P:
1487+
u = P.top_item()
1488+
P.pop()
14991489
alpha.append(int_to_vertex[u])
15001490
seen[u] = True
15011491

@@ -1505,7 +1495,7 @@ def maximum_cardinality_search(G, reverse=False, tree=False, initial_vertex=None
15051495
v = p_tmp[0]
15061496
if not seen[v]:
15071497
weight[v] += 1
1508-
pq.push((weight[v], v))
1498+
P.decrease(v, -weight[v])
15091499
if pred[v] == v:
15101500
pred[v] = u
15111501
p_tmp += 1

0 commit comments

Comments
 (0)