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 ?
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller pactasuntservanda. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $5.40. You're not tied to anything after your purchase.