覆盖“不存在的组件”循环中

发布于 2025-01-09 17:24:12 字数 1081 浏览 0 评论 0原文

我有以下网络图:

library(tidyverse)
library(igraph)


set.seed(123)
n=15
data = tibble(d = paste(1:n))

relations = tibble(
  from = sample(data$d),
  to = lead(from, default=from[1]),
)

graph = graph_from_data_frame(relations, directed=T, vertices = data) 

V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")

plot(graph, layout=layout.circle, edge.arrow.size = 0.2)

在此处输入图像描述

我学会了如何删除该图中的所有“边缘”:

g <- graph-E(graph)
plot(g)

现在,我正在尝试做同样的事情,但使用“循环”:

for (i in 1:15)
 for (j in 1:15) { 

graph <- graph - edge(paste0(i, "|", j)) 

} 

但我认为问题在于上面的代码正在尝试删除存在的“边缘”,并且当命令该代码删除不存在的边缘时,此代码无法“跳过”(覆盖)实例:

Error in delete_edges(e1, unlist(e2, recursive = FALSE)) : 
  At iterators.c:1828 : Cannot create iterator, invalid edge id, Invalid vertex id

是否有一种方法可以指示此“循环” “跳过”两条边没有连接的每个实例并继续直到循环完成?

谢谢你!

I have the following network graph:

library(tidyverse)
library(igraph)


set.seed(123)
n=15
data = tibble(d = paste(1:n))

relations = tibble(
  from = sample(data$d),
  to = lead(from, default=from[1]),
)

graph = graph_from_data_frame(relations, directed=T, vertices = data) 

V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")

plot(graph, layout=layout.circle, edge.arrow.size = 0.2)

enter image description here

I learned how to remove all the "edges" in this graph:

g <- graph-E(graph)
plot(g)

Now, I am trying to do this same thing, but using a "loop" instead:

for (i in 1:15)
 for (j in 1:15) { 

graph <- graph - edge(paste0(i, "|", j)) 

} 

But I think the problem is that the above code is trying to delete "edges" that exist, and this code does not have the ability to "skip" (override) an instance when it is commanded to delete a non-existing edge:

Error in delete_edges(e1, unlist(e2, recursive = FALSE)) : 
  At iterators.c:1828 : Cannot create iterator, invalid edge id, Invalid vertex id

Is there a way to instruct this "loop" to "skip" every instances where two edges do not have a connection and to continue until the loop is done?

Thank you!

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

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

发布评论

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

评论(1

难以启齿的温柔 2025-01-16 17:24:12

我不知道您为什么要运行 for 循环,但请使用 igraph 中的 as_edgelist() 函数在下面找到一个可能的解决方案> 图书馆。

Reprex

  • 您的数据
library(igraph)
library(dplyr)

set.seed(123)
n=15
data = tibble(d = paste(1:n))

relations = tibble(
  from = sample(data$d),
  to = lead(from, default=from[1]),
)

graph = graph_from_data_frame(relations, directed=T, vertices = data) 

V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")

plot(graph, layout=layout.circle, edge.arrow.size = 0.2)

  • 建议代码
edgelist <- as_edgelist(graph, names = TRUE)

  for (i in 1:15) { 
    
    graph <- graph - edge(paste0(edgelist[i,1], "|", edgelist[i,2])) 
    
  } 

plot(graph)

reprex 包 (v2.0.1)

I don't know why you want to run a for loop, but please find below a possible solution using the as_edgelist() function from the igraph library.

Reprex

  • Your data
library(igraph)
library(dplyr)

set.seed(123)
n=15
data = tibble(d = paste(1:n))

relations = tibble(
  from = sample(data$d),
  to = lead(from, default=from[1]),
)

graph = graph_from_data_frame(relations, directed=T, vertices = data) 

V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")

plot(graph, layout=layout.circle, edge.arrow.size = 0.2)

  • Suggested code
edgelist <- as_edgelist(graph, names = TRUE)

  for (i in 1:15) { 
    
    graph <- graph - edge(paste0(edgelist[i,1], "|", edgelist[i,2])) 
    
  } 

plot(graph)

Created on 2022-02-24 by the reprex package (v2.0.1)

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