在 igraph R 中查找没有任何 predecessor/ingoing 边的顶点

Find vertices without any predecessor/ingoing edges in igraph R

如何获取图中没有任何前驱的顶点。例如,这是图形数据:

lfs = data.table( from = c('x', 'x', 'y'), to = c('y', 'p', 'z'))
lfs
#  from to
#:    x  y
#:    x  p
#:    y  z
g = graph_from_data_frame(lfs)
g
# IGRAPH DN-- 4 3 --
# + attr: name (v/c)
# + edges (vertex names):
# [1] x->y x->p y->z

在此图表中 x 没有任何前身。有没有什么查询函数可以方便的得到这样的顶点?

您可以使用 degree 找到进入节点的边

library(igraph)
lfs <- data.frame( from = c('x', 'x', 'y'), to = c('y', 'p', 'z'))    
g <- graph_from_data_frame(lfs)

# find the edges in to a node
degree(g, mode="in")
#x y p z 
#0 1 1 1

# You can then subset to get the node names
V(g)$name[!degree(g, mode="in")]
# "x"