igraph (version 0.6-2)

unfold.tree: Convert a general graph into a forest

Description

Perform a breadth-first search on a graph and convert it into a tree or forest by replicating vertices that were found more than once.

Usage

unfold.tree(graph, mode = c("all", "out", "in", "total"), roots)

Arguments

graph
The input graph, it can be either directed or undirected.
mode
Character string, defined the types of the paths used for the breadth-first search. out follows the outgoing, in the incoming edges, all and total both of them. This argument
roots
A vector giving the vertices from which the breadth-first search is performed. Typically it contains one vertex per component.

Value

  • A list with two components:
  • treeThe result, an igraph object, a tree or a forest.
  • vertex_indexA numeric vector, it gives a mapping from the vertices of the new graph to the vertices of the old graph.

concept

  • Tree
  • Forest
  • Breadth-first search

Details

A forest is a graph, whose components are trees. The roots vector can be calculated by simply doing a topological sort in all components of the graph, see the examples below.

Examples

Run this code
g <- graph.tree(10)V(g)$id <- seq_len(vcount(g))-1
roots <- sapply(decompose.graph(g), function(x) {
            V(x)$id[ topological.sort(x)[1]+1 ] })
tree <- unfold.tree(g, roots=roots)

Run the code above in your browser using DataCamp Workspace