This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 6.006 Intro to Algorithms Recitation 19 April 15, 2011 Dynamic Programming: 1D Optimization Fibonacci Sequence To efficiently calculate F [ x ] , the x th element of the Fibonacci sequence, we can construct the array F from left to right (or “bottom up”). We start with F [0] = 1 and F [1] = 1 and iteratively calculate the next number in the sequence using F [ i ] = F [ i 1] + F [ i 2] until we get F [ x ] . Crazy 8’s In the game Crazy 8’s, we want to find the longest subsequence of cards where consecutive cards must have the same value, same suit, or contains at least one eight. If the cards are stored in array C , we want to keep an auxiliary score array S where S [ i ] represents the length of the longest subsequence ending with card C [ i ] . Again, we will construct the array C from left to right (or “bottom up”). We start with S [0] = 1 since the longest subsequence ending with the first card is that card itself and has a length of 1. We iteratively calculate the next score S [ i ] by scanning all previous scores and set S [ i ] to be S [ k ] + 1 where S [ k ] represents the length of the longest subsequence that card C [ i ] can be appended to. Dynamic Programming: 2D Optimization Edit Distance In the edit distance problem, we have two strings X and Y . Our goal is to find out the minimum cost of transforming string X into string Y using a set of editing operations. We will iterate through the characters of X and at each character, we have three editing operations that we can make: 1. Copy the current character of the source into the output 2. Delete the current character of the source and go onto the next character 3. Insert a new character into the output string For example, let’s look at how we can edit MAKER to DATE 6.006 Intro to Algorithms Recitation 19 April 15, 2011 Using a sequence of [Insert(D), Delete, Copy, Insert(T), Delete, Copy, Delete] , we were able to edit MAKER into DATE. Note that this sequence is not unique. We could have swapped the first Insert(D) and Delete with each other without changing the final result. A naive edit could have been deleting all of MAKER and then inserting all of DATE, making no copies in the edit sequence....
View
Full Document
 Spring '11
 byrns
 Math, Dynamic Programming, Graph Theory, Shortest path problem, shortest path, cij, editing sequence

Click to edit the document details