What kind of heuristics would you consider to select your maximum subset?
What is wrong with earliest start time, shortest interval or fewest conflicts?
Consider Sij the set of activities that start after activity ai finishes and before aj starts, and write c[i,j] the size of an optimal solution on Sij.
If ak is part of the optimal solution, then
c[i,j]=c[i,k]+1+c[k,j].We brute-force the choice of ak to obtain:c[i,j]={0max{c[i,k]+c[k,j]+1:ak∈Sij}if Sij=∅otherwiseImplement a top-down memoized version of the problem. What is its running time?
How would you order the activities?
The greedy algorithm that considers jobs in increasing order of finish time is optimal.
Phrased in terms of the DP, we could say
The optimal solution of Sij could always be assumed to contain the activity in Sijwith earliest finish time.
Implement a greedy version of the interval scheduling problem
Implement a solution, prove it's optimal and find its running time.
Given an array of nonnegative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index.
What if we want to know the minimum number of jumps required to reach the last index?
At a lemon stand, each lemonade costs 5 euros. Each customer will buy one lemonade and pay with either a 5, 10, or 20 euro bill. You must determine whether it is possible to provide the correct change to each customer, knowing that you don't have any change at first.
Given a text that uses 32 symbols (26 letters, space, punctuation), how can we encode this text in bits?
How can we use the relative frequencies of the letters to reduce our encoding? How do we know when the next symbol begins?
A prefix code does not have ambiguities.
A prefix code for a set S is a function
c:S→{0,1}such that c(x) is never a prefix of y if x,y∈S are distinct.Draw the tree associated with
c(a)=11,c(e)=01,c(k)=001,c(l)=10,c(u)=000When is a binary tree a representation of a prefix code?
What is the meaning of 111010001111101000
In a binary tree, the formula becomes
ABL(T)=x∈S∑fxdepthT(x)Given an alphabet S, find a prefix code that minimizes the ABL.
Encode the string "pepper" using Huffman encoding
Huffman(S) { if |S|=2 { return tree with root and 2 leaves } else { let y and z be lowest-frequency letters in S S’ = S remove y and z from S’ insert new letter ω in S’ with fω=fy+fz T’ = Huffman(S’) T = add two children y and z to leaf ω from T’ return T } }
What's the time complexity?
Let T' be the tree obtained by removing two sibling leaves x,y, and relabelling the parent ω with frequency fx+f(y).
ABL(T′)=ABL(T)−fωHuffman code for S achives the minimum ABL of any prefix code
We need a priority queue model so we can associate shorter encodings with the most frequent letters. The following method should be implemented and run "fast".
insert(S, x)
max(S)
extract_max(S)
increase_key(S, x, k)
A heap is an array visualized as a nearly complete binary trees.
Represent the following array as a heap
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
23 | 12 | 34 | 4 | 5 | 10 | 7 | 8 | 29 | 1 |
Given a node i, what are the indices of its children? What about the index of the parent?
A max (resp. min) heap is a heap that has the additional property that a parent is always greater (resp. less) than or equal to its children.
A min heap is ideal to find the two least frequent letters!
How are these methods implemented to keep the heap invariant? Why do we sift down instead of up?
Implement Huffman's encoding. How many bits do you save on Shakespeare's 18th sonnet?