cuthill_mckee_ordering
Reduces the bandwidth of a graph by reordering the indices assigned to each vertex using the Cuthill-McKee algorithm.
Complexity: O(m log(m)|V|) where m = max { degree(v) | v in V }
Defined in: <boost/graph/cuthill_mckee_ordering.hpp>
Example
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/cuthill_mckee_ordering.hpp>
int main() {
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;
Graph g{6};
boost::add_edge(0, 3, g);
boost::add_edge(0, 5, g);
boost::add_edge(1, 2, g);
boost::add_edge(1, 4, g);
boost::add_edge(2, 5, g);
boost::add_edge(3, 4, g);
using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
std::vector<Vertex> order;
boost::cuthill_mckee_ordering(g, std::back_inserter(order));
std::cout << "Cuthill-McKee ordering:";
for (auto v : order) {
std::cout << " " << v;
}
std::cout << "\n";
}
Cuthill-McKee ordering: 1 2 4 5 3 0
(1) Single starting vertex
template <class IncidenceGraph, class OutputIterator,
class ColorMap, class DegreeMap>
OutputIterator cuthill_mckee_ordering(const IncidenceGraph& g,
typename graph_traits<IncidenceGraph>::vertex_descriptor s,
OutputIterator inverse_permutation,
ColorMap color, DegreeMap degree)
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Incidence Graph. |
IN |
|
The starting vertex. |
OUT |
|
The new vertex ordering. The vertices are written to the output iterator in their new order. |
UTIL |
|
Used internally to keep track of the progress of the algorithm (to avoid visiting the same vertex twice). |
IN |
|
This must map vertices to their degree. |
(2) Automatic starting vertex selection
template <class VertexListGraph, class OutputIterator>
OutputIterator cuthill_mckee_ordering(const VertexListGraph& g,
OutputIterator inverse_permutation);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Vertex List Graph and Incidence Graph. |
OUT |
|
The new vertex ordering. The vertices are written to the output iterator in their new order. |
template <class VertexListGraph, class OutputIterator,
class VertexIndexMap>
OutputIterator cuthill_mckee_ordering(const VertexListGraph& g,
OutputIterator inverse_permutation,
VertexIndexMap index_map);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Vertex List Graph and Incidence Graph. |
OUT |
|
The new vertex ordering. The vertices are written to the output iterator in their new order. |
IN |
|
Maps each vertex to an integer in the range |
template <class VertexListGraph, class OutputIterator,
class ColorMap, class DegreeMap>
OutputIterator cuthill_mckee_ordering(const VertexListGraph& g,
OutputIterator inverse_permutation,
ColorMap color, DegreeMap degree)
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Vertex List Graph and Incidence Graph. |
OUT |
|
The new vertex ordering. The vertices are written to the output iterator in their new order. |
UTIL |
|
Used internally to keep track of the progress of the algorithm (to avoid visiting the same vertex twice). |
IN |
|
This must map vertices to their degree. |
(3) Explicit starting vertices via deque
template <class IncidenceGraph, class OutputIterator,
class ColorMap, class DegreeMap>
OutputIterator cuthill_mckee_ordering(const IncidenceGraph& g,
std::deque<typename graph_traits<IncidenceGraph>::vertex_descriptor> vertex_queue,
OutputIterator inverse_permutation,
ColorMap color, DegreeMap degree)
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Incidence Graph. |
IN |
|
The deque containing the starting vertices for each component. |
OUT |
|
The new vertex ordering. The vertices are written to the output iterator in their new order. |
UTIL |
|
Used internally to keep track of the progress of the algorithm (to avoid visiting the same vertex twice). |
IN |
|
This must map vertices to their degree. |
Description
The goal of the Cuthill-McKee (and reverse Cuthill-McKee) ordering algorithm [11, 38, 39, 40] is to reduce the bandwidth of a graph by reordering the indices assigned to each vertex. The Cuthill-McKee ordering algorithm works by a local minimization of the i-th bandwidths. The vertices are basically assigned a breadth-first search order, except that at each step, the adjacent vertices are placed in the queue in order of increasing degree.
Version 1 of the algorithm lets the user choose the "starting vertex",
version 2 finds a good starting vertex using the pseudo-peripheral pair
heuristic (among each component), while version 3 contains the starting
nodes for each vertex in the deque. The choice of the "starting vertex"
can have a significant effect on the quality of the ordering. For
versions 2 and 3, find_starting_vertex will be called for each
component in the graph, increasing run time significantly.
The output of the algorithm are the vertices in the new ordering. Depending on what kind of output iterator you use, you can get either the Cuthill-McKee ordering or the reverse Cuthill-McKee ordering. For example, if you store the output into a vector using the vector’s reverse iterator, then you get the reverse Cuthill-McKee ordering.
std::vector<vertex_descriptor> inv_perm(num_vertices(G));
cuthill_mckee_ordering(G, inv_perm.rbegin(), ...);
Either way, storing the output into a vector gives you the permutation from the new ordering to the old ordering.
inv_perm[new_index[u]] == u
Often times, it is the opposite permutation that you want, the permutation from the old index to the new index. This can easily be computed in the following way.
for (size_type i = 0; i != inv_perm.size(); ++i)
perm[old_index[inv_perm[i]]] = i;