📌  相关文章
📜  在给定图中形成三角形的三个节点的最小度数

📅  最后修改于: 2021-09-07 02:44:29             🧑  作者: Mango

给定一个由N个顶点和M 个边组成的无向图和一个数组edge[][] ,每行代表由一条边连接的两个顶点,任务是找到图中形成三角形的三个节点的最小度数。如果图中不存在任何三角形,则打印“-1”

例子:

方法:可以通过找到形成三角形的每个三元组节点的度数并计算该三角形中每个节点的度数来解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个变量,表示如ANS INT_MAX,其存储形成三角形的节点的最低的程度。
  • 从给定的边集创建一个邻接矩阵。
  • 将给定图的每个节点的度数存储在辅助数组中,例如degree[]
  • 现在,在[1, N]范围内迭代所有可能的节点(i, j, k)三元组并执行以下步骤:
    • 如果每对三元组之间都有边,则存在一个三角形。因此,将ans的值更新为ans(degree[i] + degree[j] + degree[k] – 6)的最小值。
    • 否则,继续下一次迭代。
  • 完成上述步骤后,如果ans值为INT_MAX ,则打印“-1” 。否则,将an的值打印为图中任何三角形的最小度数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum degree
// of a connected triangle in the graph
int minTrioDegree(int N,
                  vector >& 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 > 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;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the minimum degree
// of a connected triangle in the graph
static int minTrioDegree(int N,int [][]Edges)
{
    // Store the degree of each node
    // in the graph
    int degree[] = new int[N + 1];
 
    // Stores the representation of
    // graph in an adjancency matrix
    int adj[][] = new int[N + 1][N + 1];
 
    // Create the adjacency matrix and
    // count the degree of nodes
    for (int i = 0; i < Edges.length; 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 = Integer.MAX_VALUE;
 
    // 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 = Math.min(ans,
                                  degree[i]
                                      + degree[j]
                                      + degree[k] - 6);
                    }
                }
            }
        }
    }
 
    // Return the result
    return ans == Integer.MAX_VALUE ? -1 : ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int [][]Edges = { { 1, 2 }, { 1, 3 },
              { 2, 3 }, { 1, 4 },
              { 2, 5 }, { 2, 7 },
              { 3, 6 }, { 3, 7 } };
    int N = 7;
 
    System.out.print(minTrioDegree(N, Edges));
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
import sys
 
# Function to find the minimum degree
# of a connected triangle in the graph
def minTrioDegree(N, Edges):
 
    # Store the degree of each node
    # in the graph
    degree = [0] * (N+1)
 
    # Stores the representation of
    # graph in an adjancency matrix
    adj = []
 
    for i in range(0, N+1):
        temp = []
        for j in range(0, N+1):
            temp.append(0)
 
        adj.append(temp)
 
    # Create the adjacency matrix and
    # count the degree of nodes
    for i in range(len(Edges)):
 
        # u & v are the endpoint of
        # the ith edge
        u = Edges[i][0]
        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] += 1
        degree[v] += 1
 
    # Stores the required result
    ans = sys.maxsize
 
    # Traverse for the first node
    for i in range(1, N+1, 1):
 
        # Traverse for the second node
        for j in range(i + 1, N+1, 1):
 
            # If there is an edge between
            # these two nodes
            if adj[i][j] == 1:
 
                # Traverse all possible
                # third nodes
                for k in range(j + 1, N+1, 1):
 
                    # If there is an edge
                    # between third node
                    # and the previous two
                    if (adj[i][k] == 1) and (adj[j][k] == 1):
 
                        # Update ans
                        ans = min(ans, degree[i] + degree[j] + degree[k] - 6)
 
    # Return the result
    if ans == sys.maxsize:
        return -1
    return ans
 
# Driver Code
Edges = [[1, 2], [1, 3], [2, 3], [1, 4], [2, 5], [2, 7], [3, 6], [3, 7]]
N = 7
 
print(minTrioDegree(N, Edges))
 
# This code is contributed by Dharanendra L V.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
 
// Function to find the minimum degree
// of a connected triangle in the graph
static int minTrioDegree(int N, int [,]Edges)
{
   
    // Store the degree of each node
    // in the graph
    int []degree = new int[N + 1];
 
    // Stores the representation of
    // graph in an adjancency matrix
    int [,]adj = new int[N + 1, N + 1];
 
    // Create the adjacency matrix and
    // count the degree of nodes
    for (int i = 0; i < Edges.GetLength(0); 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.MaxValue;
 
    // 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 = Math.Min(ans,
                                  degree[i]
                                      + degree[j]
                                      + degree[k] - 6);
                    }
                }
            }
        }
    }
 
    // Return the result
    return ans == int.MaxValue ? -1 : ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int [,]Edges = { { 1, 2 }, { 1, 3 },
              { 2, 3 }, { 1, 4 },
              { 2, 5 }, { 2, 7 },
              { 3, 6 }, { 3, 7 } };
    int N = 7;
 
    Console.Write(minTrioDegree(N, Edges));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
4

时间复杂度: O(N 3 )
辅助空间: O(N 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live