Isomorphism
Determines whether an isomorphism exists between two graphs.
Complexity: O(|V|!)
Defined in: <boost/graph/isomorphism.hpp>
Example
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/isomorphism.hpp>
int main() {
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS>;
Graph g1, g2;
// Triangle: 0->1->2->0
boost::add_edge(0, 1, g1); boost::add_edge(1, 2, g1); boost::add_edge(2, 0, g1);
// Same triangle, relabeled: 2->0->1->2
boost::add_edge(2, 0, g2); boost::add_edge(0, 1, g2); boost::add_edge(1, 2, g2);
bool result = boost::isomorphism(g1, g2);
std::cout << "Graphs are " << (result ? "isomorphic" : "not isomorphic") << "\n";
}
Graphs are isomorphic
(1) Positional version
template <typename Graph1, typename Graph2, typename IsoMap,
typename VertexInvariant1, typename VertexInvariant2,
typename V1Map, typename V2Map>
bool isomorphism(
const Graph1& g1, const Graph2& g2,
IsoMap f, VertexInvariant1 invariant1, VertexInvariant2 invariant2,
std::size_t max_invariant, VertexIndex1Map i1_map, VertexIndex2Map i2_map);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed or undirected graph. The graph type must be a model of Vertex List Graph and Edge List Graph. |
IN |
|
A directed or undirected graph. The graph type must be a model of Bidirectional Graph and Vertex List Graph. |
OUT |
|
The mapping from vertices in graph 1 to vertices in graph 2. |
IN |
|
Mapping from vertices of graph 1 to integers which restricts which vertices may be considered isomorphic. See the named parameter version for details. |
IN |
|
Mapping from vertices of graph 2 to integers which restricts which vertices may be considered isomorphic. See the named parameter version for details. |
IN |
|
This parameter is ignored as it is no longer necessary, but kept for backwards compatibility. |
IN |
|
This maps each vertex of graph 1 to an integer in the range
|
IN |
|
This maps each vertex of graph 2 to an integer in the range
|
(2) Named parameter version
template <class Graph1, class Graph2, class P, class T, class R>
bool isomorphism(
const Graph1& g1, const Graph2& g2,
const bgl_named_params<P,T,R>& params = all defaults);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed or undirected graph. The graph type must be a model of Vertex List Graph and Edge List Graph. |
IN |
|
A directed or undirected graph. The graph type must be a model of Bidirectional Graph and Vertex List Graph. |
IN |
|
Named parameters passed via |
| Direction | Named Parameter | Description / Default |
|---|---|---|
OUT |
|
The mapping from vertices in graph 1 to vertices in graph 2. |
IN |
|
Mappings from vertices to integers which restrict which vertices may be
considered isomorphic. If a candidate isomorphism maps v1 to v2 but
i1(v1) != i2(v2), that candidate is rejected. This mapping can
be used either to speed up the search (as is done by the default value,
which requires that the degrees of v1 and v2 are equal) or to impose
extra conditions on the result. The |
IN |
|
This parameter is ignored as it is no longer necessary, but kept for backwards compatibility. |
IN |
|
This maps each vertex to an integer in the range
|
IN |
|
This maps each vertex to an integer in the range
|
Description
An isomorphism is a 1-to-1 mapping of the vertices in one graph to the vertices of another graph such that adjacency is preserved. In other words, given graphs G1 = (V1,E1) and G2 = (V2,E2) an isomorphism is a function f such that for all pairs of vertices a,b in V1, edge (a,b) is in E1 if and only if edge (f(a),f(b)) is in E2.
This function returns true if there exists an isomorphism between
graph 1 and graph 2 and false otherwise. Also, if an isomorphism map
named parameter is provided then an isomorphism is recorded in the map.
The current implementation is based on descriptions of a backtracking algorithm in [41,43]. The algorithm used is simple but slow. A more efficient (and much more complex) algorithm is described in [42]. When (and if) a version of this algorithm is ported to the BGL interface it should replace the current algorithm.