def Beer_algorithm(beer, pos,bBar): n = len (beer) b =beer.copy () for i in range(n - 1): if b[i] >= bBar: iCanSpare b[i] 2 * (p[i + 1] - p[i]) – bBar | if iCanSpare > 0: b[i + 1] += iCanSpare else:...



Greedy algorithm proof correctness, greedy choice property



Please proof the following algorithm's correctness by showing the algorithm exhibits greedy choice property and optimal substructure properties.



Python code:


def Beer_algorithm(beer, pos,bBar):<br>n =<br>len (beer)<br>b =beer.copy ()<br>for i in range(n - 1):<br>if b[i] >= bBar:<br>iCanSpare<br>b[i]<br>2 * (p[i + 1] - p[i]) – bBar<br>|<br>if iCanSpare > 0:<br>b[i + 1] += iCanSpare<br>else:<br>b[i + 1]<br>bBar<br>b[i] + 2 * (p[i + 1] – p[i])<br>-=<br>b[i]<br>= bBar<br>if b[n - 1] < bBar:<br>return 0<br>return 1<br>

Extracted text: def Beer_algorithm(beer, pos,bBar): n = len (beer) b =beer.copy () for i in range(n - 1): if b[i] >= bBar: iCanSpare b[i] 2 * (p[i + 1] - p[i]) – bBar | if iCanSpare > 0: b[i + 1] += iCanSpare else: b[i + 1] bBar b[i] + 2 * (p[i + 1] – p[i]) -= b[i] = bBar if b[n - 1] < bbar:="" return="" 0="" return="">

Jun 09, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here