1

Suppose I have an adjacency matrix as follows:

library(igraph)
df <- data.frame(id = 1:8, parent = c(NA, NA, 1, 1, 3, 4, NA, 7))
g <- graph_from_data_frame(na.omit(df))

sample graph

For each vertex, how do I show the first and final vertices in the directed path? For example, vertex '4' starts at 6 and ends with 1. (Alternatively, obtaining a list of all vertices in that path would work).

Soul Donut
  • 357
  • 3
  • 12
  • So you just want the in and out neighbors? What would be the result for vertex 1 or vertex 8? What if a vertex is connected to more than 2 other vertices? Maybe checkout the `neighbors()` function. – MrFlick Apr 27 '16 at 20:04
  • I want the root/terminal leaves of each tree. The data (should) be such that all trees in the forest only have a single root. – Soul Donut Apr 27 '16 at 20:25

2 Answers2

5

Consider a topological sort. Topological sorting a directed graph will give you the first and last vertex.

For R you may use the topological sort method in the igraph package. http://igraph.org/r/doc/topo_sort.html

jsta
  • 3,216
  • 25
  • 35
Felipe Sulser
  • 1,185
  • 8
  • 19
0

Following this answer, I ended up doing a graph decomposition on the forest, then finding which vertices had an out-degree equal to 0, thus determining the root node for each tree (doing the same for in-degree yields which vertices are terminal, although I realized I didn't need this information -- as a result I'm not marking this as the answer).

library(igraph)
library(dplyr)

df <- data.frame(id = 1:8, parent = c(NA, NA, 1, 1, 3, 4, NA, 7))
edgelist_df <- na.omit(df)
g <- graph_from_data_frame(edgelist_df)

tree_to_df <- function(graph, forest_edgelist){
  # for a directed tree, find its root and assign that root to every
  # node in the tree's edgelist

  # `dplyr::filter` fails on the subset below, so we use base R
  tree_dat <- forest_edgelist[forest_edgelist$id %in% V(graph)$name,]

  root <- which(degree(graph, v = V(graph), mode = 'out') == 0, useNames = T)
  tree_dat$root <- names(root)
  return(tree_dat)
}

root_dat <-
   decompose.graph %>% # find connected subgraphs
   lapply(tree_to_df, forest_edgelist = edgelist_df) %>%
   bind_rows
Community
  • 1
  • 1
Soul Donut
  • 357
  • 3
  • 12