Algorithm analysis, recurrence relations, and dynamic programming

Algorithmanalysis,moremath,
anddynamicprogramming
Today
• Morealgorithmanalysis
• Solvingrecurrencerelations
• Introtodynamicprogramming
Algorithmanalysis
1.
2.
3.
4.
5.
Determinesizeoftheproblem
Determinebasicoperation
Determineifwehavebest/worstcaseorjustasinglecase
Setupsummationorrecurrence
Solve
Brute-forceclosestpair
double closestDistance(vector<Point> arr) {
'
double best = ∞;
'
for (int i = 0; i < arr.size(); i++) {
'
for (int j = i+1; j < arr.size(); j++) {
'
double dist = sqrt(/* ... */);
'
best = min(best, dist);
'
}
'
}
'
return best;
'
}
'
Insertionsort
def insertion_sort(arr):
'
for i in range(len(arr)):
'
element = arr[i]
'
j = i – 1
'
while j >= 0 and arr[j] > element:
'
arr[j + 1] = arr[j]
'
j = j – 1
'
arr[j + 1] = element
'
Bubblesort
double bubble(vector<int> arr) {
'
for (int i=0; i<arr.size()-1; i++) {
'
for (int j=0; j<arr.size()–1–i; i++) {
'
if (arr[j+1] < arr[j])
'
swap(arr[j], arr[j+1]);
'
}
'
}
'
}
'
Binarysearch
def exists(arr, item):
'
left = 0
'
right = len(arr)-1
'
while left <= right:
'
mid = (left + right)//2
'
if item == arr[mid]:
'
return True
'
else if item < arr[mid]:
'
right = mid – 1
'
else:
'
left = mid + 1
'
return False
'
Binarysearch
def exists(arr, item):
'
left = 0
'
right = len(arr)-1
'
while left <= right:
'
mid = (left + right)//2
'
if item == arr[mid]:
'
return True
'
else if item < arr[mid]:
'
right = mid – 1
'
else:
'
left = mid + 1
'
return False
'
Solvingrecurrencerelationsexactly
• Sometimes,itcanbevaluabletofindanexact solutionto a
recurrencerelation
• Thereareacoupleoftechniqueswecanuse
• We'llprimarilyusebackwardsubstitution
Recurrencerelations
1. x(n)=x(n-1) + 1;x(1) = 0
2. x(n)=x(n-1) + 5;x(1) = 0
3. x(n)=2*x(n-1);x(0) = 1
4. x(n)=x(n-1) +n;x(0) = 0
Dynamicprogramming
• Designedtosolveproblemsmadeupofmultiplesmaller,overlapping,
subproblems
• Wehavepreviouslyseensomeproblemslikethis
• Dynamicprogrammingaimstoavoidcomputingmoreinformation
thannecessary
Fibonaccisequence
• 1,1,2,3,5,8,13,21,34,...
• F(0)=0
• F(1)=1
• F(n)=F(n-2)+F(n-1)
Optimizationproblems
• Dynamicprogrammingisideallysuitedforsolvingoptimization
problems
• Givensomeconstraints,figureoutthe'best'possibleanswer
• Givenagroupofitemsandaweightlimit,findthebestsubsetofitemstopick
up
• Givenanumberofplacestoputfirestations,findthebestplacessothatthe
averagedistanceofpeopletothefirestationsisminimized
• Dynamicprogrammingreliesonprincipleofoptimality
• Anoptimalsolutionismadeupofoptimalsolutionstoitssubproblems
Coin-rowproblem
• Aseriesofcoinsofdifferingvaluesarelaidoutinarow
• Youarefreetopickupwhichevercoinsyouwant,aslongasyoudon't
pickuptwoadjacentcoins
• Whatisthebestsubsettopickup?
4,10,18,14,7,10,19,20,2,13,3,8
Coin-gridproblem
• Weareplacedattheupper-leftcellofagrid
• Eachgridcellmayormaynotcontainavaluablecointhatwecanpick
up
• Wecanonlymovedownorright,andwillpickupallcoinswewalk
over
• Whatisthemostvaluablepath?
Coin-gridproblemwithwalls
• Weareplacedattheupper-leftcellofagrid
• Eachgridcellmayeither:
• Contain avaluablecoin
• Beimpassible
• Beempty
• Wecanonlymovedownorright,willpickupallcoinswewalkover,
andcannotmovethroughimpassibleblocks
• Whatisthemostvaluablepath?
Memoryfunctions/memoization
• Insteadofcomputingthearray/gridofanswersaheadoftime,itcan
beusefultocomputethisinformationasneeded
• Wecanthinkofourfunctionas'remembering'previousinputvalues
andreturningrememberedanswers
• Alsoreferredtoas"memoization"
Coin-gridproblemwithwalls
• Weareplacedattheupper-leftcellofagrid
• Eachgridcellmayeither:
• Contain avaluablecoin
• Beimpassible
• Beempty
• Wecanonlymovedownorright,willpickupallcoinswewalkover,
andcannotmovethroughimpassibleblocks
• Whatisthemostvaluablepath?