计算图的边缘覆盖的程序
给定图的顶点数 N。任务是确定边缘覆盖。
边覆盖:覆盖所有顶点所需的最小边数称为边覆盖。
例子:
Input : N = 5
Output : 3
Input : N = 4
Output : 2
示例 1:对于 N = 5 个顶点,
边缘覆盖为:3(选择红色标记的边缘,所有顶点都将被覆盖)
示例 2:对于 N = 8 个顶点,
边缘覆盖为:4(选择红色标记的边缘,所有顶点都将被覆盖)
公式:
Edge Cover = ceil (no. of vertices / 2)
下面是上述方法的实现:
C++
// C++ program to find Edge Cover
#include
using namespace std;
// Function that calculates Edge Cover
int edgeCover(int n)
{
float result = 0;
result = ceil(n / 2.0);
return result;
}
// Driver Code
int main()
{
int n = 5;
cout << edgeCover(n);
return 0;
}
Java
// Java program to find Edge Cover
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
// Function that calculates Edge Cover
static int edgeCover(int n)
{
int result = 0;
result = (int)Math.ceil((double)n / 2.0);
return result;
}
// Driver Code
public static void main(String args[])
{
int n = 5;
System.out.print(edgeCover(n));
}
}
Python3
# Python 3 implementation of the above approach.
import math
# Function that calculates Edge Cover
def edgeCover(n):
result = 0
result = math.ceil(n / 2.0)
return result
# Driver code
if __name__ == "__main__" :
n = 5
print(int(edgeCover(n)))
# this code is contributed by Naman_Garg
C#
// C# program to find Edge Cover
using System;
class GFG
{
// Function that calculates Edge Cover
static int edgeCover(int n)
{
int result = 0;
result = (int)Math.Ceiling((double)n / 2.0);
return result;
}
// Driver Code
static public void Main ()
{
int n = 5;
Console.Write(edgeCover(n));
}
}
// This code is contributed by Raj
PHP
Javascript
输出:
3