4

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!

nak5120
  • 4,089
  • 4
  • 35
  • 94

1 Answers1

9

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))
nak5120
  • 4,089
  • 4
  • 35
  • 94
Ryan Haunfelder
  • 766
  • 3
  • 11
  • Thank you. So would I just do an index match to see what the labels are? – nak5120 May 31 '16 at 22:20
  • 1
    Yes, you could do something like, lapply(cl.tri,function(x){V(g)$names[x]}) before you create the data frame or, apply(df, 2, function(x){V(g)$names[x]}) after. There may be a way to get the cliques function to directly return labels but I'm not sure. – Ryan Haunfelder Jun 01 '16 at 18:09
  • This was a great start, thanks. When I tried this though an Error came up when I ran the df=data.frame(matrix(unlist(cl.tri),ncol=3,byrow=T)) I tried the first function – nak5120 Jun 01 '16 at 21:32
  • Hey Nick, can you edit your question to include a full working example? Otherwise I will just have to infer what the issue is. My response above should use V(g)$name[x] instead of V(g)$names[x]. It depends how you stored the names of your nodes but I think the igraph default is in the vertex attribute "name". – Ryan Haunfelder Jun 02 '16 at 15:06
  • That was the error! Thanks Ryan. If you want, you can put an answer down to the question I posted to receive a correct answer, or I can just delete the question....up to you – nak5120 Jun 02 '16 at 16:16
  • I think it's best to delete the other question to avoid confusion. Glad it worked out for you. – Ryan Haunfelder Jun 02 '16 at 16:52