Question

I have an N x 2 table of integers called games[ , ]. The table of nodes/edges is converted to a graph:

net <- graph.data.frame(as.data.frame(games), directed=FALSE)
deg.net <- degree(net, mode='total', loops=FALSE) 

(I realize that not all options are necessary.)

The problem I am having is that the degree distribution seems to be for in-degree only. For example, the games file has the lines:

103 86
24 103
103 2
92 103
87 103
103 101
103 44

and yet igraph indicates that the degree for node 103 is '3' when it should be '7'.

Any insight in what I am missing would be appreciated.

Was it helpful?

Solution

One thing that you should keep in mind is that most igraph functions refer to the vertices by their IDs, which are simply integers from 0 to N-1 where N is the number of vertices in the graph. If you have an N x 2 table of integers (containing zero-based vertex indices) and you want igraph to use the integers as the vertex IDs, you can simply use the graph constructor after having flattened the matrix into a vector by rows. When you use graph.data.frame, the first two columns of the data frame are assumed to contain symbolic vertex names (i.e. there is no requirement that they must be integers); these will be assigned to the name vertex attribute, and igraph will simply make up the IDs from 0 to N-1.

So, let's assume that you have an N x 2 matrix, one row per each edge:

> edges <- matrix(c(103, 86, 24, 103, 103, 2, 92, 103, 87, 103, 103, 101, 103, 44), ncol=2, byrow=T)

First we create a graph out of it after flattening the matrix by rows:

> g <- graph(as.vector(t(edges)))

This gives you a directed graph with 7 edges and the out/in-degrees of vertex 103 will be as expected:

> ecount(g)
7
> degree(g, 103, mode="out")
4
> degree(g, 103, mode="in")
3
> degree(g, 103, mode="all")
7

If you use graph.data.frame with the above matrix, igraph will construct a graph where the numbers in the matrix are stored in the name vertex attribute:

> g <- graph.data.frame(as.data.frame(edges))
> V(g)$name
[1] "103" "24"  "92"  "87"  "86"  "2"   "101" "44"

This shows you that the vertex with the name 103 actually became vertex zero in the graph:

> degree(g, 0, mode="out")
4
> degree(g, 0, mode="in")
3
> degree(g, 0, mode="all")
7

As far as I know, degree is also able to work with the vertex names directly if there is a vertex attribute called name in the graph, so you can also do this:

> degree(g, "103", mode="in")
3

Hope this helps.

OTHER TIPS

You created a undirected graph. There is no in and out degree in such a graph. From the igraph documentation link you can get the general idea. Your deg.net will return a vector with all the degrees(per node) on your graph, similar to this:

[1] 2 1 1 1 1 1 1

If you want to get the degree of a specific node(in our example it's 103) you have to specify the node(appearence_order-1). In your example you are looking for the (1st_node-1), it's node 0. So you must type:

degree(net,0, mode='total', loops=FALSE)

Which will return the degree of node 0, "7".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top