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 their blog: 0xCAFEBABE.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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.

Sponsors

Mango solutions



plotly webpage

dominolab webpage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training

datasociety

http://www.eoda.de





ODSC

ODSC

CRC R books series





Six Sigma Online Training









Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)