📜  二部图中的最大边数

📅  最后修改于: 2021-10-25 05:00:11             🧑  作者: Mango

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

  1. 二部图是具有 2 组顶点的图。
  2. 该集合使得同一集合中的顶点永远不会在它们之间共享边。

例子:

方法:当给定集合的每个顶点与另一个集合的每个其他顶点都有一条边时,边的数量将是最大的,即边 = 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程