
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum number of edges in Bipartite graph in C++
Problem statement
Given an integer N which represents the number of Vertices. The Task is to find the maximum number of edges possible in a Bipartite graph of N vertices.
Bipartite Graph
- A Bipartite graph is one which is having 2 sets of vertices.
- The set are such that the vertices in the same set will never share an edge between them.
Example
If N = 10 then there will be total 25 edges −
- Both sets will contain 5 vertices and every vertex of first set will have an edge to every other vertex of the second set
- Hence total edges will be 5 * 5 = 25
Algorithm
- The number of edges will be maximum when every vertex of a given set has an edge to every other vertex of the other set i.e. edges = m * n where m and n are the number of edges in both the sets
- in order to maximize the number of edges, m must be equal to or as close to n as possible
- Hence, the maximum number of edges can be calculated with the formula −
(n * n) / 4
Example
#include <bits/stdc++.h> using namespace std; int getMaxEdges(int n) { return floor((n * n) / 4); } int main() { int n = 7; cout << "Maximum edges = " << getMaxEdges(n) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Maximum edges = 12
- Related Articles
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph in C++
- Count number of edges in an undirected graph in C++
- C++ Program to find out the number of bridge edges in a given graph
- Maximum Bipartite Matching
- Edges and Vertices of Graph
- C++ Program to Perform Graph Coloring on Bipartite Graphs
- C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges
- Check if a given graph is Bipartite using DFS in C++ program
- C++ Program to Find All Forward Edges in a Graph
- C++ Program to Generate a Random Directed Acyclic Graph DAC for a Given Number of Edges
- C++ Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected\n
- C++ Program to Check whether Graph is a Bipartite using BFS
- C++ Program to Check whether Graph is a Bipartite using DFS
- Check if a given graph is Bipartite using DFS using C++
- Program to find the diameter, cycles and edges of a Wheel Graph in C++

Advertisements