Algorithms
We have to learn to know, apply and analyse algorithmic techniques.
Recap: Asymptotic notation
The formal definitions
is asymptotically at most .
is asymptotically at least .
is asymptotically equal to .
is asymptotically strictly smaller than .
is asymptotically strictly larger than .
Recurrent relations can be solved using substitution, inspection of the recursion tree or the master theorem.
Using substitutions
Given a recurrent relation , make a guess what the solution could be.
Prove using induction (prove the base case, set up the induction hypothesis and prove the step) that the guess is
correct.
Master Theorem
The master theorem provides a solution to recurrent relations of the form for constants
and and asymptotically positive.
Is exponential? Then surely you will get .
Write as .
Calculate .
Case 1: If , then .
Case 2: If and , then .
Case 3: If , then .
Algorithmic techniques
Some problems one might get faced to:
, Optimization problem: Find the best/optimal solution for a certain problem.
Construction problem: In which way can one solve the problem.
Decision problem: Is it possible to solve the problem?
Divide and conquer
Divide-and-conquer is a pattern to specify how to break up a computation, until the problem becomes simple enough to be
solved directly, and then combine. It involves three steps:
1. Divide: split in subproblems (which are smaller instances of the same problem).
2. Conquer: solve the subproblems recursively.
Base case: when the subproblem is small enough, just solve it in a straightforward manner.
3. Combine: combine the subsolutions to form the solution for the original problem.
MergeSort for example (as seen in the course datastructures) closely follows this paradigm.
A recurrence for the running time of a divide-and-conquer algorithm falls out from the three steps basic paradigm, in which
if the problem size is small enough, we say for some constant , the straightforward solution takes .
( and denote time to divide and combine respectively.)
Correctness: Induction
Prove that the base case is correct. Then the induction hypothesis is: the algorithm works for all smaller input.
Induction step:
The sub-problems have smaller inputs and hence are being solved correctly.
Sub-solutions are being combined correctly.
Dynamic programming
Helps to prevent recurring calculations, especially with recurrent relations. Idea is not to calculate something for the
second time. One could describe DP as "careful" brute-force. Two possible implementations:
Memoization: Technique of caching and reusing previously computed results.
"Classical" DP / bottom-up DP: Filling the entries of the cache/array, until the target value has been reached.
You typically get polynomial time.
, Example: Fibonacci
The "naive" recursive approach to calculate a Fibonacci number, which is
fib(n):
if n "== 0: return 0
if n "== 1: return 1
else: return fib(n-1) + fib(n-2)
has recurrent relation .
Instead we could use a memoized DP algorithm, which looks like the following pseudo code:
memo = {} "/* Start with empty dictionary "*/
fib(n):
if n in memo: return memo[n]
if n "== 0: f = 0
if n "== 1: f = 1
else: f = fib(n-1) + fib(n-2)
memo[n] = f
return f
For all , fib(k) only recurses the first time it's called.
Algorithm design
Identify the sequence of problems. Identify one last choice, the top-choice, which leads to a splitting in one or more
subproblems. You show that the optimal solution is build out of the solutions of one or more subproblems.
Optimality principle
The optimality principle is the basic principle of DP. We should show that the optimal subproblem will be used for the total
problem and thus may be used to find the final solution.
For every option for the top-choice, we should look at which subproblem's solution will be part of the total problem
and why this is the case.
Example: Knapsack problem
One has product with value and weight , and has a maximum weight . What's the subset of product with highest
total value and maximum weight ?
Voordelen van het kopen van samenvattingen bij Stuvia op een rij:
Verzekerd van kwaliteit door reviews
Stuvia-klanten hebben meer dan 700.000 samenvattingen beoordeeld. Zo weet je zeker dat je de beste documenten koopt!
Snel en makkelijk kopen
Je betaalt supersnel en eenmalig met iDeal, creditcard of Stuvia-tegoed voor de samenvatting. Zonder lidmaatschap.
Focus op de essentie
Samenvattingen worden geschreven voor en door anderen. Daarom zijn de samenvattingen altijd betrouwbaar en actueel. Zo kom je snel tot de kern!
Veelgestelde vragen
Wat krijg ik als ik dit document koop?
Je krijgt een PDF, die direct beschikbaar is na je aankoop. Het gekochte document is altijd, overal en oneindig toegankelijk via je profiel.
Tevredenheidsgarantie: hoe werkt dat?
Onze tevredenheidsgarantie zorgt ervoor dat je altijd een studiedocument vindt dat goed bij je past. Je vult een formulier in en onze klantenservice regelt de rest.
Van wie koop ik deze samenvatting?
Stuvia is een marktplaats, je koop dit document dus niet van ons, maar van verkoper pactasuntservanda. Stuvia faciliteert de betaling aan de verkoper.
Zit ik meteen vast aan een abonnement?
Nee, je koopt alleen deze samenvatting voor €5,08. Je zit daarna nergens aan vast.