# Minimum operations of the given type required to make a complete graph

Given **N** vertex where **N** is **even**. Initially there is no edge between any of the vertices.

You are allowed to perform operation as illustrated here:

**In a single operation**, the total nodes can be divided into two groups and edge**(u, v)**can be drawn for all possible values of**u**and**v**such that**u**and**v**both belong to different groups.

The task is to find the count of minimum number of given operations required to convert these vertices into a complete graph.

**Examples:**

Input:N = 4

Output:2

Operation 1: Groups = [1, 2], [3, 4] all possible edges will be {1-4, 1-3, 2-3, 2-4}.

Operation 2: Groups = [1, 3], [2, 4] all possible edges will be {1-4, 1-3, 2-3, 2-4, 1-2, 3-4}.

Graph is now a complete graph.

Input:N = 10

Output:4

**Approach:** A graph will be called a complete graph when there will be an edge between every pair of vertices. Here the problem can be solved by divide and conquer approach. To perform the minimum number of operations, divide the vertices into two groups each with **N / 2** vertices and draw all possible edges. Now observe that we have to create edge between the vertices which are now in the same group. So we will divide them into half and put those in different groups.

These steps will be repeated until all the edges have been drawn i.e. at max **⌈ log2(N) ⌉** times as operation will divide edges into two halves of equal sizes.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return ` `// the minimum number of steps required ` `int` `minOperations(` `int` `N) ` `{ ` ` ` `double` `x = log2(N); ` ` ` ` ` `int` `ans = ` `ceil` `(x); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 10; ` ` ` `cout << minOperations(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the minimum ` ` ` `// number of steps required ` ` ` `static` `int` `minOperations(` `int` `N) ` ` ` `{ ` ` ` `double` `x = Math.log(N) / Math.log(` `2` `); ` ` ` ` ` `int` `ans = (` `int` `)(Math.ceil(x)); ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `N = ` `10` `; ` ` ` `System.out.println(minOperations(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` `from` `math ` `import` `log2, ceil ` ` ` `# Function to return the minimum ` `# number of steps required ` `def` `minOperations(N): ` ` ` `x ` `=` `log2(N) ` ` ` ` ` `ans ` `=` `ceil(x) ` ` ` ` ` `return` `ans ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `10` ` ` `print` `(minOperations(N)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum ` `// number of steps required ` `static` `int` `minOperations(` `int` `N) ` `{ ` ` ` `double` `x = Math.Log(N, 2); ` ` ` ` ` `int` `ans = (` `int` `)(Math.Ceiling(x)); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `static` `void` `Main() ` `{ ` ` ` `int` `N = 10; ` ` ` `Console.WriteLine(minOperations(N)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the minimum number ` `// of steps required ` `function` `minOperations(` `$N` `) ` `{ ` ` ` `$x` `= log(` `$N` `, 2); ` ` ` ` ` `$ans` `= ` `ceil` `(` `$x` `); ` ` ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver Code ` `$N` `= 10; ` `echo` `minOperations(` `$N` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

## Recommended Posts:

- Minimum operations of given type to make all elements of a matrix equal
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all the array elements equal
- Find the minimum number of operations required to make all array elements equal
- Count operations of the given type required to reduce N to 0
- Minimum time required to complete a work by N persons together
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Minimum number of operations required to reduce N to 1
- Minimum number operations required to convert n to m | Set-2
- Minimum gcd operations to make all array elements one
- Minimum operations to make GCD of array a multiple of k
- Minimum number of changes required to make the given array an AP
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum Increment operations to make Array unique
- Minimum operations to make counts of remainders same in an array

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.