Search code examples
rsocial-networkinggraph-theoryigraph

count cycles in network


What is the best way, or are there any ways implemented in are to count both 3 and 4 cycles in networks.

3 cycles equal connected groups of three nodes(triangles) to be calculated from one mode networks 4 cycles equal connected groups of four nodes(squares) to be calculated from two mode networks

If i have networks like this:

onemode <- read.table(text= "start end
                              1    2
                              1    3
                              4    5
                              4    6
                              5    6",header=TRUE)

twomode <- read.table(text= "typa typev
                              aa    a
                              bb    b
                              bb    a
                              aa    b",header=TRUE)

I thought

 library(igraph)
 g <- graph.data.frame(twomode)
 E(g)
 graph.motifs(g, size = 4) 

would count the number of squares in my two mode network but I dont understand the output. I thought the result would be 1


Solution

  • ?graph.motifs

    graph.motifs searches a graph for motifs of a given size and returns a numeric vector containing the number of different motifs. The order of the motifs is defined by their isomorphism class, see graph.isoclass.

    So the output of this is numeric vector where each value is the count of a certain motif(with sizes is 4 or 3) in your graph.

     graph.motifs(g,size=4)
    

    To get the total number of the motifs, you can use graph.motifs.no

    graph.motifs.no(g,size=4)
    [1] 1
    

    Which is the number of the motif 20

    which(graph.motifs(g,size=4) >0)
    [1] 20