@@ -515,7 +515,8 @@ cpdef bandwidth_heuristics(g, algorithm='cuthill_mckee') noexcept:
515
515
Unfortunately, exactly computing the bandwidth is NP-hard ( and an
516
516
exponential algorithm is implemented in Sagemath in routine
517
517
:func:`~sage. graphs. graph_decompositions. bandwidth. bandwidth`) . Here, we
518
- implement two heuristics to find good orderings: Cuthill-McKee, and King.
518
+ implement two heuristics to find good orderings: Cuthill-McKee, reverse
519
+ Cuthill-McKee ( also known as ``RCM``) and King.
519
520
520
521
This function works only in undirected graphs, and its running time is
521
522
`O( md_{max}\l og d_{max}) ` for the Cuthill-McKee ordering, and
@@ -527,7 +528,8 @@ cpdef bandwidth_heuristics(g, algorithm='cuthill_mckee') noexcept:
527
528
- ``g`` -- the input Sage graph
528
529
529
530
- ``algorithm`` -- string ( default: ``'cuthill_mckee'``) ; the heuristic used
530
- to compute the ordering among ``'cuthill_mckee'`` and ``'king'``
531
+ to compute the ordering among ``'cuthill_mckee'``,
532
+ ``'reverse_cuthill_mckee'`` and ``'king'``
531
533
532
534
OUTPUT:
533
535
@@ -542,6 +544,8 @@ cpdef bandwidth_heuristics(g, algorithm='cuthill_mckee') noexcept:
542
544
( 1, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ])
543
545
sage: bandwidth_heuristics( graphs. GridGraph( [3,3 ]))
544
546
( 3, [(0, 0), (1, 0), (0, 1), (2, 0), (1, 1), (0, 2), (2, 1), (1, 2), (2, 2) ])
547
+ sage: bandwidth_heuristics( graphs. GridGraph( [3,3 ]) , algorithm='reverse_cuthill_mckee')
548
+ ( 3, [(2, 2), (1, 2), (2, 1), (0, 2), (1, 1), (2, 0), (0, 1), (1, 0), (0, 0) ])
545
549
sage: bandwidth_heuristics( graphs. GridGraph( [3,3 ]) , algorithm='king')
546
550
( 3, [(0, 0), (1, 0), (0, 1), (2, 0), (1, 1), (0, 2), (2, 1), (1, 2), (2, 2) ])
547
551
@@ -581,11 +585,16 @@ cpdef bandwidth_heuristics(g, algorithm='cuthill_mckee') noexcept:
581
585
# Tests for errors and trivial cases
582
586
if not isinstance (g, Graph):
583
587
raise TypeError (" the input must be a Sage Graph" )
584
- if algorithm not in [' cuthill_mckee' , ' king' ]:
588
+ if algorithm not in [' cuthill_mckee' , ' reverse_cuthill_mckee ' , ' king' ]:
585
589
raise ValueError (f" unknown algorithm {algorithm!r}" )
586
590
if not g.num_edges():
587
591
return (0 , list (g))
588
592
593
+ cdef bint reverse = False
594
+ if algorithm == ' reverse_cuthill_mckee' :
595
+ reverse = True
596
+ algorithm = ' cuthill_mckee'
597
+
589
598
# These variables are automatically deleted when the function terminates.
590
599
cdef BoostVecGraph g_boost
591
600
cdef vector[v_index] result
@@ -601,8 +610,12 @@ cpdef bandwidth_heuristics(g, algorithm='cuthill_mckee') noexcept:
601
610
602
611
cdef int n = g.num_verts()
603
612
cdef dict pos = {int_to_vertex[< int > result[i]]: i for i in range (n)}
604
- cdef int bandwidth = max ([abs (pos[u] - pos[v]) for u, v in g.edge_iterator(labels = False )])
613
+ cdef int bandwidth = max ([abs (pos[u] - pos[v])
614
+ for u, v in g.edge_iterator(labels = False )])
605
615
616
+ if reverse:
617
+ return (bandwidth, [int_to_vertex[< int > result[i]]
618
+ for i in range (n - 1 , - 1 , - 1 )])
606
619
return (bandwidth, [int_to_vertex[< int > result[i]] for i in range (n)])
607
620
608
621
0 commit comments