Let
be a sequence of integers. Devise a polynomial time algorithm finding a longest
increasing subsequence of
(i.e., a longest subsequence
satisfying
and analyze its time complexity.
Let us assume we have created the following directed graph . The set of vertices is the set
of integers. And there is a link from vertex
to vertex
if and only if
. The
subsequence we are seeking for is a longest path in this graph. Below stands an instance of such a
graph.
To look for a longest subsequence in such a graph, we can use a val and a dad arrays
of
integers. val
contains the length a longest path reaching
and dad
is its father in such a path. Every time a node is looked at, we check all its successors and we compare
their val values with the sum of the val value of the current node plus 1. If the sum
is greater then it is assigned to the val entry associated with the successor node and the
dad array is updated consequently. The highest val value will denote the last node
of a longest path and reading the dad table in reverse order will yield that path.
The algorithm has a
time complexity since we have to check each node and for each node the set
of its adjacent nodes.