# 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 edge 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)

Below is the implementation of the above approach:

## C++

`// C++ program to find Edge Cover ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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)); ` `} ` ` ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find Edge Cover ` ` ` `// Function that calculates ` `// Edge Cover ` `function` `edgeCover(` `$n` `) ` `{ ` ` ` `$result` `= 0; ` ` ` ` ` `$result` `= ` `ceil` `(` `$n` `/ 2.0); ` ` ` ` ` `return` `$result` `; ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` ` ` `echo` `edgeCover(` `$n` `); ` ` ` `// This code is contributed by Raj ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Edge Coloring of a Graph
- Check if removing a given edge disconnects a graph
- Tree, Back, Edge and Cross Edges in DFS of Graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Maximize number of nodes which are not part of any edge in a Graph
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Finding minimum vertex cover size of a graph using binary search
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Program to calculate age
- Program to calculate the value of sin(x) and cos(x)
- Program to calculate value of nCr
- Program to calculate Area Of Octagon
- Program to calculate GST from original and net prices
- Program to calculate volume of Ellipsoid
- Program to calculate distance between two points in 3 D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.