copy_graph :: Boost.Graph Documentation

# copy_graph

Copies all vertices and edges from one graph into another, along with their properties.

Complexity: O(V + E)
Defined in: `<boost/graph/copy.hpp>`

| `copy_graph` internally requires a `vertex_index` property map. With `VertexList=listS`, the call will not compile unless you supply your own vertex index.

## Example

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

    int main() {
        using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS>;
        Graph g;
        boost::add_edge(0, 1, g);
        boost::add_edge(1, 2, g);
        boost::add_edge(2, 3, g);

        Graph g_copy;
        boost::copy_graph(g, g_copy);

        std::cout << "Original: " << num_vertices(g) << " vertices, "
                  << num_edges(g) << " edges\n";
        std::cout << "Copy:     " << num_vertices(g_copy) << " vertices, "
                  << num_edges(g_copy) << " edges\n";
    }

    Original: 4 vertices, 3 edges
    Copy:     4 vertices, 3 edges

### (1) Named parameter version

    template <class VertexListGraph, class MutableGraph,
              class P, class T, class R>
    void copy_graph(
        const VertexListGraph& G, MutableGraph& G_copy,
        const bgl_named_params<P, T, R>& params);

Direction | Parameter | Description
--- | --- | ---
IN | `const VertexListGraph& G` | A directed or undirected graph. The graph type must be a model of Vertex List Graph.
OUT | `MutableGraph& G_copy` | The resulting copy of the graph. The graph type must be a model of Mutable Graph.

Named Parameters

Direction | Parameter | Description
--- | --- | ---
IN | `vertex_copy(VertexCopier vc)` | This is a Binary Function that copies the properties of a vertex in the original graph into the corresponding vertex in the copy. Default: `vertex_copier<VertexListGraph, MutableGraph>` which uses the property tag `vertex_all` to access a property map from the graph.
IN | `edge_copy(EdgeCopier ec)` | This is a Binary Function that copies the properties of an edge in the original graph into the corresponding edge in the copy. Default: `edge_copier<VertexListGraph, MutableGraph>` which uses the property tag `edge_all` to access a property map from the graph.
IN | `vertex_index_map(VertexIndexMap i_map)` | The vertex index map type must be a model of Readable Property Map and must map the vertex descriptors of `G` to the integers in the half-open range `[0,num_vertices(G))`. Default: `get(vertex_index, G)`. Note: if you use this default, make sure your graph has an internal `vertex_index` property. For example, `adjacency_list` with `VertexList=listS` does not have an internal `vertex_index` property.
UTIL/OUT | `orig_to_copy(Orig2CopyMap c)` | This maps vertices in the original graph to vertices in the copy. Default: an iterator_property_map created from a `std::vector` of the output graph’s vertex descriptor type of size `num_vertices(g)` and using the `i_map` for the index map.

### (2) Positional version

    template <class VertexListGraph, class MutableGraph>
    void copy_graph(const VertexListGraph& G, MutableGraph& G_copy);

Equivalent to (1) with every named parameter left at its default. Uses the default vertex/edge copiers (via `vertex_all` / `edge_all` property tags) and `get(vertex_index, G)`.

Direction | Parameter | Description
--- | --- | ---
IN | `const VertexListGraph& G` | Same as (1).
OUT | `MutableGraph& G_copy` | Same as (1).

## Description

This function copies all of the vertices and edges from graph `G` into `G_copy`. Also, it copies the vertex and edge properties, either by using the `vertex_all` and `edge_all` property maps, or by user-supplied copy functions.