igraph (version 0.6-1)

Combining attributes: How igraph functions handle attributes when the graph changes

Description

Many times, when the structure of a graph is modified, vertices/edges map of the original graph map to vertices/edges in the newly created (modified) graph. For example simplify maps multiple edges to single edges. igraph provides a flexible mechanism to specify what to do with the vertex/edge attributes in these cases.

Arguments

concept

Vertex/edge/graph attributes

preformatted

list(weight="sum", "ignore")

Specifying combination methods for all graphs

The are two standard igraph parameters that define the default behavior when combining vertices and edges: vertex.attr.comb specifies how to combine vertices by default, edge.attr.comb does the same for edges.

E.g. if you want to drop all vertex attributes when combining vertices, you can specify igraph.options(vertex.attr.comb="ignore")

As another example, if -- when combining edges -- you want to keep the mean weight of the edges, concatenate their names into a single character scalar, and drop everything else, then use igraph.options(edge.attr.comb=list(weight="mean", name=toString, "ignore")

Simple and complex attributes

An attribute is simple if (for all vertices/edges) it can be specified as an atomic vector. Character and numeric attributes are always simple. E.g. a vertex attribute that is a numeric vector of arbitrary length for each vertex, is a complex attribute.

Combination of attributes might turn a complex attribute into a single one, and the opposite is possible, too. E.g. when contatenating attribute values to form the new attribute value, the result will be typically a complex attribute.

See also examples below.

Details

The functions that support the combination of attributes have one or two extra arguments called vertex.attr.comb and/or edge.attr.comb that specify how to perform the mapping of the attributes. E.g. contract.vertices contracts many vertices into a single one, the attributes of the vertices can be combined and stores as the vertex attributes of the new graph.

The specification of the combination of (vertex or edge) attributes can be given as

  1. a character scalar,
a function object or a list of character scalars and/or function objects.

See Also

attributes on how to use graph/vertex/edges attributes in general. igraph.options on igraph parameters.

Examples

Run this code
g <- graph( c(1,2, 1,2, 1,2, 2,3, 3,4) )
E(g)$weight <- 1:5

## print attribute values with the graph
igraph.options(print.graph.attributes=TRUE)
igraph.options(print.vertex.attributes=TRUE)
igraph.options(print.edge.attributes=TRUE)

## new attribute is the sum of the old ones
simplify(g, edge.attr.comb="sum")

## collect attributes into a string
simplify(g, edge.attr.comb=toString)

## concatenate them into a vector, this creates a complex
## attribute
simplify(g, edge.attr.comb="concat")

E(g)$name <- letters[seq_len(ecount(g))]

## both attributes are collected into strings
simplify(g, edge.attr.comb=toString)

## harmonic average of weights, names are dropped
simplify(g, edge.attr.comb=list(weight=function(x) length(x)/sum(1/x),
                                name="ignore"))

Run the code above in your browser using DataCamp Workspace