防止 R igraph 添加零顶点

发布于 2024-12-02 19:14:11 字数 283 浏览 0 评论 0原文

我正在使用以下代码创建一个图表:

g <- graph(c(1,1, 1,4, 2,1, 2,2, 3,1, 3,4, 5,1, 5 ,2))

我想知道如何创建此图,而不必执行以下额外步骤:

delete.vertices(g, 0)

该图函数似乎是默认的到零索引顶点,因此图中存在不与任何其他顶点连接的顶点 0。我可以使用 V(g) 或简单的summary(g) 来验证这一点,它显示 id 从 0 到 5 的 6 个顶点。

I am creating a graph using the follow code:

g <- graph(c(1,1, 1,4, 2,1, 2,2, 3,1, 3,4, 5,1, 5,2))

I would like to know how I can create this graph and not have to go the extra step of:

delete.vertices(g, 0)

The graph function seems to default to zero indexed vertices and so a vertex 0 exists in the graph that is not connected with any other vertices. I can verify this using V(g), or simply summary(g), which show 6 vertices with ids from 0 to 5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掌心的温暖 2024-12-09 19:14:11

我从 r-forge 站点访问的文档说:“顶点和边在 igraph 中具有数字顶点 id。顶点 id 始终是连续的,并且从零开始。”因此,在将向量发送到图形函数之前,您需要从该向量中减去 1。否则,您最终会得到一个未连接的“0”顶点。避免未连接的“0”顶点的一般方法是从向量中减去最小值。

> g <- graph( c(1,1, 1,4, 2,1, 2,2, 3,1, 3,4, 5,1, 5,2) -1)
> g
Vertices: 5 
Edges: 8 
Directed: TRUE 
Edges:

[0] 0 -> 0
[1] 0 -> 3
[2] 1 -> 0
[3] 1 -> 1
[4] 2 -> 0
[5] 2 -> 3
[6] 4 -> 0
[7] 4 -> 1
> g2 <- graph( c(1,1, 1,4, 2,1, 2,2, 3,1, 3,4, 5,1, 5,2) )
> g2 <- delete.vertices(g2, 0)
> g2
Vertices: 5 
Edges: 8 
Directed: TRUE 
Edges:

[0] 0 -> 0
[1] 0 -> 3
[2] 1 -> 0
[3] 1 -> 1
[4] 2 -> 0
[5] 2 -> 3
[6] 4 -> 0
[7] 4 -> 1
> all.equal(g,g2)
[1] TRUE

所以我怀疑你的问题可能真的是“如何使用我自己的规则来标记顶点?”。试试这个:

g <- graph( c(1,1, 1,4, 2,1, 2,2, 3,1, 3,4, 5,1, 5,2) -1)
plot(g, vertex.label=1:5)

文档的开头部分还说:

igraph 主页位于 http://igraph.sourceforge.net。特别参见文档部分。如果您有问题或意见,请加入 igraph-help 邮件列表。

The documentation that I access from the r-forge site says: "Vertices and edges have numerical vertex ids in igraph. Vertex ids are always consecutive and they start with zero." So you need to subtract 1 from that vector before sending it to the graph function. Otherwise you end up with an unconnected "0" vertex. The general method for avoiding an unconnected "0" vertex would be to subtract the minimum from the vector.

> g <- graph( c(1,1, 1,4, 2,1, 2,2, 3,1, 3,4, 5,1, 5,2) -1)
> g
Vertices: 5 
Edges: 8 
Directed: TRUE 
Edges:

[0] 0 -> 0
[1] 0 -> 3
[2] 1 -> 0
[3] 1 -> 1
[4] 2 -> 0
[5] 2 -> 3
[6] 4 -> 0
[7] 4 -> 1
> g2 <- graph( c(1,1, 1,4, 2,1, 2,2, 3,1, 3,4, 5,1, 5,2) )
> g2 <- delete.vertices(g2, 0)
> g2
Vertices: 5 
Edges: 8 
Directed: TRUE 
Edges:

[0] 0 -> 0
[1] 0 -> 3
[2] 1 -> 0
[3] 1 -> 1
[4] 2 -> 0
[5] 2 -> 3
[6] 4 -> 0
[7] 4 -> 1
> all.equal(g,g2)
[1] TRUE

So I suspect you question might really be "How can I label vertices using my own rules?". Try this:

g <- graph( c(1,1, 1,4, 2,1, 2,2, 3,1, 3,4, 5,1, 5,2) -1)
plot(g, vertex.label=1:5)

The opening section of the docs also says:

The igraph homepage is at http://igraph.sourceforge.net. See especially the documentation section. Join the igraph-help mailing list if you have questions or comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文