-1

I tried solving one problem which was claimed to be solvable using Dynamic programming only. Following is the problem. A man has total energy as H and he needs to cover distance D. He wants to cover this distance using maximum energy upto H in minimum time. He can run in 5 modes. Total distance is covered by running each km according to one of the 5 modes. These five modes are described using two arrays which are sorted

Time:-'5m 10sec', '6 m 11sec','7 m 7sec','8 m 11sec','9 m 11sec'

Energy required:-11,9,8,7,6

So my greedy solution strategy is

  1. Compute x=floor of H/D
  2. Run next km using mode which takes lowest time and requires energy<=x

    & set H=H-energy required by mode

    set D=D-1

  3. Goto step 1 till remaining distance becomes 0.

  4. Answer will be some of all times for each km.

I think this solution will work but it fails in reality. I know how to solve it using DP, but I wanted to know where it fails. I have tried many examples but not getting the feeling. I do not recall above two arrays elements exactly but they are necessarily in sorted order.

rainyday
  • 353
  • 3
  • 17
  • 1
    Why would you expect this to work? Try to prove that your algorithm should work, and you'll find a gap in your logic somewhere. – user2357112 Nov 22 '17 at 05:54
  • Well, for you algorithm you do now even need loop - the answer is (time: where max(E): E<=x)) * D. On each step you will choose the same mode because you have fixed energy for each km. How about following: let DL - distance left after selection mode. HL - energy left after selecting mode. On each step you do "Select mode that Time of mode -> min and (min mode energy over all modes)*DL >= HL". "(min mode energy over all modes)*DL >= HL" is needed to prevent ending up with no solution for cases like H=12, D=2 and first step is H=11. – Yevgeniy.Chernobrivets Nov 22 '17 at 06:13
  • Have you tried a debugger yet? – MrSmith42 Nov 22 '17 at 08:56
  • Your `x` has a wrong assumption that for each unit km, you can only choose modes with at most average of overall energy. This is not true, consider H = 8, D = 4, with two modes (1, 10), (5, 1). x is 2, so you never can choose mode 2, so you will use 40 seconds and 4 energy, while best strategy is run 3 km with mode 1 and 1 km with mode 2, giving 31 second and 8 energy in total – shole Nov 22 '17 at 09:48
  • @shole No, see my logic, I am updating both D and H on each step. So for last km H=5,D=1 and x=5, so algorithm will chose second mode for last km. – rainyday Nov 22 '17 at 13:22
  • "I think this solution will work" So there's a flaw in your logic. "I wanted to know where it fails" We don't know where the flaw is, you don't show us your logic, only the completed algorithm. And it's very obviously wrong. What makes you think it should work? We have no idea. – n. m. could be an AI Nov 22 '17 at 13:33
  • @n.m. It's not obvious to me. Can you tell where it is wrong ? Can you provide example ? I guess my solution is clear to you. – rainyday Nov 22 '17 at 14:08
  • Here's a simplified setup,. There are 3 modes. You can walk at 5 km/h for 1 burger/km or jog at 10 km/h for 3 burgers/km or cycle at 30 km/h for 4 burgers/km. You have 9 burgers and 3 km to cover. Your algo wil select jog, jog, jog (18 minutes) but the optimum is walk, cycle, cycle (16 minutes). – n. m. could be an AI Nov 22 '17 at 17:53

1 Answers1

1

The reason is simple; You haven't proven your algorithm, so you can't claim it is correct. Simple as that. Do you have any idea how many heuristic algorithms in research literature "look" correct, yet they have no proofs of correctness. Hence why they're only heuristics.

Your solution is a heuristic. For a counter example. Take H=100, D=3. Also take the modes to be with time [1,2,3,4,5] and the corresponding energies as [35,34,32,32,32].

Your initial x is floor(100/3)=33. The lowest time with energy <=33 is 3. Choose it. Update H and D such that H=67, D=2. We have floor(67/2)=33. Again, choose time 3. Update H and D such that H=34, D=1. Finally x=floor(34/1)=34 Choose time 2. So your list of times to cover the distances is [3,3,2] with total energy 32+32+34=98 and total time 8.

However, I can come up with a better solution than your greedy solution. Namely the times [1,3,4] that take 8 units of time but have 35+32+32=99 total energy. Beating your greedy solution.

Moral of the story? If you can't prove your algorithm, assume its wrong, or simply a heuristic.

AspiringMat
  • 2,161
  • 2
  • 21
  • 33