make_connected

Adds a minimal set of edges to make an undirected graph connected.

Complexity: O(n + m)
Defined in: <boost/graph/make_connected.hpp>

Example

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/make_connected.hpp>
#include <iostream>

using namespace boost;
using Graph = adjacency_list<vecS, vecS, undirectedS>;

int main() {
    Graph g(6);
    add_edge(0, 1, g);
    add_edge(2, 3, g);
    add_edge(4, 5, g);

    std::cout << "Edges before: " << num_edges(g) << "\n";
    boost::graph::make_connected(g);
    std::cout << "Edges after:  " << num_edges(g) << "\n";
}
Edges before: 3
Edges after:  5

Synopsis

namespace boost::graph {
template <typename Graph, typename VertexIndexMap,
          typename AddEdgeVisitor>
void make_connected(
    Graph& g,
    VertexIndexMap vm,
    AddEdgeVisitor vis);
}
Direction Parameter Description

IN/OUT

Graph& g

An undirected graph. The type Graph must be a model of VertexListGraph, IncidenceGraph, and MutableGraph.

IN

VertexIndexMap vm

A Readable Property Map that maps vertices from g to distinct integers in the range [0, num_vertices(g)).
Default: get(vertex_index, g)

IN

AddEdgeVisitor vis

A model of AddEdgeVisitor. The visitor is passed by value, so the algorithm works on a copy. Wrap it in std::ref(vis) to have your own visitor updated in place.
Default: default_add_edge_visitor, a class that defines visit_vertex_pair to dispatch its calls to add_edge.


Migrating from boost::make_connected

The boost::make_connected overloads are deprecated. They took the visitor by non-const reference, which rejected temporaries and differed from the other BGL algorithms. Removal planned for Boost 1.95. Calls left unchanged then fail to compile instead of silently changing behavior.

  1. Replace boost::make_connected with boost::graph::make_connected. Unqualified calls need qualifying too, since argument dependent lookup only finds the deprecated overloads.

  2. If you read the visitor after the call, pass it as std::ref(vis). Otherwise the algorithm works on a copy and your visitor is left unchanged.

// Before
make_connected(g, get(vertex_index, g), edge_updater);

// After
boost::graph::make_connected(g, get(vertex_index, g), std::ref(edge_updater));

boost::make_connected (deprecated)

Deprecated: use boost::graph::make_connected. Removal planned for Boost 1.95. See the migration steps.

namespace boost {
template <typename Graph, typename VertexIndexMap, typename AddEdgeVisitor>
void make_connected(Graph& g, VertexIndexMap vm, AddEdgeVisitor& vis);

template <typename Graph, typename VertexIndexMap>
void make_connected(Graph& g, VertexIndexMap vm);

template <typename Graph>
void make_connected(Graph& g);
}

Description

An undirected graph G is connected if, for every pair of vertices u,v in G, there is a path from u to v. make_connected adds the minimum number of edges needed to make the input graph connected. The algorithm first identifies all of the connected components in the graph, then adds edges to connect those components together in a path. For example, if a graph contains three connected components A, B, and C, make_connected will add two edges. The two edges added might consist of one connecting a vertex in A with a vertex in B and one connecting a vertex in B with a vertex in C.

The default behavior of make_connected is to modify the graph g by calling add_edge(u,v,g) for every pair of vertices (u,v) where an edge needs to be added to connect g. This behavior can be overridden by providing a visitor as the AddEdgeVisitor parameter. The only requirement for an AddEdgeVisitor is that it define a member function with the following signature:

template <typename Graph, typename Vertex>
void visit_vertex_pair(Vertex u, Vertex v, Graph& g);

This event point can also be used as a hook to update the underlying edge index map automatically as edges are added. See the documentation for the AddEdgeVisitor concept for more information.