# Number of Triangles in Directed and Undirected Graphs

Given a Graph, count number of triangles in it. The graph is can be directed or undirected.

Example:

Input: digraph[V][V] = { {0, 0, 1, 0}, {1, 0, 0, 1}, {0, 1, 0, 0}, {0, 0, 1, 0} }; Output: 2 Give adjacency matrix represents following directed graph.

We have discussed a method based on graph trace that works for undirected graphs. In this post a new method is discussed with that is simpler and works for both directed and undirected graphs.

The idea is to use three nested loops to consider every triplet (i, j, k) and check for the above condition (there is an edge from i to j, j to k and k to i)

However in an **undirected graph**, the triplet (i, j, k) can be permuted to give six combination (See previous post for details). Hence we divide the total count by 6 to get the actual number of triangles.

In case of **directed graph**, the number of permutation would be 3 (as order of nodes becomes relevant). Hence in this case the total number of triangles will be obtained by dividing total count by 3. For example consider the directed graph given below

Following is the implementation.

## C/C++

`// C++ program to count triangles ` `// in a graph. The program is for ` `// adjacency matrix representation ` `// of the graph. ` `#include<bits/stdc++.h> ` ` ` `// Number of vertices in the graph ` `#define V 4 ` ` ` `using` `namespace` `std; ` ` ` `// function to calculate the ` `// number of triangles in a ` `// simple directed/undirected ` `// graph. isDirected is true if ` `// the graph is directed, its ` `// false otherwise ` `int` `countTriangle(` `int` `graph[V][V], ` ` ` `bool` `isDirected) ` `{ ` ` ` `// Initialize result ` ` ` `int` `count_Triangle = 0; ` ` ` ` ` `// Consider every possible ` ` ` `// triplet of edges in graph ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < V; j++) ` ` ` `{ ` ` ` `for` `(` `int` `k = 0; k < V; k++) ` ` ` `{ ` ` ` `// check the triplet if ` ` ` `// it satisfies the condition ` ` ` `if` `(graph[i][j] && graph[j][k] ` ` ` `&& graph[k][i]) ` ` ` `count_Triangle++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// if graph is directed , ` ` ` `// division is done by 3, ` ` ` `// else division by 6 is done ` ` ` `isDirected? count_Triangle /= 3 : ` ` ` `count_Triangle /= 6; ` ` ` ` ` `return` `count_Triangle; ` `} ` ` ` `//driver function to check the program ` `int` `main() ` `{ ` ` ` `// Create adjacency matrix ` ` ` `// of an undirected graph ` ` ` `int` `graph[][V] = { {0, 1, 1, 0}, ` ` ` `{1, 0, 1, 1}, ` ` ` `{1, 1, 0, 1}, ` ` ` `{0, 1, 1, 0} ` ` ` `}; ` ` ` ` ` `// Create adjacency matrix ` ` ` `// of a directed graph ` ` ` `int` `digraph[][V] = { {0, 0, 1, 0}, ` ` ` `{1, 0, 0, 1}, ` ` ` `{0, 1, 0, 0}, ` ` ` `{0, 0, 1, 0} ` ` ` `}; ` ` ` ` ` `cout << ` `"The Number of triangles in undirected graph : "` ` ` `<< countTriangle(graph, ` `false` `); ` ` ` `cout << ` `"\n\nThe Number of triangles in directed graph : "` ` ` `<< countTriangle(digraph, ` `true` `); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count triangles ` `// in a graph. The program is ` `// for adjacency matrix ` `// representation of the graph. ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Number of vertices in the graph ` ` ` `int` `V = ` `4` `; ` ` ` ` ` `// function to calculate the number ` ` ` `// of triangles in a simple ` ` ` `// directed/undirected graph. isDirected ` ` ` `// is true if the graph is directed, ` ` ` `// its false otherwise. ` ` ` `int` `countTriangle(` `int` `graph[][], ` ` ` `boolean` `isDirected) ` ` ` `{ ` ` ` `// Initialize result ` ` ` `int` `count_Triangle = ` `0` `; ` ` ` ` ` `// Consider every possible ` ` ` `// triplet of edges in graph ` ` ` `for` `(` `int` `i = ` `0` `; i < V; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < V; j++) ` ` ` `{ ` ` ` `for` `(` `int` `k=` `0` `; k<V; k++) ` ` ` `{ ` ` ` `// check the triplet if it ` ` ` `// satisfies the condition ` ` ` `if` `(graph[i][j] == ` `1` `&& ` ` ` `graph[j][k] == ` `1` `&& ` ` ` `graph[k][i] == ` `1` `) ` ` ` `count_Triangle++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// if graph is directed , division ` ` ` `// is done by 3 else division ` ` ` `// by 6 is done ` ` ` `if` `(isDirected == ` `true` `) ` ` ` `{ ` ` ` `count_Triangle /= ` `3` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `count_Triangle /= ` `6` `; ` ` ` `} ` ` ` `return` `count_Triangle; ` ` ` `} ` ` ` ` ` `// driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` ` ` `// Create adjacency matrix ` ` ` `// of an undirected graph ` ` ` `int` `graph[][] = {{` `0` `, ` `1` `, ` `1` `, ` `0` `}, ` ` ` `{` `1` `, ` `0` `, ` `1` `, ` `1` `}, ` ` ` `{` `1` `, ` `1` `, ` `0` `, ` `1` `}, ` ` ` `{` `0` `, ` `1` `, ` `1` `, ` `0` `} ` ` ` `}; ` ` ` ` ` `// Create adjacency matrix ` ` ` `// of a directed graph ` ` ` `int` `digraph[][] = { {` `0` `, ` `0` `, ` `1` `, ` `0` `}, ` ` ` `{` `1` `, ` `0` `, ` `0` `, ` `1` `}, ` ` ` `{` `0` `, ` `1` `, ` `0` `, ` `0` `}, ` ` ` `{` `0` `, ` `0` `, ` `1` `, ` `0` `} ` ` ` `}; ` ` ` ` ` `System.out.println(` `"The Number of triangles "` `+ ` ` ` `"in undirected graph : "` `+ ` ` ` `countTriangle(graph, ` `false` `)); ` ` ` ` ` `System.out.println(` `"\n\nThe Number of triangles"` `+ ` ` ` `" in directed graph : "` `+ ` ` ` `countTriangle(digraph, ` `true` `)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Anshika Goyal. ` |

