Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm Design 3. We have seen two different approaches to computing the number of combinations of m out of n items C(n,m) or n choose m. Divide-and-Conquer.

Similar presentations


Presentation on theme: "Algorithm Design 3. We have seen two different approaches to computing the number of combinations of m out of n items C(n,m) or n choose m. Divide-and-Conquer."— Presentation transcript:

1 Algorithm Design 3

2 We have seen two different approaches to computing the number of combinations of m out of n items C(n,m) or n choose m. Divide-and-Conquer is clearly inferior to Dynamic Programming since the latter requires the computation of each sub-instance only once. Is there a better approach? In terms of computational load In terms of range of values For example what is the value of C(100,000, 99,998) and can this value be computed using either method described? (D&C or DynPro). If yes, what is the order of complexity? If asked to compute this value by hand, how would you proceed? Introduction

3 Developing a Better Algorithm You have seen two methods for computing binomial coefficients based on a recurrence relation. Using the closed form expression for binomial coefficients provided in the textbook create another version of this function that has a lower order of complexity than either bin or bin2. Develop, implement and test your version of this function and then derive its order of complexty. Hint: simplify the factorial terms algebraically and capture their essential computations in a single loop.

4 Another Binary Coefficient Function function bin3(n,k:integer)return long_float is val : long_float:=1.0; numdone: boolean:=false; dendone: boolean:=false; numval : integer; denval : integer; numend : integer; begin numval:=n; if (n-k)>k then numend:=n-k+1; denval:=k; else numend:=k+1; denval:=n-k; end if; if n>k and k>0 then while not(numdone and dendone) loop if (val<1.0 and not(numdone)) or dendone then val:=val*long_float(numval); numval:=numval-1; numdone:=(numval<numend); elsif not(dendone)then val:=val/long_float(denval); denval:=denval-1; dendone:=denval<1; else null; end if; end loop; end if; return val; end bin3; In this version the coefficient value, val is derived in a single loop giving a complexity of order O(n). The if-then-elsif statement selects a numerator or a denominator term as needed to keep val near unity until only numerator terms remain.

5 Summary Divide-and-Conquer method of computing combinations is impractical. Dynamic Programming is also limited. Sometimes the direct calculation or brute-force approach is preferred. The range of values in the direct calculation method is limited only by the size of the answer.


Download ppt "Algorithm Design 3. We have seen two different approaches to computing the number of combinations of m out of n items C(n,m) or n choose m. Divide-and-Conquer."

Similar presentations


Ads by Google