Program to find total number of edges in a Complete Graph
Last Updated :
02 Sep, 2022
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int totEdge( int n)
{
int result = 0;
result = (n * (n - 1)) / 2;
return result;
}
int main()
{
int n = 6;
cout << totEdge(n);
return 0;
}
|
Java
class GFG {
static int totEdge( int n)
{
int result = 0 ;
result = (n * (n - 1 )) / 2 ;
return result;
}
public static void main(String []args)
{
int n = 6 ;
System.out.println(totEdge(n));
}
}
|
Python 3
def totEdge(n) :
result = (n * (n - 1 )) / / 2
return result
if __name__ = = "__main__" :
n = 6
print (totEdge(n))
|
C#
using System;
class GFG
{
static int totEdge( int n)
{
int result = 0;
result = (n * (n - 1)) / 2;
return result;
}
public static void Main()
{
int n = 6;
Console.Write(totEdge(n));
}
}
|
PHP
<?php
function totEdge( $n )
{
$result = 0;
$result = ( $n * ( $n - 1)) / 2;
return $result ;
}
$n = 6;
echo totEdge( $n );
?>
|
Javascript
<script>
function totEdge(n)
{
var result = 0;
result = (n * (n - 1)) / 2;
return result;
}
var n = 6;
document.write( totEdge(n));
</script>
|
Complexity Analysis:
- Time Complexity: O(1)
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...