给定一个图的N个顶点。任务是在N个顶点的完整图中找到可能的边总数。
完整图: “完整图”是其中每对顶点通过一条边连接的图。
例子:
Input : N = 3
Output : Edges = 3
Input : N = 5
Output : Edges = 10
在N个顶点的完整图中,可能的边总数为:
Total number of edges in a complete graph of N vertices = ( n * ( n – 1 ) ) / 2
示例1:下面是一个完整的图形,其中N = 5个顶点。
上面完整图中的边总数= 10 =(5)*(5-1)/ 2。
下面是上述想法的实现:
C++
// C++ implementation to find the
// number of edges in a complete graph
#include
using namespace std;
// Function to find the total number of
// edges in a complete graph with N vertices
int totEdge(int n)
{
int result = 0;
result = (n * (n - 1)) / 2;
return result;
}
// Driver Code
int main()
{
int n = 6;
cout << totEdge(n);
return 0;
}
Java
// Java implementation to find the
// number of edges in a complete graph
class GFG {
// Function to find the total number of
// edges in a complete graph with N vertices
static int totEdge(int n)
{
int result = 0;
result = (n * (n - 1)) / 2;
return result;
}
// Driver Code
public static void main(String []args)
{
int n = 6;
System.out.println(totEdge(n));
}
}
Python 3
# Python 3 implementation to
# find the number of edges
# in a complete graph
# Function to find the total
# number of edges in a complete
# graph with N vertices
def totEdge(n) :
result = (n * (n - 1)) // 2
return result
# Driver Code
if __name__ == "__main__" :
n = 6
print(totEdge(n))
# This code is contributed
# by ANKITRAI1
C#
// C# implementation to find
// the number of edges in a
// complete graph
using System;
class GFG
{
// Function to find the total
// number of edges in a complete
// graph with N vertices
static int totEdge(int n)
{
int result = 0;
result = (n * (n - 1)) / 2;
return result;
}
// Driver Code
public static void Main()
{
int n = 6;
Console.Write(totEdge(n));
}
}
// This code is contributed
// by ChitraNayal
PHP
Javascript
输出:
15