skip to content

Why does longest simple path lack optimal substructure when shortest path has it?

level: seniorimportance: should knowfreq 40%

answer

  1. swapping min for max is not enough
  2. check the prefix of the optimal route
  3. can the two halves be pasted?
  4. the no-revisit rule is global
  5. shared pool of unvisited towns

basics

~20 s

The pieces are not independent: a longest simple path's prefix need not be longest to that town, and pasting two optimal halves can revisit a town, producing a walk that is not simple. Shortest paths suffer neither failure.

solid answer

~50 s

Optimal substructure needs two things: the optimal whole must contain optimal parts, and the parts must be **independent**. Longest simple path fails both. Take a small road map with towns A, B, C, D and roads A-B, B-C, C-D, D-A plus a shortcut B-D, each road one unit. The longest simple A-to-C route is A-B-D-C, three roads — yet its A-to-B prefix is the single road A-B, while the longest simple A-to-B route is A-D-C-B, also three roads. The optimal whole does not contain the optimal part. Worse, pasting the optimal A-to-B route onto the optimal B-to-C route revisits towns, so the result is not a simple path. The 'no repeated vertex' constraint is *global*, which couples the two halves. Shortest paths have no such coupling: any sub-route of a shortest route is itself shortest, and concatenation stays valid.

go deeper

for a junior

Know that shortest path and longest simple path are not mirror images, and that a path is 'simple' when it visits no vertex twice. That constraint is the source of the difference.

for a middle

Explain both failure modes with a small graph: the optimal route's prefix need not be optimal for its endpoints, and two optimal halves may share vertices so they cannot be concatenated.

for a senior

Show judgment about what to do next — identify the shared resource that couples the subproblems, know that folding it into the state restores correctness at exponential cost, and recognise that the problem is NP-hard rather than merely awkward.

for a principal

Own the decision this forces: when a formulation is correct only with an exponential state, the choice is between an exact method on bounded inputs, a heuristic with a quality bound, or reshaping the problem so the global constraint stops binding.

## The two problems look symmetric and are not Swapping `min` for `max` in a shortest-path recurrence feels like it should give longest paths. It does not, and the reason is the textbook illustration of why optimal substructure must be *argued* rather than assumed. First, the definitions matter. A **simple path** visits no town twice. Without that restriction, "longest path" is meaningless in any graph containing a cycle — you would loop forever. The restriction is what makes the question well posed, and it is also exactly what destroys the property. ## A concrete road map Four towns, five one-unit roads: ``` A - B roads: A-B, B-C, C-D, D-A, B-D | \ | D - C ``` Adjacency: `A: B, D` — `B: A, C, D` — `C: B, D` — `D: A, C, B`. **Failure one — the optimal whole does not contain optimal parts.** The longest simple route from A to C is `A-B-D-C`, three roads (or the mirror `A-D-B-C`). Cut it at B. Its prefix is the single road `A-B`, length one. But the longest simple route from A to B is `A-D-C-B`, length three. So the optimal solution to the whole problem contains a *sub-optimal* solution to the subproblem. The cut-and-paste argument that carries shortest paths cannot even get started here. **Failure two — the pieces are not independent.** Try to build the answer anyway: take the longest simple A-to-B route, `A-D-C-B`, and the longest simple B-to-C route, `B-A-D-C`, and concatenate at B. The result visits A, D and C twice each. It is not a simple path; it is not a valid solution at all. Pasting optimal pieces produced something outside the solution space. That second failure is the deeper one. The constraint "no repeated town" is **global**: it is a property of the whole route, not of either half separately. Whichever towns one half consumes are forbidden to the other. Two subproblems that compete over a shared resource — here, the pool of unvisited towns — are not independent, and independence is precisely the condition the exchange argument relies on. ## Why shortest paths escape both failures For shortest paths with non-negative edge costs, cut an optimal A-to-Z route at any town M. If the A-to-M portion were not shortest, substitute a shorter one and the total drops — contradiction. The substitution is always legal because the suffix does not care how you reached M; there is no constraint linking the halves. (A shortest route also never repeats a town when costs are non-negative, so simplicity comes for free rather than being an extra constraint you must enforce.) The asymmetry is therefore not about minimising versus maximising. Plenty of maximisation problems have flawless optimal substructure. It is about whether the subproblems are independent, and about whether the constraint defining a valid solution is local or global. ## The consequence, and the honest thing to say Because the property fails, no straightforward recurrence over (current town, destination) is sound. Finding a longest simple path in a general graph is NP-hard, so no polynomial-time algorithm is known for it — which is a much stronger statement than "my recurrence did not work." Two tempting repairs and why they fail: - **Negate the road lengths and run a shortest-path search.** Negative edge costs make cycles with negative total length possible, and then "shortest" is unbounded — the search is ill-posed rather than merely slow. Negation converts a hard problem into an ill-defined one, not an easy one. - **Restore independence by widening the state.** This is the principled fix and it genuinely works: make the subproblem `(current town, set of towns already visited)`. The pieces become independent again because the shared resource is now part of the key. The catch is that the state space grows with the number of subsets of towns, which is exponential — correct, but not polynomial. Widening state to restore independence always costs state-space size; here the cost is prohibitive. ## What an interviewer is listening for A weak answer says "longest path just doesn't work with DP." A strong one distinguishes the two failures — sub-optimal pieces inside the optimal whole, and pieces that cannot legally be pasted — names the global simplicity constraint as the coupling, and volunteers the state-widening repair together with its exponential price. Being able to produce a four-town counterexample on demand is what makes the answer credible rather than recited.

  • Can you restore optimal substructure for longest simple path by changing the subproblem definition?
    Yes, at a price. Define the subproblem as (current town, set of towns already visited). The visited set is exactly the shared resource that coupled the halves, so putting it in the key makes the subproblems independent again and the recurrence becomes sound. But the number of distinct states now grows with the number of subsets of towns, so the algorithm is correct and still exponential.
  • Does the property come back on a directed acyclic graph?
    It does. With no cycles, a path can never revisit a vertex, so the simplicity constraint is automatic rather than imposed, and the halves stop competing. Longest path on a directed acyclic graph is solvable in linear time by relaxing edges in topological order — the same problem is easy or NP-hard depending entirely on whether that global constraint is binding.
  • Is the failure caused by maximising rather than minimising?
    No. Direction of the objective is irrelevant; many maximisation problems have clean optimal substructure. The failure comes from the simplicity constraint being global, which makes the subproblems dependent. Shortest path with non-negative costs happens to get simplicity for free, which is why the minimising version looks better behaved.

Two teams planning halves of a road trip can each pick their own best route only if they are not drawing from the same pool of towns. The moment no town may be visited twice, one team's best plan quietly forecloses the other's.

saying these in an interview costs you the question

  • Says the difference is minimising versus maximising
  • Claims longest path works if you negate the edge weights
  • Cannot produce a small counterexample on demand
  • Misses that pasted halves can revisit a vertex
  • Never names the simplicity constraint as a global coupling
  • Says memoizing on the current vertex alone fixes it

context