Extract just the number of edges in a class graphNEL - r

Using R I have created an graphNEL class (undirected) with nodes and edges. I want to save the numbers that gets printed out when I print the variable which the undirected graph is saved under:
graphNEL graph with undirected edges
Number of Nodes = 671
Number of Edges = 4267
I tried using the function edgeL(), but the number I get printed out is the number of nodes. I was thinking that I get this number because each gene has its own amount of edges, which is why the output equals the number of nodes. All I want is to save the number of edges in this graph. How can I do this?
Thanks

I am not familiar with the package, however it looks like nodes and edgeL are returned as slots. This means that you should be able to see the data using $ or # if you save your graphNEL as an object.
yourgraphObject#edgeL

Related

Alternative for shortest_path algorithm

I have a network consisting of 335 nodes. I computed the weighted shortest.paths between all of the nodes.
Now I would like to see which path sequences where used to travel between the nodes.
I use the the shortest_path command in igraph and iterate through all combinations of nodes in my network (335² combinations - 335(path from/to same node is 0)/2 (graph is undirected). So all in all I have to iterate over 55.945 combinations.
My approach looks like this:
net is my network
sp_data is a df with all combinations of links in the network
results1 <- sapply(sp_data[,1], function(x){shortest_paths(net, from = x, to = V(net), output="epath"})
Unfortunately this needs ages to compute and at the end I don't have enough memory to store the information. (Error: cannot allocate vector of size 72 Kb).
Basically I have two questions:
How can it be that the shortest.paths command needs seconds to compute the distance between all nodes of my network whereas extracting the path sequences (not just it length) needs days and exceeds the memory capacity?
Is there an alternative to get the desired output (path sequences of shortest path)? I guess that the sapply Syntax should already be faster than a for::loop?
you could try cppRouting package.
It provides get_multi_paths function which return a list containing the node sequence for each shortest path.
library(igraph)
library(cppRouting)
#random graph
g <- make_full_graph(335)
#convert to three columns data.frame
df<-data.frame(igraph::as_data_frame(g),dist=1)
#instantiate cppRouting graph
gr<-cppRouting::makegraph(df)
#extract all nodes
all_nodes<-unique(c(df$from,df$to))
#Get all paths sequence
all_paths<-get_multi_paths(Graph=gr,from=all_nodes,to=all_nodes)
#Get path from node 1 to 3
all_paths[["1"]][["3"]]

How to create network with both edges and isolates using statnet/igraph

My question is similar to the one posted here: Network adding edges error
I am creating a network from scratches: I have data about 228 vertices, over a period of 13 years. In the first year, I have just 1781 edges: they do not involve all of my vertices (barely 164), therefore the remaining nodes should result as isolated.
I created the network starting from my edgelist, using the code
fdi.graph.2003 <- graph_from_data_frame(fdi.edge.2003, directed = T, vertices = fdi.attr.2003)
where fdi.edge.2003 is a data.frame containing edge attributes (including some potential weight columns): it only involves 164 vertices out of the total vertices defined by fdi.attr.2003 is a data.frame containing a row for each vertex that is involved in the edgelist (164 in total).
all i get is a network with 164 vertices and no isolates. However, I know they do exist in my data! Any suggestion on how to do it? I think that I shoul initialize a network with all 228 vertices, adding their attributes and then adding the edges. However, nothing I am trying is working: rather, I am receiving the most disparate errors related to "Illegal vertex reference in addEdges_R".
Any suggestion is more than welcome, also in the case it would involve the alternative package igraph, for which I am finding the same problem
Filippo
Use add.isolates from the sna package
net1 = as.network(cbind(1:3, 3:5)) #5 vertices, 3 edges
net2 = as.network(add.isolates(net1, 10), matrix.type = "edgelist") #15 v, 3 e
And then you'll probably want to create new vertex names, e.g.
net2%v%"vertex.names" = 1:15

how to extract only the vertices with multiple edges from a graph using igraph in R

I am new to igraph and graph theory. I have a very large file (> 4 GB) and I was told it is a graph. I can see the format includes the pairs separated by tab and I can read it as a table first then convert it to graph data frame.
Number of vertices with vcount and number of edges with ecount suggest that there are vertices with multiple edges. I have been looking at various sources but I could not find the information about directly extracting the vertices with more than one edges.
Any help is appreciated.
To get the edges incident to each vertex (if g is your igraph)
ie <- igraph::incident_edges(g, igraph::V(g))
Then, to get the number of edges adjacent to each vertex
num.incident.edges <- sapply(ie, length)
Sorry, I guess I was wrong with the terminology. What I meant by vertices with multiple edges is called 'articulation_points'.
This was what I was looking for:
library(igraph)
bi <- biconnected_components(g)
bi$articulation_points

How to create graph with large number of points in R?

I have a large dataset contains a large number of nodes; more than 25000 nodes, organized in a .csv file. The structure is similar to the following :
node freq
a 3
b 2
c 5
I want to create a graph from these node in which edges between nodes are constructed by a function of the freq column. I have used the rgraph function from sna package, such as:
num_nodes <- length(data$node)
pLink = data$freq/10
# create 1 graph with nodes and link proability, graph loops = FALSE
graph_adj= rgraph(num_nodes,1,pLink,"graph",FALSE)
graph <- graph.adjacency(graph_adj, mode="undirected")
The above code is running in case of small number of nodes, but with large number of nodes, The R session aborted with the following Error:
Error: C stack usage 19924416 is too close to the limit
Is there another way to create a graph with the mentioned properties: a large number of nodes and edges are created with probability?

igraph edge between two vertices

I'm new to R and igraph and I was wondering if anybody can help me with the following.
I want to find the edge weight between two vertices in a graph. My graph structure is defined by the normal ego (node1), alter (node2) and the weight of the edge between them.
I know that I can get the weight for each of the edges in the list of edges that originate from node number 5 using E(igraph_friendship) [ from(5) ]$weight
And that I can find the weight for each of the edges in the list of edges that end onto node number 10 using E(igraph_friendship) [ to(10) ]$weight
But what if I simply want to find the weight of the edge that simple connects just node 5 and node 10?
Alternatively, if I can get the identifier of the edge that connects node 5 and 10 in the list of all edges, E(igraph_friendship), that would work too.
Thanks a lot for your help, I've been looking around a lot for it and I really appreciate your help!
Gabor's use of the adjacency matrix helped. However, it took me a while to figure out how to get my edge lists with weights into an adjacency matrix. I tried to do it the usual way using graph.data.frame but then would get a weird error when I tried translating the igraph object to and adjacency matrix (error: Error in .M.kind(x) : not yet implemented for matrix w/ typeof character). This post helped do the trick: https://sites.google.com/site/daishizuka/toolkits/sna/weighted-edgelists.
However, what I found out from the R help email list help to work best was this simple operator directly on the igraph object: E(g)[5 %--% 10]$weight. See http://igraph.sourceforge.net/doc/R/iterators.html for details
This is actually quite easy in igraph 0.6 and above, because you can
treat the graph as if it was an adjacency matrix (or weighed adjacency
matrix in your case):
library(igraph)
g <- graph.ring(10)
g[1,2]
# [1] 1
E(g)$weight <- runif(ecount(g))
g[1,2]
# [1] 0.8115639
If you want to do this for the whole matrix, then you can simply do
g[]
Or, if you don't want sparse matrices, then
g[sparse=FALSE]
Please see ?"[.igraph" for more.

Resources