在树形图中获取节点后代 [英] Get node descendants in a tree graph

查看:113
本文介绍了在树形图中获取节点后代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有向图( grafopri1fase1 )该图没有循环,并且它具有树结构(不是二叉树)。
我有一个从图( grafopri1fase1 )中提取的节点数组( meterdiretti )匹配一个条件。



我想知道从 Meterdiretti 的每个节点开始, Meterdiretti



我想要的结果是一个具有以下格式的矩阵

 第一列------------第二列
meterdiretti [1] --------从meterdiretti [1]开始可访问的节点总数

meterdiretti [2] --------从meterdiretti [2]开始可访问的节点总数

....

meterdiretti [n] ----------从meterdiretti [n]
开始可访问的节点总数pre>

解决方案

根据需要选择合适的对象-如果您可以在问题中添加可复制的示例,那就很好了。 / p>

我认为您要算的是节点的后代。您可以使用 neighborhood.size mode = out 自变量来做到这一点。

  library(igraph)

#创建一个随机图
g<-graph.tree(17,children = 2)
plot(g,layout = layout.reingold.tilford)

#在单个节点上测试
Neighborhood.size(g,vcount(g), 1, out)-1
#[1] 16

#应用于几个节点
Neighborhood.size(g,vcount(g),c(1,4,7 ), out)-1
[1] 16 4 2


I have a directed graph (grafopri1fase1) the graph has no loops and it has a tree structure (not binary tree). I have an array of nodes (meterdiretti) that i have extracted from the graph (grafopri1fase1) matching a condition.

I would like to know starting from each node of Meterdiretti how many nodes are under each node of Meterdiretti.

The result I would like to have is a Matrix with the following format

first column------------     second column       
meterdiretti[1] -------- total amount of nodes reachable starting from meterdiretti[1] 

meterdiretti[2] -------- total amount of nodes reachable starting from meterdiretti[2] 

.... 

meterdiretti[n]  ----------total amount of nodes reachable starting from meterdiretti[n]

解决方案

Take a punt at what you want - it would be good if you could add a reproducible example to your question.

I think what you want is to count the descendents of a node. You can do this with neighborhood.size and mode="out" argument.

library(igraph)

# create a random graph
g <- graph.tree(17, children = 2)
plot(g, layout=layout.reingold.tilford)

# test on a single node    
neighborhood.size( g, vcount(g),  "1", "out") - 1
# [1] 16

# apply over a few nodes
neighborhood.size( g, vcount(g),  c(1,4,7), "out") - 1
[1] 16  4  2

这篇关于在树形图中获取节点后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