We have n deck of cards. To shuffle i-th deck we need a(i) seconds. Our task is to give a greedy algorithm, which will merge two decks until we have one deck.

My idea is to create a priority queue. At top of the queue we have a minimum value of a(i). And then shuffle Q.top() with the next Q.top() while Q.size() > 1.

Could you help my proving, if it works, or not?

  • $\begingroup$ Yes, it will work. It's same think like Huffman Code $\endgroup$ – Wojciech Nov 15 '16 at 13:47
  • 3
    $\begingroup$ What did you try? Where did you get stuck? We're happy to help you understand the concepts but just solving exercises for you is unlikely to achieve that. Have you checked our general reference on how to prove greedy algorithms correct? Have you tried applying the methods there? If not, I suggest you work through the material there, try to apply it to your situation, and then edit the question to show us what progress you've made and where specifically you got stuck. $\endgroup$ – D.W. Nov 15 '16 at 15:36

Just like Wojciech said, there is a bijection between calculating Huffman Code and your problem. As you can see, we can treat your problem as:

Find binary rooted tree with $n$ weighted (weights $a_1,...,a_n$) leafs, such that sum of distances from leafs to root multiplied by their weight would be minimal.

This problem can be solved by calculating Huffman Code. Now let's see why we can use that algorithm to solve our problem. We will treat each deck as a leaf and each shuffling as an inner verticle. If all inner vertices have exactly 2 kids then such tree is a result of some shuffling procedures. We can easily prove that every Huffman Code Tree don't have inner verticles with only one child (because we could reduce that tree otherwise).

For further reading:

In this lecture note you can read prove that you can use your algorithm to build Huffman Code.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.