[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] How to copy a vertex with it's respective edges (all/in/out
From: |
Tamás Nepusz |
Subject: |
Re: [igraph] How to copy a vertex with it's respective edges (all/in/out) from a directed graph g, to a new directed graph g1? |
Date: |
Sat, 17 Mar 2012 11:54:44 +0100 |
> Is there an effective method to copy a vertex with its edges in a new graph?
> The goal is to test sampling processes!
If you are trying to test sampling processes, then I presume that you have
selected a set of "sampled" vertices from a large graph and you are trying to
construct the subgraph that consists of the edges where at least one endpoint
is in the set of "sampled" vertices. I also assume that you are using R since
I've seen your question on Stack Overflow ;)
So, let's assume that your graph is in "g" and the set of sampled vertices is
in "sampled" (which is a vector consisting of zero-based vertex IDs).
First, we select the set of edges where at least one endpoint is in "sampled":
all.vertices <- (1:vcount(g)) - 1
es <- E(g) [ sampled %--% 1:n ]
es is now an "edge sequence" object that consists of the edges of interest.
Next, we take the edge list of the graph (which is an m x 2 matrix) and select
the rows corresponding to the edges:
el <- get.edgelist(g)[as.vector(es)+1]
Here, as.vector(es) converts the edge sequence into a vector consisting of the
edge IDs of the edges in the edge sequence, and use it to select the
appropriate subset of the edge list. Note that we had to add 1 to the edge IDs
because R vectors are indexed from 1 but igraph edge IDs are from zero.
Next, we construct d1 from the edge list:
g1 <- graph(el, vcount(g), directed=is.directed(g))
Note that g1 will contain exactly as many vertices as g. You can take the
subgraph consisting of the sampled vertices as follows:
g1 <- subgraph(g1, sampled)
Best,
T.