Program to Calculate the Edge Cover of a Graph
Given the number of vertices N of a graph. The task is to determine the Edge cover.
Edge Cover: Minimum number of edges required to cover all vertex is known as Edge Cover.
Examples:
Input : N = 5
Output : 3
Input : N = 4
Output : 2
Example 1: For N = 5 vertices,
Edge Cover is: 3 (Choosing the edges marked in Red, all of the vertices will get covered)
Example 2: For N = 8 vertices,
Edge Cover is: 4 (Choosing the edges marked in Red, all of the vertices will get covered)
Formula:
Edge Cover = ceil (no. of vertices / 2)
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int edgeCover( int n)
{
float result = 0;
result = ceil (n / 2.0);
return result;
}
int main()
{
int n = 5;
cout << edgeCover(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
static int edgeCover( int n)
{
int result = 0 ;
result = ( int )Math.ceil(( double )n / 2.0 );
return result;
}
public static void main(String args[])
{
int n = 5 ;
System.out.print(edgeCover(n));
}
}
|
Python3
import math
def edgeCover(n):
result = 0
result = math.ceil(n / 2.0 )
return result
if __name__ = = "__main__" :
n = 5
print ( int (edgeCover(n)))
|
C#
using System;
class GFG
{
static int edgeCover( int n)
{
int result = 0;
result = ( int )Math.Ceiling(( double )n / 2.0);
return result;
}
static public void Main ()
{
int n = 5;
Console.Write(edgeCover(n));
}
}
|
PHP
<?php
function edgeCover( $n )
{
$result = 0;
$result = ceil ( $n / 2.0);
return $result ;
}
$n = 5;
echo edgeCover( $n );
?>
|
Javascript
<script>
function edgeCover(n) {
var result = 0;
result = parseInt( Math.ceil(n / 2.0));
return result;
}
var n = 5;
document.write(edgeCover(n));
</script>
|
Time Complexity: O(1), As we are doing constant time operations only.
Auxiliary Space: O(1)
Last Updated :
23 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...