Skip to contents

Create a new graph, by permuting vertex ids.

Usage

permute(graph, permutation)

Arguments

graph

The input graph, it can directed or undirected.

permutation

A numeric vector giving the permutation to apply. The first element is the new id of vertex 1, etc. Every number between one and vcount(graph) must appear exactly once.

Value

A new graph object.

Details

This function creates a new graph from the input graph by permuting its vertices according to the specified mapping. Call this function with the output of canonical_permutation() to create the canonical form of a graph.

permute() keeps all graph, vertex and edge attributes of the graph.

Author

Gabor Csardi csardi.gabor@gmail.com

permute_vertices().

Examples


# Random permutation of a random graph
g <- sample_gnm(20, 50)
g2 <- permute(g, sample(vcount(g)))
isomorphic(g, g2)
#> [1] TRUE

# Permutation keeps all attributes
g$name <- "Random graph, Gnm, 20, 50"
V(g)$name <- letters[1:vcount(g)]
E(g)$weight <- sample(1:5, ecount(g), replace = TRUE)
g2 <- permute(g, sample(vcount(g)))
isomorphic(g, g2)
#> [1] TRUE
g2$name
#> [1] "Random graph, Gnm, 20, 50"
V(g2)$name
#>  [1] "r" "d" "s" "a" "k" "i" "o" "n" "f" "h" "e" "t" "p" "j" "b" "c" "g" "l" "m"
#> [20] "q"
E(g2)$weight
#>  [1] 4 3 1 1 4 1 1 3 5 3 4 3 3 4 2 4 2 3 1 1 3 2 4 1 1 3 3 1 3 5 4 3 4 5 2 4 1 3
#> [39] 4 1 3 5 2 3 5 4 3 1 1 1
all(sort(E(g2)$weight) == sort(E(g)$weight))
#> [1] TRUE