23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

There are few algorithmic techniques that can take problems that seem to require<br />

exponential time <strong>and</strong> produce polynomial-time algorithms to solve them. Dynamic<br />

programm<strong>in</strong>g is one such technique. In addition, the algorithms that result from<br />

applications of the dynamic programm<strong>in</strong>g technique are usually quite simple—<br />

often need<strong>in</strong>g little more than a few l<strong>in</strong>es of code to describe some nested loops for<br />

fill<strong>in</strong>g <strong>in</strong> a table.<br />

The Components of a Dynamic Programm<strong>in</strong>g Solution<br />

The dynamic programm<strong>in</strong>g technique is used primarily for optimization<br />

problems, where we wish to f<strong>in</strong>d the "best" way of do<strong>in</strong>g someth<strong>in</strong>g. Often the<br />

number of different ways of do<strong>in</strong>g that "someth<strong>in</strong>g" is exponential, so a bruteforce<br />

search for the best is computationally <strong>in</strong>feasible for all but the smallest<br />

problem sizes. We can apply the dynamic programm<strong>in</strong>g technique <strong>in</strong> such<br />

situations, however, if the problem has a certa<strong>in</strong> amount of structure that we can<br />

exploit. This structure <strong>in</strong>volves the follow<strong>in</strong>g three components:<br />

Simple Subproblems: There has to be some way of repeatedly break<strong>in</strong>g the<br />

global optimization problem <strong>in</strong>to subproblems. Moreover, there should be a<br />

simple way of def<strong>in</strong><strong>in</strong>g subproblems with just a few <strong>in</strong>dices, like i, j, k, <strong>and</strong> so on.<br />

Subproblem Optimization: An optimal solution to the global problem must be a<br />

composition of optimal subproblem solutions. We should not be able to f<strong>in</strong>d a<br />

globally optimal solution that conta<strong>in</strong>s suboptimal subproblems.<br />

Subproblem Overlap: Optimal solutions to unrelated subproblems can conta<strong>in</strong><br />

subproblems <strong>in</strong> common.<br />

Hav<strong>in</strong>g given the general components of a dynamic programm<strong>in</strong>g algorithm, we<br />

next show how to apply it to the longest common subsequence problem.<br />

12.5.3 Apply<strong>in</strong>g Dynamic Programm<strong>in</strong>g to the LCS<br />

Problem<br />

We can solve the longest common subsequence problem much faster than<br />

exponential time us<strong>in</strong>g the dynamic programm<strong>in</strong>g technique. As mentioned above,<br />

one of the key components of the dynamic programm<strong>in</strong>g technique is the def<strong>in</strong>ition<br />

of simple subproblems that satisfy the subproblem optimization <strong>and</strong> subproblem<br />

overlap properties.<br />

Recall that <strong>in</strong> the LCS problem, we are given two character str<strong>in</strong>gs, X <strong>and</strong> Y, of<br />

length n <strong>and</strong> m, respectively, <strong>and</strong> are asked to f<strong>in</strong>d a longest str<strong>in</strong>g S that is a<br />

subsequence of both X <strong>and</strong> Y. S<strong>in</strong>ce X <strong>and</strong> Y are character str<strong>in</strong>gs, we have a natural<br />

set of <strong>in</strong>dices with which to def<strong>in</strong>e subproblems—<strong>in</strong>dices <strong>in</strong>to the str<strong>in</strong>gs X <strong>and</strong> Y.<br />

Let us def<strong>in</strong>e a subproblem, therefore, as that of comput<strong>in</strong>g the value L[i, j],<br />

779

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!