algorithm - Formal Correctness proof for Greedy solution for Wine trading (SPOJ)? -


don't me wrong posting question problem on online judge. want know how prove correctness of solution. following problem wine trading problem. says there houses in row @ unit distance , each house either wants sell or buy wine. total demand = total supply. work done in transaction amount of wine involved times distance. problem fulfill demand of houses in minimum work. proposed solution first seller(say starting right side of row) sells first buyer(the amount = min(seller,buyer))(this greedy choice) , solve remaining problem. how can 1 formally prove correct?

not sure formal want it, here intuition of proof.

to simplify note suppliers '+' , others '-'.

wlog, start supplier @ left side. have choice of buyer.

+         -    - 

suppose didn't choose first one.

+         -    - <==============> 

then have feed first 1 supplier, , reason have chosen him is closer first buyer. can @ left or @ right of first buyer.

left

+      +  -    - <==============>        <==> 

well, distance same greedy solution.

+      +  -    - <=========>        <=======> 

right

+         - +  - <==============>           <=> 

well, greedy solution better (since avoids overlapping).

+         - +  - <=========>             <==> 

in other words, greedy overlaps when necessary, , if not, benefits 2 times overlapping distance.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -