The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
Longest Increasing Sequence Next: Minimum Weight Triangulation Up: Dynamic Programming Previous: Approximate String Matching Longest Increasing Sequence Hopefully, a pattern is emerging. Every dynamic programming solution has three components: 1. Formulate the answer as a recurrence relation or recursive algorithm. 2. Show that the number of different values of your recurrence is bounded by a (hopefully small) polynomial. 3. Specify an order of evaluation for the recurrence so you always have the partial results you need available when you need them. To see how this is done, let's see how we would develop an algorithm to find the longest monotonically increasing sequence in a sequence of n numbers. This problem arises in pattern matching on permutations, as described in Section . We distinguish an increasing sequence from a run, in that the selected elements need not be neighbors of each other. The selected elements must be in sorted order from left to right. For example, consider the sequence The longest increasing subsequence of has length 3 and is either (2,3,4) or (2,3,6). The longest increasing run is of length 2, either (2,8) or (1,6). Finding the longest increasing run in a numerical sequence is straightforward, indeed you should be able to devise a linear-time algorithm fairly easily. However, finding the longest increasing subsequence is considerably trickier. How can we identify which scattered elements to skip? To apply dynamic programming, we need to construct a recurrence computing the length of the longest sequence. To find the right recurrence, ask what information about the first n-1 elements of S, coupled with the last element , would enable you to find the answer for the entire sequence? ● The length of the longest increasing sequence in seems a useful thing to know. In fact, this will be the longest increasing sequence in S, unless extends it or another increasing sequence of the same length. Unfortunately, knowing just this length is not enough. Suppose I told you that the longest increasing sequence in was of length 5 and that . file:///E|/BOOK/BOOK2/NODE47.HTM (1 of 3) [19/1/2003 1:28:48]
Longest Increasing Sequence Will the length of the final longest increasing subsequence of S be 5 or 6? ● Therefore, we also need the length of the longest sequence that will extend. To be certain we know this, we really need the length of the longest sequence that any possible number can extend. This provides the idea around which to build a recurrence. Define to be the length of the longest sequence ending with . Verify that the following table is correct: sequence 9 5 2 8 7 3 1 6 4 length 1 1 1 2 2 2 1 3 3 predecessor - - - 2 2 3 - 6 6 The longest increasing sequence containing the nth number will be formed by appending it to the longest increasing sequence to the left of n that ends on a number smaller than . The following recurrence computes : These values define the length of the longest increasing sequence ending at each number. The length of the longest increasing subsequence of the entire permutation is given by , since the winning sequence will have to end somewhere. What is the time complexity of this algorithm? Each one of the n values of is computed by comparing against up to values to the left of it, so this analysis gives a total of time. In fact, by using dictionary data structures in a clever way, we can evaluate this recurrence in time. However, the simple recurrence would be easy to program and therefore is a good place to start. What auxiliary information will we need to store in order to reconstruct the actual sequence instead of its length? For each element , we will store the index of the element that appears immediately before in the longest increasing sequence ending at . Since all of these pointers go towards the left, it is a simple matter to start from the last value of the longest sequence and follow the pointers so as to reconstruct the other items in the sequence. Next: Minimum Weight Triangulation Up: Dynamic Programming Previous: Approximate String Matching file:///E|/BOOK/BOOK2/NODE47.HTM (2 of 3) [19/1/2003 1:28:48]
- Page 139 and 140: Dictionaries Next: Binary Search Tr
- Page 141 and 142: Binary Search Trees BinaryTreeQuery
- Page 143 and 144: Priority Queues Next: Specialized D
- Page 145 and 146: Specialized Data Structures Next: S
- Page 147 and 148: Sorting Next: Applications of Sorti
- Page 149 and 150: Applications of Sorting Figure: Con
- Page 151 and 152: Data Structures Next: Incremental I
- Page 153 and 154: Incremental Insertion Next: Divide
- Page 155 and 156: Randomization Next: Bucketing Techn
- Page 157 and 158: Randomization Next: Bucketing Techn
- Page 159 and 160: Bucketing Techniques Algorithms Mon
- Page 161 and 162: War Story: Stripping Triangulations
- Page 163 and 164: War Story: Stripping Triangulations
- Page 165 and 166: War Story: Mystery of the Pyramids
- Page 167 and 168: War Story: Mystery of the Pyramids
- Page 169 and 170: War Story: String 'em Up We were co
- Page 171 and 172: War Story: String 'em Up Figure: Su
- Page 173 and 174: Exercises Next: Implementation Chal
- Page 175 and 176: Exercises used to select the pivot.
- Page 177 and 178: Dynamic Programming Next: Fibonacci
- Page 179 and 180: Fibonacci numbers Next: The Partiti
- Page 181 and 182: Fibonacci numbers Next: The Partiti
- Page 183 and 184: The Partition Problem . What is the
- Page 185 and 186: The Partition Problem Figure: Dynam
- Page 187 and 188: Approximate String Matching Next: L
- Page 189: Approximate String Matching The val
- Page 193 and 194: Minimum Weight Triangulation Next:
- Page 195 and 196: Limitations of Dynamic Programming
- Page 197 and 198: War Story: Evolution of the Lobster
- Page 199 and 200: War Story: Evolution of the Lobster
- Page 201 and 202: War Story: What's Past is Prolog Ne
- Page 203 and 204: War Story: What's Past is Prolog th
- Page 205 and 206: War Story: Text Compression for Bar
- Page 207 and 208: War Story: Text Compression for Bar
- Page 209 and 210: Divide and Conquer Next: Fast Expon
- Page 211 and 212: Fast Exponentiation Next: Binary Se
- Page 213 and 214: Binary Search Next: Square and Othe
- Page 215 and 216: Exercises Next: Implementation Chal
- Page 217 and 218: Exercises whose denominations are ,
- Page 219 and 220: The Friendship Graph Next: Data Str
- Page 221 and 222: The Friendship Graph friendship gra
- Page 223 and 224: Data Structures for Graphs linked t
- Page 225 and 226: War Story: Getting the Graph ``Well
- Page 227 and 228: Traversing a Graph Next: Breadth-Fi
- Page 229 and 230: Breadth-First Search Next: Depth-Fi
- Page 231 and 232: Depth-First Search Next: Applicatio
- Page 233 and 234: Depth-First Search Next: Applicatio
- Page 235 and 236: Connected Components Next: Tree and
- Page 237 and 238: Two-Coloring Graphs Next: Topologic
- Page 239 and 240: Topological Sorting Next: Articulat
Longest Increasing Sequence<br />
Next: Minimum Weight Triangulation Up: Dynamic Programming Previous: Approximate String<br />
Matching<br />
Longest Increasing Sequence<br />
Hopefully, a pattern is emerging. Every dynamic programming solution has three components:<br />
1. Formulate the answer as a recurrence relation or recursive algorithm.<br />
2. Show that the number of different values of your recurrence is bounded by a (hopefully small)<br />
polynomial.<br />
3. Specify an order of evaluation for the recurrence so you always have the partial results you need<br />
available when you need them.<br />
To see how this is done, let's see how we would develop an algorithm to find the longest monotonically<br />
increasing sequence in a sequence of n numbers. This problem arises in pattern matching on<br />
permutations, as described in Section . We distinguish an increasing sequence from a run, in that the<br />
selected elements need not be neighbors of each other. <strong>The</strong> selected elements must be in sorted order<br />
from left to right. For example, consider the sequence<br />
<strong>The</strong> longest increasing subsequence of has length 3 and is either (2,3,4) or (2,3,6). <strong>The</strong> longest increasing<br />
run is of length 2, either (2,8) or (1,6).<br />
Finding the longest increasing run in a numerical sequence is straightforward, indeed you should be able<br />
to devise a linear-time algorithm fairly easily. However, finding the longest increasing subsequence is<br />
considerably trickier. How can we identify which scattered elements to skip? To apply dynamic<br />
programming, we need to construct a recurrence computing the length of the longest sequence. To find<br />
the right recurrence, ask what information about the first n-1 elements of S, coupled with the last element<br />
, would enable you to find the answer for the entire sequence?<br />
● <strong>The</strong> length of the longest increasing sequence in seems a useful thing to know. In<br />
fact, this will be the longest increasing sequence in S, unless extends it or another increasing<br />
sequence of the same length. Unfortunately, knowing just this length is not enough. Suppose I<br />
told you that the longest increasing sequence in was of length 5 and that .<br />
file:///E|/BOOK/BOOK2/NODE47.HTM (1 of 3) [19/1/2003 1:28:48]