Program to find total number of edges in a Complete Graph - GeeksforGeeks

14 Apr, 2021
Given N number of vertices of a Graph. The task is to find the total number of edges possible in a complete graph of N vertices.Complete Graph: A Complete Graph is a graph in which every pair of vertices is connected by an edge. Examples:  
Input : N = 3
Output : Edges = 3

Input : N = 5
Output : Edges = 10
 
The total number of possible edges in a complete graph of N vertices can be given as,  
Total number of edges in a complete graph of N vertices = ( n * ( n – 1 ) ) / 2  
Example 1: Below is a complete graph with N = 5 vertices. 
The total number of edges in the above complete graph = 10 = (5)*(5-1)/2.Below is the implementation of the above idea: 
C++
Java
Python 3
C#
PHP
Javascript
// C++ implementation to find the// number of edges in a complete graph #include <bits/stdc++.h>using namespace std; // Function to find the total number of// edges in a complete graph with N verticesint totEdge(int n){    int result = 0;     result = (n * (n - 1)) / 2;     return result;} // Driver Codeint main(){    int n = 6;     cout << totEdge(n);     return 0;}
// 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 verticesstatic 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 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 verticesdef totEdge(n) :     result = (n * (n - 1)) // 2     return result             # Driver Codeif __name__ == "__main__" :     n = 6     print(totEdge(n)) # This code is contributed# by ANKITRAI1
// C# implementation to find// the number of edges in a// complete graphusing System; class GFG{     // Function to find the total// number of edges in a complete// graph with N verticesstatic int totEdge(int n){    int result = 0;     result = (n * (n - 1)) / 2;     return result;} // Driver Codepublic static void Main(){    int n = 6;    Console.Write(totEdge(n));}} // This code is contributed// by ChitraNayal
<?php// PHP 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 verticesfunction totEdge($n){    $result = 0;     $result = ($n * ($n - 1)) / 2;     return $result;} // Driver Code$n = 6;echo totEdge($n); // This code is contributed// by Shivi_Aggarwal?>
<script> // Javascript 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 verticesfunction totEdge(n){    var result = 0;     result = (n * (n - 1)) / 2;     return result;} // Driver Codevar n = 6;document.write( totEdge(n)); </script>
15
 
Kirti_Mangal
ankthon
ukasp
Shivi_Aggarwal
rutvik_56
math
school-programming
Graph
Mathematical
Mathematical
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Topological Sorting
Bellman–Ford Algorithm | DP-23
Detect Cycle in a Directed Graph
Floyd Warshall Algorithm | DP-16
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Program to find GCD or HCF of two numbers