Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Program to find total number of edges in a Complete Graph
Next article icon

Program to find total number of edges in a Complete Graph

Last Updated : 02 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// 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 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
<?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 vertices  function 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 ?> 
JavaScript
<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 vertices function totEdge(n) {     var result = 0;      result = (n * (n - 1)) / 2;      return result; }  // Driver Code var n = 6; document.write( totEdge(n));  </script> 

Output
15

Complexity Analysis:

  • Time Complexity: O(1)
  • Auxiliary Space: O(1)

Next Article
Program to find total number of edges in a Complete Graph

N

Naman_Garg
Improve
Article Tags :
  • Graph
  • Mathematical
  • DSA
  • school-programming
  • math
Practice Tags :
  • Graph
  • Mathematical

Similar Reads

    Program to find the diameter, cycles and edges of a Wheel Graph
    Wheel Graph: A Wheel graph is a graph formed by connecting a single universal vertex to all vertices of a cycle. Properties: Wheel graphs are Planar graphs.There is always a Hamiltonian cycle in the Wheel graph.Chromatic Number is 3 and 4, if n is odd and even respectively. Problem Statement: Given
    6 min read
    Find the Number of Cliques in a Graph
    In graph theory, a clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent, that is, they are connected by an edge of the graph. The number of cliques in a graph is the total number of cliques that can be found in the graph. The Mathemat
    15 min read
    Ways to Remove Edges from a Complete Graph to make Odd Edges
    Given a complete graph with N vertices, the task is to count the number of ways to remove edges such that the resulting graph has odd number of edges. Examples: Input: N = 3  Output: 4  The initial graph has 3 edges as it is a complete graph. We can remove edges (1, 2) and (1, 3) or (1, 2) and (2, 3
    4 min read
    Program to find the number of region in Planar Graph
    Given two integers V and E which represent the number of Vertices and Edges of a Planar Graph. The Task is to find the number of regions of that planar graph. Planar Graph: A planar graph is one in which no edges cross each other or a graph that can be drawn on a plane without edges crossing is call
    3 min read
    Find weight of MST in a complete graph with edge-weights either 0 or 1
    Given an undirected weighted complete graph of N vertices. There are exactly M edges having weight 1 and rest all the possible edges have weight 0. The array arr[][] gives the set of edges having weight 1. The task is to calculate the total weight of the minimum spanning tree of this graph. Examples
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences