给定一个整数N ,它表示顶点的数量。任务是在N个顶点的二部图中找到最大可能的边数。
二部图:
- 二分图是一个具有2组顶点的图。
- 该集合使得同一集合中的顶点之间永不共享边。
例子:
Input: N = 10
Output: 25
Both the sets will contain 5 vertices and every vertex of first set
will have an edge to every other vertex of the second set
i.e. total edges = 5 * 5 = 25
Input: N = 9
Output: 20
方法:当给定集合的每个顶点都具有另一集合的每个其他顶点的边缘时,边缘的数量将最大,即edges = m * n ,其中m和n是两个集合中的边缘的数量。为了最大化边的数量, m必须等于或尽可能接近n 。因此,可以使用以下公式计算最大边缘数:
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum number
// of edges possible in a Bipartite
// graph with N vertices
int maxEdges(int N)
{
int edges = 0;
edges = floor((N * N) / 4);
return edges;
}
// Driver code
int main()
{
int N = 5;
cout << maxEdges(N);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the maximum number
// of edges possible in a Bipartite
// graph with N vertices
public static double maxEdges(double N)
{
double edges = 0;
edges = Math.floor((N * N) / 4);
return edges;
}
// Driver code
public static void main(String[] args)
{
double N = 5;
System.out.println(maxEdges(N));
}
}
// This code is contributed by Naman_Garg.
Python3
# Python3 implementation of the approach
# Function to return the maximum number
# of edges possible in a Bipartite
# graph with N vertices
def maxEdges(N) :
edges = 0;
edges = (N * N) // 4;
return edges;
# Driver code
if __name__ == "__main__" :
N = 5;
print(maxEdges(N));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the maximum number
// of edges possible in a Bipartite
// graph with N vertices
static double maxEdges(double N)
{
double edges = 0;
edges = Math.Floor((N * N) / 4);
return edges;
}
// Driver code
static public void Main()
{
double N = 5;
Console.WriteLine(maxEdges(N));
}
}
// This code is contributed by jit_t.
PHP
Javascript
输出:
6