Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem with igraph degree( ) function

Tags:

igraph

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.

like image 436
Aengus Avatar asked Nov 27 '25 10:11

Aengus


1 Answers

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.

like image 91
Tamás Avatar answered Dec 02 '25 01:12

Tamás



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!