*chevron_right*

*filter_none*

## Python

`# Python program to count triangles in a graph. The program is ` `# for adjacency matrix representation of the graph. ` ` ` ` ` `# function to calculate the number of triangles in a simple ` `# directed/undirected graph. ` `# isDirected is true if the graph is directed, its false otherwise ` `def` `countTriangle(g, isDirected): ` ` ` `nodes ` `=` `len` `(g) ` ` ` `count_Triangle ` `=` `0` `#Initialize result ` ` ` `# Consider every possible triplet of edges in graph ` ` ` `for` `i ` `in` `range` `(nodes): ` ` ` `for` `j ` `in` `range` `(nodes): ` ` ` `for` `k ` `in` `range` `(nodes): ` ` ` `# check the triplet if it satisfies the condition ` ` ` `if` `( i!` `=` `j ` `and` `i !` `=` `k ` `and` `j !` `=` `k ` `and` ` ` `g[i][j] ` `and` `g[j][k] ` `and` `g[k][i]): ` ` ` `count_Triangle ` `+` `=` `1` ` ` `# if graph is directed , division is done by 3 ` ` ` `# else division by 6 is done ` ` ` `return` `count_Triangle` `/` `3` `if` `isDirected ` `else` `count_Triangle` `/` `6` ` ` `# Create adjacency matrix of an undirected graph ` `graph ` `=` `[[` `0` `, ` `1` `, ` `1` `, ` `0` `], ` ` ` `[` `1` `, ` `0` `, ` `1` `, ` `1` `], ` ` ` `[` `1` `, ` `1` `, ` `0` `, ` `1` `], ` ` ` `[` `0` `, ` `1` `, ` `1` `, ` `0` `]] ` `# Create adjacency matrix of a directed graph ` `digraph ` `=` `[[` `0` `, ` `0` `, ` `1` `, ` `0` `], ` ` ` `[` `1` `, ` `0` `, ` `0` `, ` `1` `], ` ` ` `[` `0` `, ` `1` `, ` `0` `, ` `0` `], ` ` ` `[` `0` `, ` `0` `, ` `1` `, ` `0` `]] ` ` ` `print` `(` `"The Number of triangles in undirected graph : %d"` `%` `countTriangle(graph, ` `False` `)) ` ` ` `print` `(` `"The Number of triangles in directed graph : %d"` `%` `countTriangle(digraph, ` `True` `)) ` ` ` `# This code is contributed by Neelam Yadav ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count triangles in a graph. ` `// The program is for adjacency matrix ` `// representation of the graph. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Number of vertices in the graph ` ` ` `const` `int` `V = 4; ` ` ` ` ` `// function to calculate the ` ` ` `// number of triangles in a ` ` ` `// simple directed/undirected ` ` ` `// graph. isDirected is true if ` ` ` `// the graph is directed, its ` ` ` `// false otherwise ` ` ` `static` `int` `countTriangle(` `int` `[,] graph, ` ` ` `bool` `isDirected) ` ` ` `{ ` ` ` `// Initialize result ` ` ` `int` `count_Triangle = 0; ` ` ` ` ` `// Consider every possible ` ` ` `// triplet of edges in graph ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < V; j++) ` ` ` `{ ` ` ` `for` `(` `int` `k = 0; k < V; k++) ` ` ` `{ ` ` ` `// check the triplet if ` ` ` `// it satisfies the condition ` ` ` `if` `(graph[i,j] != 0 && ` ` ` `graph[j,k] != 0 && ` ` ` `graph[k,i] != 0 ) ` ` ` `count_Triangle++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// if graph is directed , ` ` ` `// division is done by 3, ` ` ` `// else division by 6 is done ` ` ` `if` `(isDirected != ` `false` `) ` ` ` `count_Triangle = ` ` ` `count_Triangle / 3 ; ` ` ` `else` ` ` `count_Triangle = ` ` ` `count_Triangle / 6; ` ` ` ` ` `return` `count_Triangle; ` ` ` `} ` ` ` ` ` `// Driver function to check the program ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `// Create adjacency matrix ` ` ` `// of an undirected graph ` ` ` `int` `[,] graph = ` `new` `int` `[4,4] { ` ` ` `{0, 1, 1, 0}, ` ` ` `{1, 0, 1, 1}, ` ` ` `{1, 1, 0, 1}, ` ` ` `{0, 1, 1, 0} ` ` ` `}; ` ` ` ` ` `// Create adjacency matrix ` ` ` `// of a directed graph ` ` ` `int` `[,] digraph = ` `new` `int` `[4,4] { ` ` ` `{0, 0, 1, 0}, ` ` ` `{1, 0, 0, 1}, ` ` ` `{0, 1, 0, 0}, ` ` ` `{0, 0, 1, 0} ` ` ` `}; ` ` ` ` ` ` ` ` ` `Console.Write(` `"The Number of triangles"` ` ` `+ ` `" in undirected graph : "` ` ` `+ countTriangle(graph, ` `false` `)); ` ` ` ` ` `Console.Write(` `"\n\nThe Number of "` ` ` `+ ` `"triangles in directed graph : "` ` ` `+ countTriangle(digraph, ` `true` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count triangles ` `// in a graph. The program is for ` `// adjacency matrix representation ` `// of the graph. ` ` ` `// Number of vertices in the graph ` `$V` `= 4; ` ` ` `// function to calculate the ` `// number of triangles in a ` `// simple directed/undirected ` `// graph. isDirected is true if ` `// the graph is directed, its ` `// false otherwise ` `function` `countTriangle(` `$graph` `, ` ` ` `$isDirected` `) ` `{ ` ` ` `global` `$V` `; ` ` ` ` ` `// Initialize result ` ` ` `$count_Triangle` `= 0; ` ` ` ` ` `// Consider every possible ` ` ` `// triplet of edges in graph ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$V` `; ` `$i` `++) ` ` ` `{ ` ` ` `for` `(` `$j` `= 0; ` `$j` `< ` `$V` `; ` `$j` `++) ` ` ` `{ ` ` ` `for` `(` `$k` `= 0; ` `$k` `< ` `$V` `; ` `$k` `++) ` ` ` `{ ` ` ` ` ` `// check the triplet if ` ` ` `// it satisfies the condition ` ` ` `if` `(` `$graph` `[` `$i` `][` `$j` `] ` `and` `$graph` `[` `$j` `][` `$k` `] ` ` ` `and` `$graph` `[` `$k` `][` `$i` `]) ` ` ` `$count_Triangle` `++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// if graph is directed , ` ` ` `// division is done by 3, ` ` ` `// else division by 6 is done ` ` ` `$isDirected` `? ` `$count_Triangle` `/= 3 : ` ` ` `$count_Triangle` `/= 6; ` ` ` ` ` `return` `$count_Triangle` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `// Create adjacency matrix ` ` ` `// of an undirected graph ` ` ` `$graph` `= ` `array` `(` `array` `(0, 1, 1, 0), ` ` ` `array` `(1, 0, 1, 1), ` ` ` `array` `(1, 1, 0, 1), ` ` ` `array` `(0, 1, 1, 0)); ` ` ` ` ` `// Create adjacency matrix ` ` ` `// of a directed graph ` ` ` `$digraph` `= ` `array` `(` `array` `(0, 0, 1, 0), ` ` ` `array` `(1, 0, 0, 1), ` ` ` `array` `(0, 1, 0, 0), ` ` ` `array` `(0, 0, 1, 0)); ` ` ` ` ` `echo` `"The Number of triangles in undirected graph : "` ` ` `, countTriangle(` `$graph` `, false); ` ` ` `echo` `"\nThe Number of triangles in directed graph : "` ` ` `, countTriangle(` `$digraph` `, true); ` ` ` `// This code is contributed by anuj_67 ` `?> ` |

