1

I am trying to study the detailed code behind certain functions in igraph package in R.

For example in the code chunk below I wanted to see the function behind fast greedy clustering. igraph::cluster_fast_greedy shows the R function. It, in turn, calls on certain functions written in C, most importantly C_R_igraph_community_fastgreedy. It is clear that the following code does not do the clustering directly but calls the aforementioned C function. I want to know how to view and edit those.

I know that using trace() would allow me to edit the R function.

> igraph::cluster_fast_greedy
function (graph, merges = TRUE, modularity = TRUE, membership = TRUE, 
    weights = E(graph)$weight) 
{
    if (!is_igraph(graph)) {
        stop("Not a graph object")
    }
    if (!is.null(weights)) {
        weights <- as.numeric(weights)
    }
    on.exit(.Call(C_R_igraph_finalizer))
    res <- .Call(C_R_igraph_community_fastgreedy, graph, as.logical(merges), 
        as.logical(modularity), as.logical(membership), weights)
    if (igraph_opt("add.vertex.names") && is_named(graph)) {
        res$names <- V(graph)$name
    }
    res$algorithm <- "fast greedy"
    res$vcount <- vcount(graph)
    res$membership <- res$membership + 1
    res$merges <- res$merges + 1
    class(res) <- "communities"
    res
}
<bytecode: 0x000001d95fb78be0>
<environment: namespace:igraph>

I am posting this question at the risk of being marked duplicate, but the answers to previous similar questions have not been of much help to me. They can be found here and here.

I have checked the folder where igraph is downloaded in my computer and did not find any such file that seemed relatable.

I have also looked at their github.

Anirban_Mitra
  • 59
  • 1
  • 7
  • 1
    The C code is in the `src` folder, and the function your'e after is [here](https://github.com/cran/igraph/blob/ba26ee42b133ab383aba8acb3c23daf74324fd63/src/fast_community.c#L601). If you want to edit the code, you'll need to fork the github project, edit it and re-build it yourself. – SymbolixAU Sep 17 '21 at 01:46

2 Answers2

2

igraph is a C library, with high-level interfaces in multiple languages, including R, Python and Mathematica. There are corresponding repositories on GitHub.

The source code of igraph's C core is at https://github.com/igraph/igraph. Almost all algorithms that you can access through R/igraph are implemented here.

The code for the R interface is at https://github.com/igraph/rigraph. The latest version of the R interface will not typically use the latest released version of the C core. To find out which version of the C core is used, look in the cigraph subdirectory, which is a git submodule pointing to a specific commit of the C core.

Note that most of the glue code between the C core and the R interface is automatically generated from the functions.def file, so you will not find it in the repository.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
1

C code for R packages are found inside the src/ folder: src folder of igraph.

In your case, the function is here.

Lucas Nesi
  • 383
  • 2
  • 13