I have a list of networks of family names. Each network is composed of two different subgraphs (for instance, net_277_278 is composed of subgraphs sg_277 and sg_278). (I induced the subgraphs using a complicated mix of the gsub and induced_subgraph functions).
All the edges in the same subgraph have the form Johnson_278--Smith_278, indicating a link between two surnames belonging to the same subgraph. Links between the two subgraphs are given by the same surname (but have a different subscript). A link between the two subgraphs looks like this: Johnson_277--Johnson_278.
For each subgraph, I want to to compute some network measures (e.g., centrality), but only for the nodes that are directly connected to the other subgraph. (Note that I do not want to induce new subgraphs, since this would alter the measures). For instance, for sg_277 I would like to compute some measures only for Johnson_277, if that's the only node with a link to the sg_278.
As a small example, this is one of the networks:
net[10] #277_278
$11_277_278
IGRAPH UN-- 9 6 --
+ attr: name (v/c)
+ edges (vertex names):
[1] SANCHEZ_110277--SANCHEZ_110278 SANCHEZ_110277--PANTOJA_110277 SANCHEZ_110278--GALVAN_110278
[4] PEREZ_110278 --VEGA_110278 PEREZ_110278 --OLVERA_110278 PATIÑO_110278 --SERRANO_110278
Graphically, it looks like this
And this is one of the subgraphs:
sgraph1[[10]] #277
IGRAPH UN-- 2 1 --
+ attr: name (v/c)
+ edge (vertex names):
[1] SANCHEZ_110277--PANTOJA_110277
In this case, I would like to run the function for eigenvector centrality (evcent(sgraph1[[10]])$vector), only for the nodes linking the two subgraphs (in this case, just SANCHEZ_110277, which, as can be seen in the full network, has a link to SANCHEZ_110278.
Is there a way to do this? I have been trying stuff using regular expressions, neighbors and ego() functions but none of these seem to help.
Related
In Python you can simply use graph.select() (atleast when reading the documentation: https://igraph.org/python/doc/api/igraph.VertexSeq.html) to select a vertex based on a value. I have a huge graph connecting movies that share an actor. However I would simply like to select one vertex from that graph and then plot it with its direct neighbors. I'm running into issues when I want to select just that single vertex.
I had hoped something like worked
graph.movies.select('Snatch (2000)')
But no luck.
Another approach I took was grabbing the single Snatch vertex by filtering all others out and then adding the edges.
snatch.graph <- induced.subgraph(g.movies, vids=V(g.movies)$name == 'Snatch (2000)')
snatch.edges <- edges(g.movies, "Snatch (2000)")
add_edges(snatch.graph, snatch.edges$edges)
However this returns an empty graph with only the snatch vertex.
My goal is to grab the Snatch vertex and plot this vertex, its DIRECT neighbors and the edges amomng them. Any suggestions? Thanks alot :D Been stuck o nthis for a quite a while -.-
You can use ?ego to grab the neighbours or ?make_ego_graph to form the graph. (Depending on whether your graph is directed or not, you may need to use the mode argument).
An example:
library(igraph)
# create some data
set.seed(71085002)
n = 10
g = random.graph.game(n, 0.25)
V(g)$name = seq_len(n)
# grab neighbours of node "1"
# set mindist = 0 to include node itself
# set order for the stepsize of the neigbourhood;
nb_g = make_ego_graph(g, order=1, node="1", mindist=0)[[1]]
# plot
# you could use the `layout` argument to
# keep nodes in the same position
op = par(mfrow=c(1,2), oma=rep(0,4))
plot(g, vertex.size=0, vertex.label.cex=3, main="Full graph")
plot(nb_g, vertex.size=0, vertex.label.cex=3, main="Sub graph")
par(op) # reset
If you just need a list of the neighbouring nodes:
ego(g, order=1, node="1", mindist=0)
# [[1]]
# + 4/10 vertices, named, from 00cfa70:
# [1] 1 4 6 9
I think the method using ego (by #user20650) is comprehensive and efficient.
Here is another option if you would like to find the sub-graph built on direct neighbors, which applies distances + induced_subgraph
> induced_subgraph(g, which(distances(g, "1") <= 1))
IGRAPH 99f872b UN-- 4 3 -- Erdos renyi (gnp) graph
+ attr: name (g/c), type (g/c), loops (g/l), p (g/n), name (v/n)
+ edges from 99f872b (vertex names):
[1] 1--4 1--6 1--9
I have an iGraph network in R and would like to find the shortest path connecting multiple nodes in my network (let's say nodes 1,3,4,7). Is there a function that can do that? Something like all_simple_paths but for one global solution?
The solution should look something like the path highlighted in yellow. Note that 1->2->4 is not selected even though it is just as short as 1->3->4.
library(igraph)
tree <- graph.tree(n = 8, children = 2, mode = "out")
tree <- add_edges(tree, c(3,4, 3,5))
plot(tree)
After doing some digging I think I found the answer to my own question. What I was describing is a variation of the minimum spanning tree problem called the Steiner tree problem.
Given a weighted graph G = (V, E), a subset S ⊆ V of the vertices, and a root r ∈ V , we want to find a minimum weight tree which connects all the vertices in S to r. [ref]
Turns out there is a R package called SteinerNet created specifically for these types of problems. I had trouble installing their package directly but was able to copy the relevant source code from their GitHub repo.
out <- steinertree(type = "KB", terminals = c(1,3,4,7), graph = tree)
The package does exactly what I wanted to do, and it even produced a pretty graph!
>out[[2]]
IGRAPH fbb52e5 UN-- 4 3 -- Tree
+ attr: name (g/c), children (g/n), mode (g/c), name (v/c), color (v/c)
+ edges from fbb52e5 (vertex names):
[3] 1--3 3--4 3--7
>plot(out[[1]])
how can I create a complete list of dyads from a vertex list?
I have a list (1, 2, 3...) and I need to generate a list containing all possible dyads from that list (1-1, 1-2, 1-3, 2-1, 2-2,...).
I've tried with get.edgelist, but it doesn't work, because the graph is not fully connected (all nodes are connected among them).
Thanks
Using igraph, you can grab all edges of a graph using E(g). If you'd want all possible edges, you can apply it on a complete graph (a graph that is fully connected). If the vertices in your graph are indeed in sequence from 1 to n, you can use make_full_graph() to make a Kn - that is to say a fully connected graph. In this example, the graph has 14 vertices.
g <- make_full_graph(14, directed=F)
el <- as_edgelist(g)
edges <- E(g)
edges_list <- split(el, rep(1:nrow(el), each = ncol(el)))
edges_vert <- unlist(list(t(el)))
edges will be the igraph-object, but I think what you're after is a list in R, like edges_list.
As you see, length(edges_list) is 91 since it is an undirected graph, and the number of edges in complete graphs is a function of the number of vertices.
A complete graph with n vertices is commonly written Kn and has these many edges:
Note that in igraph dyads are called edges and nodes are called vertices.
I'm fairly new to IGraph in R.
I'm doing community detection using IGraph and have already built my communities /clusters using the walktrap technique.
Next, within each cluster, I want to count the number of vertices between each two certain vertices. The reason I want to do this is, for each vertex XX, I want to list vertices that are connected to XX via say max 3 vertices, meaning no further than 3 vertices away from XX.
Can anyone help how this can be done in R please?
making a random graph (for demonstration):
g <- erdos.renyi.game(100, 1/25)
plot(g,vertex.size=3)
get walktrap communities and save as vertex attribute:
V(g)$community<-walktrap.community(g, modularity = TRUE, membership = TRUE)$membership
V(g)$community
now make a subgraph containing only edges and vertices of one community, e.g. community 2:
sub<-induced.subgraph(g,v=V(g)$community==2)
plot(sub)
make a matrix containing all shortest paths:
shortestPs<-shortest.paths(sub)
now count the number of shortest paths smaller or equal to 3.
I also exclude shortest paths from each node to itself (shortestPaths!=0).
also divide by two because every node pair appears twice in the matrix for undirected graphs.
Number_of_shortest_paths_smaller_3 <- length(which(shortestPs<=3 & shortestPs!=0))/2
Number_of_shortest_paths_smaller_3
Hope that's close to what you need, good luck!
I have a network with two types of edges, one with edge$weight=1 and the other edge$weight=2 (to define different relationships e.g. marriage vs. kinship). I want to choose the neighbors of nodes that are linked by edge$weight=2. Then, after getting the vertex names of the neighbors, I will calculate their degree only for edge$weight=1.
It seems simple, but the following code does not give the correct neighbor names:
V(net)[E(net)[E(net)$weight==2]]
Is there another way to get vertex names with respect to the edge type?
I think you are looking for the adj function which will return the vertices adjacent to a given edge set. In your case you can choose all the relevant vertices by using V(net)[ adj(E(net)$weight==2) ]
For example:
# make a sample graph with edge weights 1 or 2
net <- graph.full(20)
E(net)$weight <- sample(1:2,ecount(net),replace=T)
# now select only those vertices connected by an edge with weight == 2
V(net)[ adj(E(net)$weight==2) ]