---
title: "VertexReplace"
language: "en"
type: "Symbol"
summary: "VertexReplace[g, {v1 -> w1, v2 -> w2, ...}] replaces each vertex vi in the graph g by wi. VertexReplace[{v -> w, ...}, ...] uses rules v -> w to specify the graph g."
keywords: 
- relabel graph
- rename graph
- vertex relabel
- vertex permutation
- permute graph
- remap graph
canonical_url: "https://reference.wolfram.com/language/ref/VertexReplace.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Graph Operations and Modifications"
    link: "https://reference.wolfram.com/language/guide/GraphModifications.en.md"
related_functions: 
  - 
    title: "FindGraphIsomorphism"
    link: "https://reference.wolfram.com/language/ref/FindGraphIsomorphism.en.md"
  - 
    title: "IndexGraph"
    link: "https://reference.wolfram.com/language/ref/IndexGraph.en.md"
  - 
    title: "VertexList"
    link: "https://reference.wolfram.com/language/ref/VertexList.en.md"
---
# VertexReplace

VertexReplace[g, {v1 -> w1, v2 -> w2, …}] replaces each vertex vi in the graph g by wi.

VertexReplace[{v -> w, …}, …] uses rules v -> w to specify the graph g.

## Details and Options

* ``VertexReplace`` is also known as vertex substitution.

* ``VertexReplace`` effectively uses ``Replace`` for each vertex in the ``VertexList``.

[image]

* ``VertexReplace`` works with undirected graphs, directed graphs, multigraphs, and mixed graphs.

## Examples (14)

### Basic Examples (2)

Replace individual vertices in the graph:

```wl
In[1]:= VertexReplace[[image], 1 -> "Hello"]

Out[1]= [image]
```

---

Replace all vertices in the graph:

```wl
In[1]:= VertexReplace[[image], Table[i -> Subscript[v, i], {i, 4}]]

Out[1]= [image]
```

### Scope (6)

``VertexReplace`` works with undirected graphs:

```wl
In[1]:= VertexReplace[[image], {1 -> "hi", 5 -> "hello"}]

Out[1]= [image]
```

---

Directed graphs:

```wl
In[1]:= VertexReplace[[image], {1 -> "hi"}]

Out[1]= [image]
```

---

Multigraphs:

```wl
In[1]:= VertexReplace[[image], {1 -> "hi"}]

Out[1]= [image]
```

---

Mixed graphs:

```wl
In[1]:= VertexReplace[[image], {1 -> "hi"}]

Out[1]= [image]
```

---

Use rules to specify the graph:

```wl
In[1]:= VertexReplace[{1 -> 3, 2 -> 1, 3 -> 6, 4 -> 6, 1 -> 5, 5 -> 4, 6 -> 1}, {1 -> "hi"}]

Out[1]= [image]
```

---

``VertexReplace`` works with large graphs:

```wl
In[1]:= g = GridGraph[{10, 10, 10, 10}];

In[2]:= Timing[h = VertexReplace[g, 1 -> VertexCount[g] + 1];]

Out[2]= {0.008165, Null}

In[3]:= {VertexCount[h], EdgeCount[h]}

Out[3]= {10000, 36000}
```

### Applications (1)

Create a graph that is isomorphic to the original graph:

```wl
In[1]:= g = PetersenGraph[4, 1, VertexLabels -> "Name", ImagePadding -> 10];

In[2]:=
vl = VertexList[g]; n = VertexCount[g];
h = VertexReplace[g, Thread[vl -> RandomSample[vl, n]]];

In[3]:= IsomorphicGraphQ[g, h]

Out[3]= True
```

Find an isomorphism that maps two graphs:

```wl
In[4]:= map = First[Normal[FindGraphIsomorphism[g, h]]]

Out[4]= {1 -> 1, 2 -> 6, 3 -> 4, 4 -> 2, 5 -> 7, 6 -> 5, 7 -> 8, 8 -> 3}
```

Highlight and label two graphs according to the mapping:

```wl
In[5]:= a = First /@ map;b = Last /@ map;

In[6]:= highlightGraph[g_, v_] := HighlightGraph[g, Table[Style[Labeled[v[[i]], v[[i]]], ColorData["TemperatureMap"][i / VertexCount[g]]], {i, VertexCount[g]}], VertexSize -> Large];

In[7]:= {highlightGraph[g, a], highlightGraph[h, b]}

Out[7]= {[image], [image]}
```

### Properties & Relations (5)

The graph created by replacing vertices has the same number of vertices as the original graph:

```wl
In[1]:= g = [image];

In[2]:= VertexCount[g] == VertexCount[VertexReplace[g, 1 -> 10]]

Out[2]= True
```

---

The graph created by replacing vertices has the same number of edges as the original graph:

```wl
In[1]:= g = [image];

In[2]:= EdgeCount[g] == EdgeCount[VertexReplace[g, 1 -> 10]]

Out[2]= True
```

---

The graph created by replacing vertices is isomorphic to the original graph:

```wl
In[1]:= g = [image];

In[2]:= IsomorphicGraphQ[g, VertexReplace[g, 1 -> 10]]

Out[2]= True
```

---

``IndexGraph`` can be implemented using ``VertexReplace`` :

```wl
In[1]:= {VertexReplace[[image], {1 -> 5, 2 -> 6, 3 -> 7, 4 -> 8}], IndexGraph[[image], 5]}

Out[1]= {[image], [image]}
```

---

The graph created by replacing vertices has the same adjacency matrix as the original graph:

```wl
In[1]:= g = RandomGraph[{10, 20}]

Out[1]= [image]

In[2]:= MatrixPlot[AdjacencyMatrix[#]]& /@ {g, VertexReplace[g, {1 -> "a", 3 -> "b"}]}

Out[2]= {[image], [image]}
```

## See Also

* [`FindGraphIsomorphism`](https://reference.wolfram.com/language/ref/FindGraphIsomorphism.en.md)
* [`IndexGraph`](https://reference.wolfram.com/language/ref/IndexGraph.en.md)
* [`VertexList`](https://reference.wolfram.com/language/ref/VertexList.en.md)

## Related Guides

* [Graph Operations and Modifications](https://reference.wolfram.com/language/guide/GraphModifications.en.md)

## History

* [Introduced in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) \| [Updated in 2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md) ▪ [2015 (10.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn103.en.md)