igraph degree distribution: count elements

February 13, 2013
By

(This article was first published on 0xCAFEBABE, and kindly contributed to R-bloggers)

Unfortunately, the degree.distribution() function of the igraph library returns the intensities of the distribution:


> g <- graph.ring(5)
> plot(g)
> summary(g)
IGRAPH U--- 10 10 -- Ring graph
attr: name (g/c), mutual (g/x), circular (g/x)

So instead of having the number of elements, the density/intensities value is returned:


> degree.distribution(g)
[1] 0 0 1

You can easily verify this in the source code of the function:


> degree.distribution
function (graph, cumulative = FALSE, ...)
{
if (!is.igraph(graph)) {
stop("Not a graph object")
}
cs <- degree(graph, ...)
hi <- hist(cs, -1:max(cs), plot = FALSE)$intensities
if (!cumulative) {
res <- hi
}
else {
res <- rev(cumsum(rev(hi)))
}
res
}


This caused me some minor issues, but the solution was easy. I simply created a new version of the function that is using $count instead of $intensities (BTW $intensities will be deprecated in R 3.0).


count.degree.distribution <- function (graph, cumulative = FALSE, ...)
{
if (!is.igraph(graph)) {
stop("Not a graph object")
}
cs <- degree(graph, ...)
hi <- hist(cs, -1:max(cs), plot = FALSE)$count
if (!cumulative) {
res <- hi
}
else {
res <- rev(cumsum(rev(hi)))
}
res
}
Using it is identical to the original version:

> count.degree.distribution(g)
[1] 0 0 10

To leave a comment for the author, please follow the link and comment on his blog: 0xCAFEBABE.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.