How would you design an algorithm that merges n sorted arrays into one, if we can only merge two arrays at a time and we want to minimize the total number of times we move an element?
-
Assume you are given n sorted arrays of different sizes. You are allowed to merge any two arrays into a single new sorted array and proceed in this manner until only one array is left. How would you design an algorithm that achieves this task and uses minimal total number of moves of elements of the arrays. And what is an informal justification you would give why your algorithm is optimal?
-
Answer:
A greedy strategy works: always merge the two shortest arrays into a new one. Below I assume that merging two arrays with aa and b elements into one requires exactly a+b moved elements. For this model, I can actually formally prove that my answer is optimal. Visualize the order in which you merge the arrays as a binary tree. The leaves are the original arrays, each inner vertex represents a merge, and the root corresponds to the result. Now, look at any original array. How many times will we move each of its elements during the entire process? The answer is clearly equal to the depth of the leaf that corresponds to this array. Hence, we have n different arrays and we are trying to construct a binary tree with n leaves such that sum (array length) * (assigned leaf depth) is minimized. This is the well-known optimal prefix code problem which has a provably optimal solution: the http://en.wikipedia.org/wiki/Huffman_coding (For a sketch of the proof: If you knew the shape of the tree, you would always put the longest arrays into the most shallow leaves, and shortest arrays into the deepest ones. In particular, the two shortest arrays would end up at the deepest level. Without loss of generality, they can be in sons of the same vertex, which gives the greedy algorithm proposed above.)
Michal Forišek at Quora Visit the source
Related Q & A:
- How can I merge all my Google calendars into my primary one?Best solution by Quora
- How to merge two arrays of objects in angular?Best solution by Stack Overflow
- I want to change my cellphone number on Yahoo mobile. How can I do this.Best solution by Yahoo! Answers
- How can I attach more than one file at a time in Yahoo mail?Best solution by pc.net
- How do you add one more msn address at a time?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.