# Optimal File Merge Patterns

Given n number of sorted files, the task is to find the minimum computations done to reach Optimal Merge Pattern.

When two or more sorted files are to be merged all together to form a single file, the minimum computations done to reach this file are known as **Optimal Merge Pattern**.

If more than 2 files need to be merged then it can be done in pairs. For example, if need to merge 4 files A, B, C, D. First Merge A with B to get X1, merge X1 with C to get X2, merge X2 with D to get X3 as the output file.

If we have two files of sizes m and n, the total computation time will be m+n. Here, we use greedy strategy by merging two smallest size files among all the files present.

**Examples:**

Given 3 files with size 2, 3, 4 units.Find optimal way to combine these files

Input:n = 3, size = {2, 3, 4}

Output:14

Explanation:There are different ways to combine these files:

Method 1:Optimal method

Method 2:

Method 3:

Input:n = 6, size = {2, 3, 4, 5, 6, 7}

Output:68

Explanation:Optimal way to combine these files

**Approach:**

Node represents a file with a given size also given nodes are greater than 2

- Add all the nodes in a priority queue (Min Heap).{node.weight = file size}
- Initialize count = 0 // variable to store file computations.
- Repeat while (size of priority Queue is greater than 1)

- create a new node
- new node = pq.poll().weight+pq.poll().weight;//pq denotes priority queue, remove 1st smallest and 2nd smallest element and add their weights to get a new node
- count += node.wight
- add this new node to priority queue;
- count is the final answer

Below is the implementation of the above approach:

`// Java program to implement ` `// Optimal File Merge Pattern ` ` ` `import` `java.util.Scanner; ` `import` `java.util.PriorityQueue; ` ` ` `public` `class` `OptimalMergePatterns { ` ` ` ` ` `// Function to find minimum computation ` ` ` `static` `int` `minComputation(` `int` `size, ` `int` `files[]) ` ` ` `{ ` ` ` ` ` `// create a min heap ` ` ` `PriorityQueue<Integer> pq ` ` ` `= ` `new` `PriorityQueue<>(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < size; i++) { ` ` ` ` ` `// add sizes to priorityQueue ` ` ` `pq.add(files[i]); ` ` ` `} ` ` ` ` ` `// variable to count total computations ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `while` `(pq.size() > ` `1` `) { ` ` ` ` ` `// pop two smallest size element ` ` ` `// from the min heap ` ` ` `int` `temp = pq.poll() + pq.poll(); ` ` ` ` ` `// add the current computations ` ` ` `// with the previous one's ` ` ` `count += temp; ` ` ` ` ` `// add new combined file size ` ` ` `// to priority queue or min heap ` ` ` `pq.add(temp); ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `// no of files ` ` ` `int` `size = ` `6` `; ` ` ` ` ` `// 6 files with their sizes ` ` ` `int` `files[] = ` `new` `int` `[] { ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `, ` `7` `}; ` ` ` ` ` `// total no of computations ` ` ` `// do be done final answer ` ` ` `System.out.println(` `"Minimum Computations = "` ` ` `+ minComputation(size, files)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Minimum Computations = 68

## Recommended Posts:

- Java program to merge two files into a third file
- Java program to merge two files alternatively into third file
- Java program to merge two files alternatively into third file
- Optimal Storage on Tapes
- Optimal Page Replacement Algorithm
- Find optimal weights which can be used to weigh all the weights in the range [1, X]
- Java program to merge contents of all the files in a directory
- Delete a file using Java
- File Permissions in Java
- Java | Renaming a file
- Check if a File is hidden in Java
- Size of file on the Internet using Java
- How to play an Audio file using Java
- How to run java class file which is in different directory?
- Java | Appending String to a File

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.