Dynamic Programming - Laughlin Lunch and Learn
-
Upload
jasmine-griffiths -
Category
Presentations & Public Speaking
-
view
36 -
download
1
Transcript of Dynamic Programming - Laughlin Lunch and Learn
HOPEFULLYSORT OF INTERESTINGALGO TALKLunch and Learn12.01.2016
Big-OBig-O
Operations for 10 “things”
Operations for 100 “things”
1 13 710 10030 700100 100001024 2^1003628800 100!
Dynamic Programming• Break a problem down into sub-problems• Store the results to avoid computing again
Two main components• Optimal substructure• Overlapping sub-problems
Fibonacci Sequence function fib(n) if n <= 1 return n return fib(n − 1) + fib(n − 2)
How many statements do we need to compute ?
Prove that can be solved in time, where .
Solve for using quadratic formula
fib(1) = 1fib(n) = fib(n-1) + fib(n-2)
an a(n-1) + a(n-2)
a2 a + 1
≈ 1.62, which satisfies a2 = 1 + 1
Top-down and Bottom-up
var m := map(0 → 0, 1 → 1) function fib(n) if key n is not in map m m[n] := fib(n − 1) + fib(n − 2) return m[n]
Top-down (Memoization) Bottom-upfunction fib(n) if n = 0 return 0 else var previousFib := 0, currentFib := 1 repeat n − 1 times // loop is skipped if n = 1 var nextFib := previousFib + currentFib previousFib := currentFib currentFib := nextFib return currentFib
Hyphenation and Justification
If a paragraph contains possible breakpoints, the number of situations that must be evaluated naively is since any two consecutive words may get split up by a break.
Knuth-Plass AlgorithmConsiders a paragraph to be a sequence of items, where each item is either a box, a glue, or a penalty specification.
Knuth-Plass Algorithm (cont.)Box: Width of the box: i, some representing the space occupied by the box.ℝGlue: Three of importance:ℝ is the ideal or normal width
is the stretchability
is the shrinkability
Penalty: Potential places to end of
line and begin another.
• Certain ‘aesthetic cost’
• High indicates poor place to break, negative indicates a good place to break
• Uses to represent width added to the line just before the break occurs
Knuth-Plass Algorithm (cont.)
Demerits: measures the badness of fit
Badness is the amount of stretching or shrinking.
Knuth-Plass Algorithm (cont.)
Calculating demeritsadjustment = available width - text width
If adjustment <= 0 adj ratio = adj ÷ avl shk
If adjustment > 0 adj ratio = adj ÷ avl str
badness = |adj ratio|3 × 100 + 0·5
Knuth-Plass Algorithm (cont.)Goals:
Minimize ∑ of each line
Dynamic programming comes into play
1) Scan line for breaking opportunities
2) At each opportunity, look at prior opportunities
3) Consider a candidate line between these two opportunities
4) Compute the demerits of this line candidate
5) Select the line candidate with the minimum demerits
Knuth-Plass Algorithm (cont.)https://github.com/bramstein/typeset/blob/master/src/linebreak.js
Greedy AlgorithmThe Knuth-Plass algorithm will almost always be more pleasing to look at, because the Greedy Algorithm may leave too much whitespace at the end of a line.
Still, the lighter runtime cost of running Greedy vs Knuth-Plass’s is why most modern browsers will use the Greedy algorithm.
SpaceLeft := LineWidthfor each Word in Text if (Width(Word) + SpaceWidth) > SpaceLeft insert line break before Word in Text SpaceLeft := LineWidth - Width(Word) else SpaceLeft := SpaceLeft - (Width(Word) + SpaceWidth)
P vs NPThe general class of questions for which some algorithm can provide an answer in polynomial time is called "class P" or just "P“
The class of questions for which an answer can be verified in polynomial time is called “NP”, which stands for "nondeterministic polynomial time.“
An answer to the P = NP question would determine whether problems that can be verified in polynomial time can also be solved in polynomial time.
Turing MachinesA Turing Machine can do everything that a real computer can do.
The Turing Machine model contains these things:
• an infinite tape as its unlimited memory
• a tape head that can read and write symbols and move around the tape
M1 = “On input string w:
1. Sweep left to right across the tape, crossing off every other 0.
2. If in stage 1 the tape contained a single 0, accept.
3. If in stage 1 the tape contained more than a single 0 and the number of 0s was odd, reject
4. Return the head to the left-hand end of the tape.
5. Go to stage 1.”
Turing Machines (cont.)M1 = “On input string w:
1. Sweep left to right across the tape, crossing off every other 0.
2. If in stage 1 the tape contained a single 0, accept.
3. If in stage 1 the tape contained more than a single 0 and the number of 0s was odd, reject
4. Return the head to the left-hand end of the tape.
5. Go to stage 1.”
M1 decides , the language consisting of all strings of 0s whose length is a power of 2.
Turing Machines and Big-OM2 = “On input string w:
1. Scan across the tape and reject if a 0 is found to the right of a 1.
2. Repeat if both 0s and 1s remain on the tape:
3. Scan across the tape, crossing off a single 0 and a single 1.
4. If 0s still remain after all the 1st have been crossed off, or if 1s still remain after all the 0s have been crossed off, reject. Else, if neither 0s nor 1s remain on the tape, accept.”
M2 decides , the language consisting of all strings of 0s followed by 1s.
Thank you