2
|
8920 Views
|
2 Replies
|
5 Total Likes
View groups...
Share

How to compute the triangles of a graph via pattern matching?

Posted 11 years ago
2 Replies
Sort By:
Posted 9 years ago
 Define the edges as usual In[2]:= jRM = {UndirectedEdge[1, 2], UndirectedEdge[1, 3], UndirectedEdge[1, 4], UndirectedEdge[3, 4], UndirectedEdge[3, 5], UndirectedEdge[4, 5], UndirectedEdge[2, 4], UndirectedEdge[2, 3]} Out[2]= {1 <-> 2, 1 <-> 3, 1 <-> 4, 3 <-> 4, 3 <-> 5, 4 <-> 5, 2 <-> 4, 2 <-> 3} now just In[29]:= Apply[UndirectedEdge, Select[Subsets[List @@@ jRM, {3}], Length[Union[Flatten[#]]] == 3 &], {2}] Out[29]= {{1 <-> 2, 1 <-> 3, 2 <-> 3}, {1 <-> 2, 1 <-> 4, 2 <-> 4}, {1 <-> 3, 1 <-> 4, 3 <-> 4}, {3 <-> 4, 3 <-> 5, 4 <-> 5}, {3 <-> 4, 2 <-> 4, 2 <-> 3}} because of the Subset command it is not efficient even for a moderate number of edges.
Posted 9 years ago
 ... How to compute the triangles of a graph Hi. Is that what you are looking for? Two of your graphs from above: FindCycle[g1,3,All] [[All,All,1]] {{2,4,3},{1,2,4},{1,5,4}} FindCycle[g2,3,All] [[All,All,1]] {{8,9,4},{3,8,9},{5,10,3},{6,8,4},{1,6,5},{5,6,8},{5,3,8},{2,7,1},{2,6,1},{1,5,10},{1,7,10}} = = = = = = = = = = HTH :>)
Community posts can be styled and formatted using the Markdown syntax.