📜  二分图中最大边数

📅  最后修改于: 2021-05-07 01:46:04             🧑  作者: Mango

给定一个整数N ,它表示顶点的数量。任务是在N个顶点的二部图中找到最大可能的边数。
二部图:

  1. 二分图是一个具有2组顶点的图。
  2. 该集合使得同一集合中的顶点之间永不共享边。

例子:

方法:当给定集合的每个顶点都具有另一集合的每个其他顶点的边缘时,边缘的数量将最大,即edges = m * n ,其中mn是两个集合中的边缘的数量。为了最大化边的数量, 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