Let be the optimal (minimum cost) route for traveling from city i to city j, and assume the route passes through city k. Let and be the routes from i to k and kto j, respectively, contained in the optimal solution to the original problem. To prove optimal substructure, we need to prove that in order for the route to be optimal, the routes and must also be optimal solutions to their respective subproblems.
Using proof by contradiction, assume there is a better solution
for the subproblem of traveling from city i to
city k, such that
Since the solution for traveling from k to j will work regardless of
how we arrived at city k, we could then use the better solution
to arrive at a lower cost solution to the original
But this contradicts the original definition that is optimal. Therefore, the route from i to k contained in the original solution must an optimal solution to the subproblem of traveling from i to k. The same argument works for the subproblem of traveling from k to j. Thus, the problem exhibits optimal substructure.
Because we do not know the optimal value for k, we try all possible
where C[i,j] is the given cost of traveling directly from city i to city j.
1 if i = j
2 then return 0
3 else c = C[i,j]
4 for k = i+1 to j-1
5 q = RECURSIVE-TRAVEL(C,i,k) + RECURSIVE-TRAVEL(C,k,j)
6 if q < c
7 then c = q
8 return c
The recurrence for RECURSIVE-TRAVEL is shown below (best, average
and worst cases are all the same). Note the constraints on n showing
that recursive calls are made only if j > i + 1.
Using the substitution method, we can show
using the inductive hypotheses
where both k and n-k are less than n.
The last inequality is true only if , or , which is true for any constant c and sufficiently large n. Thus, .
Below is a portion of the recursion tree for the computation of c(1,4)with overlapping subproblems indicated.
Since there is no backtracking, the number of unique subproblems is the
number of different values for i and j from 1 to n in c(i,j),
which can be expressed as
Therefore, the number of unique subproblems is polynomial.
1 allocate ;; costs
2 allocate ;; agencies
3 for i = 1 to n
4 for j = 1 to n
5 c[i,j] = C[i,j] ;; initially choose direct routes
6 a[i,j] = j
7 for ws = 3 to n
8 for i = 1 to n - ws + 1
9 j = i + ws - 1
10 for k = i+1 to j-1
11 q = c[i,k] + c[k,j]
12 if q < c[i,j]
13 then c[i,j] = q
14 a[i,j] = k
Analysis and Explanation: TRAVEL first allocates (lines 1-2) the arrays for holding the optimal cost and optimal decisions for which intermediate city to visit next. Thus, a[i,j]=k implies that in traveling form city i to j, you should definitely stop to change cars in city k. Lines 3-6 initialize the cost and decision arrays to be the cost matrix and the decision to travel directly from i to j, which may change later as we consider more complex routes. Lines 1-6 take O(n2)time and memory. Lines 7-14 contain three nested for loops, each of which may run n times in the worst case, yielding a total of O(n3)times through lines 11-14. Thus, the running time of TRAVEL is dominated by the nested for loops, i.e., T(n) = O(n3). Note that the window size (ws) starts at 3, because a trip of size 2 (i.e., travel from city i to city i+1) has already been processed correctly in lines 3-6.
All that remains is an algorithm to output the optimal sequence of cities to visit. The following OPTIMAL-ROUTE(a,i,j) algorithm outputs the optimal cities as stored in the a array by a call to TRAVEL.
if a[i,j] = j
then print j
Since OPTIMAL-ROUTE recurses at most n times, where n is the number of cities, then the running time is O(n). Thus, the total time to produce the optimal route is still O(n3).
Consider the above instance of the rental car problem depicting the costs of every direct route. The greedy choice would choose to go from city 1 to city 3 first, and then to city 4 for a total cost of 11. However, the optimal route is to go directly from 1 to 4. Therefore, the greedy choice is not in the optimal solution and does not exhibit the greedy choice property.
n = n - 25
n = n - 10
n = n - 5
n = n - 1
To prove this greedy choice yields an optimal solution, we must show optimal substructure and the greedy choice property.
Proof of Optimal Substructure: Let be the coins comprising an optimal solution to the coin-changing problem, where ci is either a penny, nickel, dime or quarter, and the ci's sum to n, the change to be made. Putting aside the first coin choice c1, the remaining coins represent a solution to the n - d(c1) coin-changing problem, where d(c) is the denomination of coin c. If were not an optimal solution to the n-d(c1) change problem, then there exists another solution , where k < m; thus taking fewer coins. But if such a solution exists for n-d(c1), then combining it with c1 would yield a better solution to the n change problem. Since we started with an optimal solution, this is a contradiction, must be an optimal solution to the subproblem n-d(c1), and the coin-changing problem exhibits optimal substructure.
Proof of the Greedy Choice Property: Assume we have an optimal solution to the n-change problem . First, we know that the coins ci add up to n no matter what order they are chosen, therefore, we can make any coin choice cj swap with the first choice c1 and still yield an optimal solution. We have only to prove that the greedy choice must reside somewhere in an optimal solution. Assume it does not. Then, one of the highest denomination coins less than n is not in the solution. But, that means that lower denomination coins must be used to obtain the amount of cents of the higher-denomination coin. Since it takes at least two lower-denomination coins to equal the cents of a higher-denomination coin, we could always replace these coins with the greedy coin and obtain a better solution (fewer coins). Therefore, the greedy coin must be in the optimal solution. Thus, a greedy choice for the first coin will lead to an optimal solution.
Furthermore, since the greedy choice of coin c for the n-change problem reduces the problem to an n - d(c) change problem, and the problem exhibits optimal substructure, the greedy algorithm will yield an optimal solution.