Skip to contents

This function creates a new graph, by merging several vertices into one. The vertices in the new graph correspond to sets of vertices in the input graph.


contract(graph, mapping, vertex.attr.comb = igraph_opt("vertex.attr.comb"))



The input graph, it can be directed or undirected.


A numeric vector that specifies the mapping. Its elements correspond to the vertices, and for each element the id in the new graph is given.


Specifies how to combine the vertex attributes in the new graph. Please see attribute.combination() for details.


A new graph object.


The attributes of the graph are kept. Graph and edge attributes are unchanged, vertex attributes are combined, according to the vertex.attr.comb parameter.


Gabor Csardi


g <- make_ring(10)
g$name <- "Ring"
V(g)$name <- letters[1:vcount(g)]
E(g)$weight <- runif(ecount(g))

g2 <- contract(g, rep(1:5, each = 2),
  vertex.attr.comb = toString

## graph and edge attributes are kept, vertex attributes are
## combined using the 'toString' function.
print(g2, g = TRUE, v = TRUE, e = TRUE)
#> IGRAPH 239966b UNW- 5 10 -- Ring
#> + attr: name (g/c), mutual (g/l), circular (g/l), name (v/c), weight
#> | (e/n)
#> + edges from 239966b (vertex names):
#>  [1] a, b--a, b a, b--c, d c, d--c, d c, d--e, f e, f--e, f e, f--g, h
#>  [7] g, h--g, h g, h--i, j i, j--i, j a, b--i, j