igraph: get and permute vertex ids - r

In R package igrpah, how are ids assigned to each vertex? Is there a similar function like get.edge.ids that we can get vertex id from a vertex, e.g. g = graph.ring(3); V(g)$name=LETTERS[1:3], so get.vertex.ids(V(g)['A']) will return the id of the vertex 'A', which is 1 in this example. And how can we change the id of the vertices? Sure we cannot change the id of only one node, but can we permute the vertex ids?

Use permute.vertices() to permute the IDs of the vertices. Note that the vertex IDs are always integers between 1 and |V| in R (where |V| is the number of vertices).

Related

Finding an edge by unique ID or name

It is possible to search a subgraph and node by their unique names;
n = agnode(g, "myUniqueNodeName", FALSE);
h = agsubg(g, "myUniqueSubgrahName", FALSE);
Likewise, is there a way to search edges in a strict directed graph by their unique names?
e = agedge (g, u, v, "e28", FALSE);
Documentation indicates that:
The 'name' of an edge (more correctly, identifier) is treated as a
unique indentifier for edges between a particular node pair. That is,
there can only be at most one edge with name e28 between any given u
and v, but there can be many other edges between other nodes.
It seems that there must be a list of edges that can be searched by name. Otherwise, a separate (ID -> edge) map will need to be maintained separately.

Query ArangoDB general-graph for common neighbors with more than 2 start vertices?

Consider the following example graph:
 
Given the vertices A, B and C (creators), how to figure out their common neighbors?(projects all 3 participated in)For two vertices, I could simply use GRAPH_COMMON_NEIGHBORS("myGraph", A, B), but what if I want to query for 3 or more? Expected result: 1 and 2.
Given the same vertices, how can I make it return common neighbors with no other connections?(creators exclusively participated in a project, no additional edges allowed)?Expected result: 1, because 2 has an edge coming from D, which isn't one of the starting vertices.
You can simply pass the same set of vertices as both parameters for common neighbors. Then repack the result in a better format for AQL to compute the intersection:
let res = (
let nodes = ["a/A","a/B","a/C"]
for n in GRAPH_COMMON_NEIGHBORS("g",nodes , nodes)
for f in VALUES(n)
return VALUES(f)
)
return CALL("intersection", res[0])

How do I merge two nodes into a single node using igraph

I am trying to merge two nodes (call them 'V' and 'U') in a graph (G) into a single node (V).
G is a hyperlink network of 779 nodes (websites). Each edge represents a hyperlink. V and U are actually the same website, but unfortunately the webpages from that website have become split into two separate nodes. So I want to put them back together into a single node.
I have researched the contract.vertices function, but I cannot understand how to adapt it here.
Here are the attributes of my graph (G).
> G
IGRAPH D--- 779 3544 --
+ attr: Image File (v/c), Ringset (v/n), Country Code TLD (v/n), Generic TLD (v/n), Number of Pages (v/n), Categorical 1 (v/n), Categorical 2 (v/n),
Categorical 3 (v/n), id (v/c), label (v/c), Width (e/n)
I have two nodes that I want to merge together:
> V(g)$id[8]
[1] "http://www.police.uk/"
and
> V(g)$id[14]
[1] "http://police.uk/"
In total there are 779 nodes and 3544 edges in the graph.
I want these two nodes to become a single node in the graph (i.e. they will have the same "id"). All inlinks and outlinks from/to other nodes will now point only to this new single node.
All other attributes will remain the same, with the exception of Number of Pages (the value of this will be the sum of both the nodes before they are merged).
contract.vertices is indeed the right function to try, but its API is a bit complicated since it is designed to be able to merge not only a single pair of nodes but also several pairs in a single pass. (It can also permute vertices). To this end, it requires a mapping from the old vertex IDs to the new ones.
In case you are unfamiliar with vertex IDs: igraph identifies each vertex of the graph with an integer in the range 1 to N where N is the number of vertices. The mapping that contract.vertices requires must be a list of length N where the i-th element of the list contains the new ID of the node corresponding to ID i before merging.
Suppose that your graph contains 10 nodes. The following mapping vector will simply map each node to the same ID that it already has, so it will not do any merging:
c(1,2,3,4,5,6,7,8,9,10)
Now, suppose that you want to merge node 7 into node 4. You have to tell igraph that the new ID of node 7 will be 4, so you have to change the 7th element in the above vector to 4:
c(1,2,3,4,5,6,4,8,9,10)
This will almost do the job; the problem is that igraph requires the node IDs to be in the range 1 to N and since you still have a node with ID 10 according to the above mapping, igraph will not delete the old node 7. You can either delete it manually with delete.vertices after you contracted the vertices, or you can specify a different mapping that not only merges node 7 into node 4 but also changes the ID of node 8 to 7, node 9 to 8 and node 10 to 9:
c(1,2,3,4,5,6,4,7,8,9)
Now, since you also want the Number of Pages attribute of the new node to be the sum of the values of the two old nodes, you must tell igraph what to do with the vertex attributes during the merge. The vertex.attr.comb parameter of contract.vertices serves this purpose. In your case, the value of vertex.attr.comb should be something like this:
list("Number of Pages"="sum", "first")
where "Number of Pages"="sum" means that the new value of the Number of Pages attribute should be calculated by summing the old attribute values, and "first" means that for all other attributes not mentioned here, the new value should be determined by the old value of the first node among the set of nodes that are merged into a single one. See ?attribute.combination in R for more details about the format of this argument.

Generate an OD list of nodes within n stops

I have a graph G(V,E), the number of edges is 35000 and the number of nodes is 3500,
Is there anyway I can generate a origin-destination list within n (say 4) stops for each node?
I think the function neighborhood() does exactly what you want. Set the order argument to 4 and for each vertex you'll get a vector of vertex ids for the vertices that are at most 4 steps away from it.
I figure it out:
Use the property of the adjacency matrix A, the entry in row i and column j of A^n gives the number of (directed or undirected) walks of length n from vertex i to vertex j. So for n stop, construct n matrix An, A(n-1)......A1, in which, An= A^n. Then the union of An,An-1....A1 should be the matrix that representing n stop reachable destinations for an origin.

How to copy a vertex with it's respective edges (all/in/out) from a directed graph g, to a new directed graph g1?

Is there a method or a class in igraph to do this procedure fast and efectively?
Let's assume that your graph is in g and the set of vertices to be used 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 the result 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)
Note to users of igraph 0.6 and above: igraph 0.6 will switch to 1-based indexing instead of 0-based, so there is no need to subtract 1 from all.vertices and there is no need to add 1 to as.vector(es). Furthermore, igraph 0.6 will contain a function called subgraph.edges, so one could simply use this:
g1 <- subgraph.edges(g, es)

Resources