Im studying about graphs.
I'm trying to get all the possible combinations to create triangles in a determined graph
For example for the complete graph $K_5$, I want to get in sage or python the $10$ triangles that I can form with adjacent matrices, where $1$ is the red edge and $0$ the main diagonal and where there is no edge.
I have tried with combinations but I only get the $120$ possible combinations where in most cases they are not triangles. example:
Code: thanks to saulspatz:
import numpy as np
from itertools import combinations
triangle = [(i,j) for i in range(5) for j in range (i+1,5)]
def matrices():
for t in combinations(triangle,3):
m = np.zeros((5,5))
for c in t:
m[c]=1
yield m+m.transpose()
M = [m for m in matrices()]
My algorithm using matrices() is very inefficient, since it takes a lot of time for larger graphs


Enumerate triangles in a graph using SageMath
Let us work on the example in the question, the complete graph on five vertices.
Using
subgraph_search_iteratorOne way that works not only for triangles, but for any subgraph, is to use the built-in method
subgraph_search_iterator.Note that this list contains all the labeled triangles in G; so each triangle is there 6 times, for each starting vertex and each orientation.
Using
edgesFor the special case of triangles, one may instead iterate through edges of
G, and look for common neighbours of the endpoints of edges.Here, each triangle is there three times, once for each choice of an edge of this triangle.
Using
edgesand comparisonsGetting each triangle only once is easy if there is a total order on vertices of
G(e.g. in our example, they are integers), one can limit the enumeration above to get each triangle only once.Finally, here is the list for the example in the question: