Fulla
Fulla

Reputation: 17

How to create two subgraphs from a given graph

Suppose a given directed graph G with N vertices and M edges.
How is it possible to create from the existing M connections of the graph G two subgraphs S_1 and S_2. These respective subgraphs each have half of the existing edges. Furthermore it is determined by randomness, whether an edge belongs to S_1 or S_2.

Example:

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

Of the 12 existing edges should randomly M/2=6 edges be selected, which are representing the Edges in S_1. The remaining 6 edges provide the edges is of S_2.

Upvotes: 0

Views: 480

Answers (1)

jlhoward
jlhoward

Reputation: 59345

Maybe I'm misunderstanding you:

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

Upvotes: 2

Related Questions