Triangles incident on a vertex (Graphs)

512 Views Asked by At

I have a project that I am doing. The specification requires specific methods on a graph class. Two of the methods requires this:

1.numberOfTrianglesIncidentToVertex, calculates and returns the number of triangles incident to vertex v. The algorithm below calculates the number of triangles incident for all graph vertices (V is the set of vertices, E is the set of edges):

          foreach v in V
             foreach pair of vertices (p, q) in AdjacencyList(v)
                  if (p, q) is in E then add 1 to triangles[v]

2.listOfTrianglesIncidentToVertex calculates and returns the list of triangles incident to vertex v. A triangle should be specified by its vertices.

I have no idea what a triangle is in regards to the vertex and graphs. Can someone give me a thorough explanation. I would prefer a more lamens/ no big words explanation. I do not understand the algorithm can you explain it to me?

1

There are 1 best solutions below

4
On

A 'triangle", in this context, is almost certainly a loop consisting of three distinct edges in the graph, e.g., edge $AB$, edge $BC$, and edge $CA$, where $A$, $B$, and $C$ are distinct nodes of the graph, and "$AB$" denotes the edge from $A$ to $B$.

You might typically represent a triangle by a triple of nodes, like $(A, B, C)$, with the understanding that $(A, B, C)$ and $(B, A, C)$ are "the same". This whole notion only works for simple graphs, in which there are no self-loops, and between any two nodes is at most one edge. It looks, from your description, as if that's the case here.