# 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 graphattr: 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.distributionfunction (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`

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...