Optimal substructure: Difference between revisions

Content deleted Content added
Karroje (talk | contribs)
Added two counter-examples.
Undid revision 1285865773 by 87.214.42.62 (talk)
 
(43 intermediate revisions by 36 users not shown)
Line 1:
{{Short description|Property of a computational problem}}
[[Image:Shortest path optimal substructure.pngsvg|200px|thumb|'''Figure 1'''. Finding the shortest path using optimal substructure. Numbers represent the length of the path; straight lines indicate single [[Edge (graph theory)|edges]], wavy lines indicate shortest [[Path (graph theory)|paths]], i.e., there might be other vertices that are not shown here.]]
In [[computer science]], a problem is said to have '''optimal substructure''' if an optimal solution can be constructed efficiently from optimal solutions to its subproblems.<ref>[http://books.google.com/books?id=NLngYyWFl_YC&pg=PA15&dq=introduction+to+algorithms&psp=1&sig=jX-xfEDWJU3PprUwH8Qfxidli6M#PPP1,M1 Introduction to Algorithms], 2nd ed., (Cormen, Leiserson, Rivest, and Stein) 2001, p. 327. ISBN 0-262-03293-7.</ref> This property is used to determine the usefulness of [[dynamic programming]] and [[greedy algorithm]]s in a problem.
In [[computer science]], a problem is said to have '''optimal substructure''' if an optimal solution can be constructed from optimal solutions of its subproblems. This property is used to determine the usefulness of greedy algorithms for a problem.<ref name=cormen>{{cite book|title=Introduction to Algorithms |edition=3rd|last1=Cormen|first1=Thomas H. |last2=Leiserson |first2=Charles E. |last3=Rivest|first3=Ronald L. |last4= Stein |first4=Clifford|date=2009 |isbn=978-0-262-03384-8|publisher=[[MIT Press]]|authorlink1=Thomas H. Cormen |authorlink2=Charles E. Leiserson|authorlink3=Ron Rivest |authorlink4=Clifford Stein}}</ref>
 
Typically, a [[greedy algorithm]] is used to solve a problem with optimal substructure if it can be provedproven by induction that this is optimal at each step (Cormen et al.<ref pp.&nbsp;381–2).name=cormen /> Otherwise, providingprovided the problem exhibits [[overlapping subproblemsubproblems]]s as well, [[Divide and conquer algorithm|divide-and-conquer]] methods or [[dynamic programming]] ismay be used. If there are no appropriate greedy algorithms and the problem fails to exhibit overlapping subproblems, often a lengthy but straightforward search of the solution space is the best alternative.
<!-- A special case of optimal substructure is the case where a subproblem S<sub>ab</sub> has an activity P<sub>y</sub>, then it should contain optimal solutions to subproblems S<sub>ay</sub> and S<sub>yb</sub>. --> <!-- *TODO: Add Recursion, misc. -->
 
In the application of [[dynamic programming]] to [[Optimization (mathematics)|mathematical optimization]], [[Richard Bellman]]'s [[PrincipleBellman equation#Bellman's principle of optimality|Principle of Optimality]] is based on the idea that in order to solve a dynamic optimization problem from some starting period ''t'' to some ending period ''T'', one implicitly has to solve subproblems starting from later dates ''s'', where ''t<s<T''. This is an example of optimal substructure. The Principle of Optimality is used to derive the [[Bellman equation]], which shows how the value of the problem starting from ''t'' is related to the value of the problem starting from ''s''.
 
==Example==
Consider finding a [[Shortest path problem|shortest path]] for travellingtraveling between two cities by car, as illustrated in Figure 1. Such an example is likely to exhibit optimal substructure. That is, if the shortest route from Seattle to Los Angeles passes through Portland and then Sacramento, then the shortest route from Portland to Los Angeles must pass through Sacramento too. That is, the problem of how to get from Portland to Los Angeles is nested inside the problem of how to get from Seattle to Los Angeles. (The wavy lines in the graph represent solutions to the subproblems.)
 
As an example of a problem that is unlikely to exhibit optimal substructure, consider the problem of finding the cheapest airline ticket from Buenos Aires to Moscow. Even if that ticket involves stops in Miami and then London, we can't conclude that the cheapest ticket from Miami to Moscow stops in London, because the price at which an airline sells a multi-flight trip is usually not the sum of the prices at which it would sell the individual flights in the trip.
 
==Definition==
A slightly more formal definition of optimal substructure can be given. Let a "problem" be a collection of "alternatives", and let each alternative have an associated cost, ''c''(''a)''). The task is to find a set of alternatives that minimizes ''c''(''a)''). Suppose that the alternatives can be [[Partition of a set|partitioned]] into subsets, wherei.e. each subsetalternative hasbelongs itsto ownonly costone function,subset. andSuppose each alternativesubset belongshas toits onlyown onecost subsetfunction. The minima of each of these cost functions can be found, as can the minima of the global cost function, ''restricted to the same subsets''. If these minima match for each subset, then it's almost obvious that a global minimum can be picked not out of the full set of alternatives, but out of only the set that consists of the minima of the smaller, local cost functions we have defined. If minimizing the local functions is a problem of "lower order", and (specifically) if, after a finite number of these reductions, the problem becomes trivial, then the problem has an optimal substructure.
 
== Problems with optimal substructure ==
* [[Longest common subsequence problem]]
* [[Longest increasing subsequence]]
* [[Mergesort]]
* [[Longest palindromic substring]]
* [[Quicksort]]
* [[Shortest_path_problem#All-pairs_shortest_paths|All-Pairs Shortest Path]]
* Any problem that can be solved by [[dynamic programming]].
 
== Problems ''without'' optimal substructure ==
* [[Longest path problem]]
* [[Addition-chain exponentiation]]
* '''Least-cost airline fair.''' (Using Orbitz, we will frequently find that the cheapest flight from airport A to airport B involves a single connection through airport C, but the cheapest flight from airport A to airport C involves a connection through some other airport D.)
* ''Least-cost airline fare.'' Using online flight search, we will frequently find that the cheapest flight from airport A to airport B involves a single connection through airport C, but the cheapest flight from airport A to airport C involves a connection through some other airport D. However, if the problem takes the maximum number of layovers as a parameter, then the problem has optimal substructure. The cheapest flight from A to B that involves at most ''k'' layovers is either the direct flight; or the cheapest flight from A to some airport C that involves at most ''t'' layovers for some integer ''t'' with ''0≤t<k'', plus the cheapest flight from C to B that involves at most ''k−1−t'' layovers.
 
== See also ==
* [[Dynamic Programming]]
* [[Principle of optimality]]
* [[Divide and conquer algorithm]]
 
== References ==
<references />
 
[[Category:Mathematical optimization]]
[[Category:Dynamic programming]]
 
[[it:Sottostruttura ottimale]]
[[pl:Własność optymalnej podstruktury]]
[[uk:Оптимальна підструктура]]