通过顶点读取图形而不是R中的边界列表 [英] read a graph by vertices not as an edge list in R

查看:127
本文介绍了通过顶点读取图形而不是R中的边界列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解释:我有一个存储在文本文件中的无向图作为边,其中每行包含两个值表示边,如:

  5 10 
1000 2
212 420



通常,当从文件中读取R中的图形时(使用 igraph ) ,它将被读为边,所以称为我们写的的边 E(g)并调用g的顶点,我们写 V(g)并且调用某个边的两个顶点(即调用某个边(边i))我们写 E(g)[i]



是否有类似的方法来调用一个顶点例如,如果我需要第三个边的第二个顶点,那么我需要输入什么?
从一开始,igraph上的某些东西是否将图形读取为顶点而不是边?喜欢把图形看作一个有两列的表格,这样每个边缘都被读为X [i] [1],X [i] [2]。

我需要这个,因为我想在所有顶点之间做一个循环,并从边缘单独选择它们,我认为如果每个顶点都被标记为表格中的元素,就有可能。



非常感谢您提供任何帮助解决方案

如果您有一个带顶点的双列表,您可以使用 graph_from_data_frame 将其转换为图形。要获得特定边缘的节点,您可以使用 ends

 # DATA = 
set.seed(2)
m = cbind(FROM = sample(LETTERS [1:5],10,TRUE),TO = sample(LETTERS [6:10],10,TRUE))

#转换为图
g = graph_from_data_frame(m,directed = FALSE)
#plot(g)

#第三个边上的第二个顶点
ends(graph = g,es = 3)[2]
#[1]I


To explain: I have an undirected graph stored in a text file as edges where each line consist of two values represent an edge, like:

5 10
1000 2
212 420
  .
  . 
  .

Normally when reading a graph in R from a file (using igraph), it will be read as edges so to call the edges of the graph "g" we write E(g) and to call the vertices of "g" we write V(g) and to call both vertices of a certain edge (i.e to call a certain edge (edge i)) we write E(g)[i].

My question: Is there a similar way to call one vertex only inside an edge not to call both of them.

For example, if I need the second vertex in the third edge then what I need to type? Also from the beginning, is there something on igraph to read the graph as vertices and not as edges? like to read the graph as a table with two columns such that each edge to be read as X[i][1], X[i][2].

I need this because I want to do a loop among all vertices and to choose them separately from the edge and I think it is possible if each vertex was labeled like an element in a table.

Many thanks in advance for any help

解决方案

If you have a two column table with vertices, you could use graph_from_data_frame to convert it into graph. To get nodes on particular edge, you can use ends.

#DATA
set.seed(2)
m = cbind(FROM = sample(LETTERS[1:5], 10, TRUE), TO = sample(LETTERS[6:10], 10, TRUE))

#Convert to graph
g = graph_from_data_frame(m, directed = FALSE)
#plot(g)

#Second vertex on third edge
ends(graph = g, es = 3)[2]
#[1] "I"

这篇关于通过顶点读取图形而不是R中的边界列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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