Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Show that the greedy algorithm for making change for n cents using quarters, dimes, nickels, and pennies hasO(n)complexity measured in terms of comparisons needed.

Short Answer

Expert verified

The result is proved.

Step by step solution

01

Algorithm

Greedy algorithm

Procedure- change

(c1,c2,...,cr:valuesofdenominationsofcoins,wherec1>c2>...>cr;n:apositiveinteger).

fori:=1tordi:=0whilencidi:=dj+1n:=nci
02

Solution

The algorithm makes 1 comparison in each iteration of the while-loop (nci).

Since i can take on the values from 1 to r (for i:=1tor), i can take on r values and thus there are at most r iterations of the for-loop.

In worst-case scenario, the while-loop iterates n times (when the change consists of only cents).

The number of comparisons is then the product of the number of comparisons per iteration and the number of iterations (Note: product since the loops are nested).

Number of comparisons =1rn=rn

Thus at most rn comparisons are made (with r a constant) and rn is O(n) where r is a constant.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free