[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] igraph degree( ) confusion
From: |
Tamás Nepusz |
Subject: |
Re: [igraph] igraph degree( ) confusion |
Date: |
Sat, 10 Mar 2012 22:58:56 +0100 |
This is the same as the answer I've posted to your question on Stack Overflow,
but I also post it here for sake of completeness.
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)
[1] 7
> degree(g, 103, mode="out")
[1] 4
> degree(g, 103, mode="in")
[1] 3
> degree(g, 103, mode="all")
[1] 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")
[1] 4
> degree(g, 0, mode="in")
[1] 3
> degree(g, 0, mode="all")
[1] 7
As far as I know, the degree() function 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")
[1] 3
Finally, note that igraph 0.5.x and earlier versions use zero-based vertex
indices in R, and igraph 0.6 will switch to 1-based indices to make it more
consistent with the rest of the R environment. Therefore, if you query the
degrees for all the vertices, the degree of the vertex with ID=x will be found
at index x+1 in the result vector if you use igraph 0.5.x or earlier.
Hope this helps.
--
T.
On Friday, 9 March 2012 at 23:48, Robinson, David G wrote:
> 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 (http://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.
>
>
> > ---------------------------------------------
>
>
>
>
> _______________________________________________
> igraph-help mailing list
> address@hidden (mailto:address@hidden)
> https://lists.nongnu.org/mailman/listinfo/igraph-help