How igraph functions handle attributes when the graph changes
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.
The functions that support the combination of attributes have one or two
extra arguments called
that specify how to perform the mapping of the attributes. E.g.
contract 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
- a character scalar,
- a function object or
- a list of character scalars and/or function objects.
If it is a character scalar, then it refers to one of the predefined combinations, see their list below.
If it is a function, then the given function is expected to perform the combination. It will be called once for each new vertex/edge in the graph, with a single argument: the attribute values of the vertices that map to that single vertex.
The third option, a list can be used to specify different combination methods for different attributes. A named entry of the list corresponds to the attribute with the same name. An unnamed entry (i.e. if the name is the empty string) of the list specifies the default combination method. I.e. list(weight="sum", "ignore") specifies that the weight of the new edge should be sum of the weights of the corresponding edges in the old graph; and that the rest of the attributes should be ignored (=dropped).
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"))