根据某些条件生成随机图
Generating Random Graphs According to Some Conditions
考虑以下几点:1,2,3,4,5,6,7,8,9,10
我可以根据这些数字制作有向随机图:
library(igraph)
from = sample(1:10, 10, replace=FALSE)
to = sample(1:10, 10, replace=FALSE)
graph_data = data.frame(from,to)
graph_data$id = as.factor(1:10)
graph <- graph.data.frame(graph_data, directed=T)
graph = simplify(graph)
V(graph)$color <- ifelse(graph_data$id == 1, "red", "orange")
plot(graph, layout=layout.circle, edge.arrow.size = 0.2)
我想对上图进行更改(即“条件”),以便:
- None 个“节点”是孤立的(例如节点 #6 是“孤立的”)
- 每个节点只能有“入边”和“出边”(即如果你在这个图中的某个“节点”上,如果你一直沿着向前的箭头方向,你最终会访问每个其他“节点”正好一次,最后回到你开始的“节点”)
- “最后一个节点”(即最后一行“to”列中的“节点”)始终连接到“第一个节点”(即“from”列中的“节点”第一行)
在这种情况下,这意味着:
from to
1 9 4
2 8 2
3 2 1
4 3 7
5 6 6
6 1 5
7 10 3
8 5 9
9 4 8
10 7 10
需要添加额外的一行,以便“节点 10”必须连接到“节点 9”。
我可以手动执行此操作(例如 graph_data[nrow(graph_data) + 1,] = c("10","9", "11", "orange)
)但是有人可以告诉我如何将所有这些条件自动添加到正在生成的随机图中吗?
谢谢!
我认为以下符合您的要求。如果您像这样生成 from
和 to
:
from <- sample(1:10, 10, replace = FALSE)
to <- from[c(2:10, 1)]
然后重复你剩下的代码,你会得到:
这会生成一个循环图,其中节点标签是 1 到 10 之间的随机数。
这应该可以解决您的问题
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 == "1", "red", "orange")
plot(graph, layout=layout.circle, edge.arrow.size = 0.2)
更新 1
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)
给你
考虑以下几点:1,2,3,4,5,6,7,8,9,10
我可以根据这些数字制作有向随机图:
library(igraph)
from = sample(1:10, 10, replace=FALSE)
to = sample(1:10, 10, replace=FALSE)
graph_data = data.frame(from,to)
graph_data$id = as.factor(1:10)
graph <- graph.data.frame(graph_data, directed=T)
graph = simplify(graph)
V(graph)$color <- ifelse(graph_data$id == 1, "red", "orange")
plot(graph, layout=layout.circle, edge.arrow.size = 0.2)
我想对上图进行更改(即“条件”),以便:
- None 个“节点”是孤立的(例如节点 #6 是“孤立的”)
- 每个节点只能有“入边”和“出边”(即如果你在这个图中的某个“节点”上,如果你一直沿着向前的箭头方向,你最终会访问每个其他“节点”正好一次,最后回到你开始的“节点”)
- “最后一个节点”(即最后一行“to”列中的“节点”)始终连接到“第一个节点”(即“from”列中的“节点”第一行)
在这种情况下,这意味着:
from to
1 9 4
2 8 2
3 2 1
4 3 7
5 6 6
6 1 5
7 10 3
8 5 9
9 4 8
10 7 10
需要添加额外的一行,以便“节点 10”必须连接到“节点 9”。
我可以手动执行此操作(例如 graph_data[nrow(graph_data) + 1,] = c("10","9", "11", "orange)
)但是有人可以告诉我如何将所有这些条件自动添加到正在生成的随机图中吗?
谢谢!
我认为以下符合您的要求。如果您像这样生成 from
和 to
:
from <- sample(1:10, 10, replace = FALSE)
to <- from[c(2:10, 1)]
然后重复你剩下的代码,你会得到:
这会生成一个循环图,其中节点标签是 1 到 10 之间的随机数。
这应该可以解决您的问题
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 == "1", "red", "orange")
plot(graph, layout=layout.circle, edge.arrow.size = 0.2)
更新 1
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)
给你