[#sec:copy_graph]
= 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>`

WARNING: `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

[source,cpp]
----
include::example$algorithms/utility/copy_graph.cpp[]
----

[,text]
----
include::example$algorithms/utility/copy_graph.txt[]
----

'''

=== (1) Named parameter version

[source,cpp]
----
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);
----

[cols="1,2,5"]
|===
| Direction | Parameter | Description

| IN | `const VertexListGraph& G`
| A directed or undirected graph.
  The graph type must be a model of xref:concepts/VertexListGraph.adoc[Vertex List Graph].

| OUT | `MutableGraph& G_copy`
| The resulting copy of the graph.
  The graph type must be a model of xref:concepts/MutableGraph.adoc[Mutable Graph].
|===

*Named Parameters*

[cols="1,2,5"]
|===
| Direction | Parameter | Description

| IN | `vertex_copy(VertexCopier vc)`
| This is a link:http://www.boost.org/sgi/stl/BinaryFunction.html[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 link:http://www.boost.org/sgi/stl/BinaryFunction.html[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 link:https://www.boost.org/libs/property_map/doc/ReadablePropertyMap.html[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 link:https://www.boost.org/libs/property_map/doc/iterator_property_map.html[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

[source,cpp]
----
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)`.

[cols="1,2,5"]
|===
| 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.