*chevron_right*

*filter_none*

Output:

The Number of triangles in undirected graph : 2 The Number of triangles in directed graph : 2

**Comparison of this approach with previous approach:**

Advantages:

- No need to calculate Trace.
- Matrix- multiplication is not required.
- Auxiliary matrices are not required hence optimized in space.
- Works for directed graphs.

Disadvantages:

- The time complexity is O(n
^{3}) and can’t be reduced any further.

This article is contributed by **Ashutosh Kumar**. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Number of Triangles in an Undirected Graph
- Conversion of an Undirected Graph to a Directed Euler Circuit
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Count the number of possible triangles
- Number of triangles after N moves
- Number of triangles that can be formed with given N points
- Count number of unique Triangles using STL | Set 1 (Using set)
- Number of Isosceles triangles in a binary tree
- Number of triangles possible with given lengths of sticks which are powers of 2
- Count the total number of triangles after Nth operation
- Number of triangles formed from a set of points on three lines
- Number of possible Triangles in a Cartesian coordinate system
- Count number of triangles possible for the given sides range
- Number of shortest paths in an unweighted and directed graph
- Find the number of paths of length K in a directed graph
- Number of Triangles that can be formed given a set of lines in Euclidean Plane
- Count number of edges in an undirected graph
- Finding the number of triangles amongst horizontal and vertical line segments
- Count the number of Prime Cliques in an undirected graph