The Wayback Machine - https://web.archive.org/web/20210508022727/https://www.geeksforgeeks.org/minimum-degree-of-three-nodes-forming-a-triangle-in-a-given-graph/
Skip to content
Related Articles

Related Articles

Minimum degree of three nodes forming a triangle in a given Graph
  • Last Updated : 07 May, 2021

Given an undirected graph consisting of N vertices and M edges and an array edges[][], with each row representing two vertices connected by an edge, the task is to find the minimum degree of three nodes forming a triangle in the graph. If there doesn’t exist any triangle in the graph, then print “-1”.

Examples:

Input: N = 7, Edges = [[1, 2], [1, 3], [2, 3], [1, 4], [2, 5], [2, 7], [3, 6], [3, 7]]
Output: 4
Explanation: Below is the representation of the above graph:

There are two connected triangles:



  1. One formed by nodes {1, 2, 3}. Degree of the triangle = 5.
  2. Second triangle formed by nodes {2, 3, 7}. Degree of the triangle = 4.

The minimum degree is 4.

Input: N = 6, Edges = [[1, 2], [1, 3], [2, 3], [1, 6], [3, 4], [4, 5]]
Output: 2

Approach: The given problem can be solved by finding the degree of every triplet of nodes forming a triangle and count the degree of each node in that triangle. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the minimum degree
// of a connected triangle in the graph
int minTrioDegree(int N,
                  vector<vector<int> >& Edges)
{
    // Store the degree of each node
    // in the graph
    int degree[N + 1] = { 0 };
  
    // Stores the representation of
    // graph in an adjancency matrix
    int adj[N + 1][N + 1] = { 0 };
  
    // Create the adjacency matrix and
    // count the degree of nodes
    for (int i = 0; i < Edges.size(); i++) {
  
        // u & v are the endpoint of
        // the ith edge
        int u = Edges[i][0];
        int v = Edges[i][1];
  
        // Mark both edges i.e.,
        // edge u->v and v->u
        adj[u][v] = adj[u][v] = 1;
  
        // Increment degree by 1
        // of both endnodes
        degree[u]++;
        degree[v]++;
    }
  
    // Stores the required result
    int ans = INT_MAX;
  
    // Traverse for the first node
    for (int i = 1; i <= N; i++) {
  
        // Traverse for the second node
        for (int j = i + 1; j <= N; j++) {
  
            // If there is an edge between
            // these two nodes
            if (adj[i][j] == 1) {
  
                // Traverse all possible
                // third nodes
                for (int k = j + 1;
                     k <= N; k++) {
  
                    // If there is an edge
                    // between third node
                    // and the previous two
                    if (adj[i][k] == 1
                        && adj[j][k] == 1) {
  
                        // Update ans
                        ans = min(ans,
                                  degree[i]
                                      + degree[j]
                                      + degree[k] - 6);
                    }
                }
            }
        }
    }
  
    // Return the result
    return ans == INT_MAX ? -1 : ans;
}
  
// Driver Code
int main()
{
    vector<vector<int> > Edges;
    Edges = { { 1, 2 }, { 1, 3 }, 
              { 2, 3 }, { 1, 4 }, 
              { 2, 5 }, { 2, 7 }, 
              { 3, 6 }, { 3, 7 } };
    int N = 7;
  
    cout << minTrioDegree(N, Edges);
  
    return 0;
}
Output:
4

Time Complexity: O(N3) 
Auxiliary Space: O(N2) 

My Personal Notes arrow_drop_up
Recommended Articles
Page :