-1

I have a problem simlar to Huffman's encoding, I'm not sure exactly how it can be solved or if it is a reverse Huffman's encoding. But it definitely can be solved using a greedy approach.


Consider a set of length, each associated with a probability. i.e.

X={a1=(100,1/4),a2=(500,1/4),a3=(200,1/2)}

Obviously, the sum of all the probabilities = 1.

Arrange the lengths together on a line one after the other from a starting point.

For example: {a2,a1,a3} in that order from start to finish.

Define the cost of an element a_i as its the total length from the starting line to the end of this element multiplied by its probability.

So from the previous arrangement:

cost(a2) = (500)*(1/4)
cost(a1) = (500+100)*(1/4)
cost(a3) = (500+100+200)*(1/2)

Define the total cost as the sum of all costs. e.g. cost(X) = cost(a2) + cost(a1) + cost(a3). Give an algorithm that finds an arrangement that minimizes cost(X)


I've tried forming some alternative huffman trees but it doesn't work.

Sorting by probability will fail (consider X={(100,0.4),(300,0.6)}).

Sorting by length will also fail (consider X={(100,0.1),(300,0.9)}).

If anyone can help or hint towards an optimal solution algorithm, it would be great.

bli00
  • 2,215
  • 2
  • 19
  • 46
  • and the question is...? – Sani Huttunen Oct 07 '17 at 01:19
  • "Given an algorithm that finds an arrangement that minimizes cost(X)" – bli00 Oct 07 '17 at 01:19
  • I understand that that is `given`... But what is the question? – Sani Huttunen Oct 07 '17 at 01:20
  • Sorry, give* an algorithm that finds an arrangement that minimizes cost(X) – bli00 Oct 07 '17 at 01:21
  • `Consider a set of length`? What length? You need to read through your question and address all errors before you can get an answer that will satisfy you. Also it's prudent to supply some form of code that you have tried but did not work. Also please read [How To ask a Quesetion](https://stackoverflow.com/help/how-to-ask) – Sani Huttunen Oct 07 '17 at 01:27
  • I'm not sure what you are suggesting or how the problem can be any clearer. `A set of length` is a set of lengths; a collection of measurements. Please reread the question. If you are unable to decipher the the meaning or find an algorithm that's fine don't worry about it. – bli00 Oct 07 '17 at 01:32
  • A correctly asked question shouldn't have to be `deciphered`. – Sani Huttunen Oct 07 '17 at 01:40
  • 1
    Oh... and asking the same question [twice](https://stackoverflow.com/questions/46615012/length-arrangement-with-probability-and-cost) doesn't improve your chances of getting an answer.... – Sani Huttunen Oct 07 '17 at 01:45

1 Answers1

2

Consider what happens if you swap two adjacent elements. The calculations before and after the two elements are the same, so it just depends on the two elements.

Taking two elements in isolation, the costs are P1L1 + P2(L1 + L2) and P2L2 + P1(L1 + L2). If you subtract this and simplify if I have got the algebra right you want to swap 1 to first when L1/P1 < L2/P2. Check - this at least gets the right answer when L1 = 0.

So I think you want to sort the elements into increasing order of Li/Pi, because if that is not the case you can improve the answer by swapping adjacent elements.

mcdowella
  • 19,301
  • 2
  • 19
  • 25
  • I think that's right. Put another way, if you swap adjacent (L1,p1) with (L2,p2) you gain p1L2 and lose p2L1. That's positive if p1L2 > p2L1 or p1/L1 > p2/L2. – Paul Hankin Oct 07 '17 at 07:52
  • that makes sense, can't believe I didn't think of that – bli00 Oct 07 '17 at 14:16