Why longest path problem doesn't have optimal substructure?

We all know that the shortest path problem has optimal substructure. The reasoning is like below:

Supppose we have a path p from node u to v, another node t lies on path p: u->t->v ("->" means a path).

We claim that u->t is also a shortest path from u to t, and t->v is a shortest path from t to v.

Proof: if there is another path from u to t that is shorter than u->t, we can simply replace u->t with this shorter path in the solution of u->v, resulting in a shorter path than u->v, contradicting the fact that u->v is a shortest path from u to v.

But why can't we apply similar reasoning to the longest path problem? It's because in the longest path problem there are some constraints imposed on the solution. Suppose u->v is a longest path from u->v, node t lies on it.

So it's like u->t->v. If there is a longer path from u to t than u->t, if we cut off u->t from u->v and paste in the longer path, this new solution may fail some of the restrictions, for example, it may contains a cycle, which is invalid.

上一篇:python学习笔记之迭代器和函数(第三天)


下一篇:QParserGenerator的文法文件介绍