Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-013

Triangle Count

Problem

Count the number of triangles in an undirected simple graph with n nodes.

A triangle is a set of three nodes that are pairwise connected. Return the total count.

Hint

Order the triple u < v < w so each triangle is counted once.

Test cases

case 1
in: [3,[[0,1],[1,2],[2,0]]]
expected: 1
case 2
in: [4,[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]]
expected: 4
case 3
in: [5,[[0,1],[1,2],[2,0],[0,3],[3,4],[4,0]]]
expected: 2
case 4
in: [4,[[0,1],[1,2],[2,3]]]
expected: 0
Starter codePython
def count_triangles(n, edges):
    # Your code here
    pass

4 test cases · run your code against them in the editor