如何从给定图形创建两个子图

时间:2015-09-07 18:40:13

标签: r igraph subgraph

假设给定的有向图G具有N个顶点和M个边。
如何从图M两个子图GS_1的现有S_2连接中创建。这些相应的子图每个都有一半的现有边。此外,它是由随机性决定的,边是属于S_1还是S_2

示例:

G <- erdos.renyi.game(5,p=0.5,directed = TRUE)
E(G)
> + 12/12 edges:
> [1] 1->5 2->1 4->1 5->1 1->2 3->2 5->2 1->3 3->5 5->3 2->4 3->4

12现有边缘中,应选择随机M/2=6个边,这些边代表S_1中的边。剩余的6边缘提供的边缘为S_2

1 个答案:

答案 0 :(得分:2)

也许我误解了你:

library(igraph)
set.seed(1)     # for reproducible example
G <- erdos.renyi.game(5,p=0.5,directed = TRUE) 
E(G) 
# Edge sequence:
#           
# [1] 1 -> 5
# [2] 3 -> 1
# [3] 3 -> 2
# [4] 4 -> 2
# [5] 1 -> 3
# [6] 2 -> 3
# [7] 3 -> 5
# [8] 2 -> 4
# [9] 5 -> 4

eids <- sample(1:ecount(G), ecount(G)/2)
S1 <- subgraph.edges(G, eids)
S2 <- subgraph.edges(G, (1:ecount(G))[-eids])
E(S1)
# Edge sequence:
#           
# [1] 1 -> 5
# [2] 3 -> 2
# [3] 4 -> 2
# [4] 5 -> 4
E(S2)
# Edge sequence:
#           
# [1] 3 -> 1
# [2] 1 -> 3
# [3] 2 -> 3
# [4] 3 -> 5
# [5] 2 -> 4