Search code examples
rigraphtriangle-count

Extract All Triangles in Igraph with Labels


I have created an igraph with 1000 edges. My goal is to extract all the triangles found in that igraph but to include the label rather than just then number. I also want it to be in a dataframe form that has 3 columns (one for each node of the triangle)

I have tried simply calling:

triangles(graph)

and that gives a list back with the names all in one column:

+ 28431/204 vertices, named:
    [1] node_a                                                
    [2] node_b                   
    [3] node_c 
    [4] node_a                                                
    [5] node_b                   
    [6] node_d                 
    [7] node_a                                                
    [8] node_b                   
    [9] node_e                              
   [10] node_a                                                
+ ... omitted several vertices

When I try:

adjacent.triangles(graph)

it returns all the numbers but not the names of the nodes:

[1]  15 103  45 121 152  78 325 325   3  35  90   0 488 283   3   0 325 325 325 325  78  21 190   3
[25] 133   0  47 167 167   6   3 325 505 415   0  36  78 325  78  78  90   6 206   6  36   0  78  49

I need to find a function through igraph that can give me the triangles in the following format:

COL1         COL2        COL3
node_a       node_b      node_c
node_a       node_b      node_d
node_a       node_b      node_e
node_f       node_g      node_h

Any help would be great, thanks!


Solution

  • You can use the clique function (assuming g is your graph),

    cl.tri=cliques(g,min=3,max=3)
    

    to find the cliques of size three (triangles) and then assemble them into a dataframe by,

    df<-lapply(cl.tri,function(x){V(g)$name[x]})
    
    df2=data.frame(matrix(unlist(df),ncol=3,byrow=T))