-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_increasing_subsequence.hpp
181 lines (156 loc) · 6.65 KB
/
longest_increasing_subsequence.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
* File : longest_increasing_subsequence.hpp
* Brief : Algorithms to find a longest increasing subsequence
*
* Author : Alexander Korobeynikov (alexander.korobeynikov@gmail.com)
*
*******************************************************************************
*/
#ifndef LONGEST_INCREASING_SUBSEQUENCE_HPP
#define LONGEST_INCREASING_SUBSEQUENCE_HPP
#include <iterator>
#include <vector>
#include <algorithm> // std::max()
#include "null_output_iterator.hpp"
namespace algo
{
// @brief Compute longest increasing subsequence in the input sequence
//
// @func longest_increasing_subsequence
// @time O(N logN)
// @space O(N)
//
// @param [in] begin - random iterator to the start of the sequence
// @param [in] end - random iterator to the end of the sequence
// @param [out] out - optional output iterator to write the
// LIS indecies to
// @param [in] comp - opional comparator, by default std::less
// @return value - length of longest increasing subsequence
//
// @example
// std::vector<int> seq = { 1, 0, 2, 0, 3 };
// std::vector<size_t> lis;
// size_t lislen = longest_increasing_subsequence(seq.begin(), seq.end(),
// std::back_inserter(lis));
template <typename RandomIterator,
typename OutputIterator = null_output_iterator,
typename Comparator =
std::less< typename std::iterator_traits<RandomIterator>::value_type> >
typename std::iterator_traits<RandomIterator>::difference_type
longest_increasing_subsequence(RandomIterator begin,
RandomIterator end,
OutputIterator out = OutputIterator(),
Comparator comp = Comparator())
{
typedef typename std::iterator_traits<RandomIterator>::difference_type DT;
const DT undef = (DT)-1;
const DT seqlen = end - begin;
std::vector<DT> tails(seqlen + 1, undef);
std::vector<DT> prevs(seqlen, undef);
DT maxlen = 0;
for (DT i = 0; i < seqlen; ++i) {
// binary search for a tail less than the current element
DT j = 0, lo = 1, hi = maxlen;
while (lo <= hi) {
DT m = lo + (hi - lo) / 2;
if (comp(begin[tails[m]], begin[i])) {
j = m;
lo = m + 1;
} else {
hi = m - 1;
}
}
prevs[i] = tails[j];
if (j == maxlen || comp(begin[i], begin[tails[j+1]])) {
tails[j+1] = i;
maxlen = std::max(maxlen, j + 1);
}
}
// backtrack and store the result
if (typeid(out) != typeid(null_output_iterator)){
std::vector<DT> lis(maxlen, 0);
DT n = maxlen;
DT i = tails[maxlen];
while (i != undef && n > 0) {
lis[--n] = i;
i = prevs[i];
}
std::copy(lis.begin(), lis.end(), out);
}
return maxlen;
}
// @brief Compute longest increasing subsequence in the input sequence
// Dynamic programming approach.
//
// @func longest_increasing_subsequence_dp
// @time O(N^2)
// @space O(N)
//
// @param [in] begin - random iterator to the start of the sequence
// @param [in] end - random iterator to the end of the sequence
// @param [out] out - optional output iterator to write the
// LIS indecies to
// @param [in] comp - opional comparator, by default std::less
// @return value - length of longest increasing subsequence
//
// @example
// std::vector<int> seq = { 1, 0, 2, 0, 3 };
// std::vector<size_t> lis;
// size_t lislen = longest_increasing_subsequence_dp(seq.begin(), seq.end(),
// std::back_inserter(lis));
template <typename RandomIterator,
typename OutputIterator = null_output_iterator,
typename Comparator =
std::less< typename std::iterator_traits<RandomIterator>::value_type> >
typename std::iterator_traits<RandomIterator>::difference_type
longest_increasing_subsequence_dp(RandomIterator begin,
RandomIterator end,
OutputIterator out = OutputIterator(),
Comparator comp = Comparator())
{
typedef typename std::iterator_traits<RandomIterator>::difference_type DT;
const DT undef = (DT)-1;
const DT seqlen = end - begin;
// at start, lislen[i] = 1 for all i
// meaning longest known increasing subsequence consists of just one
// element, the element at index i
std::vector<DT> lislen(seqlen, 1);
std::vector<DT> prevs(seqlen, undef);
DT maxlen = (!!(seqlen)), maxlen_ind = 0;
// for each index i in the input sequence, starting from the second
for (DT i = 1; i < seqlen; ++i) {
// for each index j, which is before index i in the input sequence
for (DT j = 0; j < i; ++j) {
// check if:
// 1. length of longest so far increasing subsequnce ending at j
// is at least as long as longest so far incr. subseq ending at i
// 2. element at j is less than element at i, meaning that
// the increasing subsequence ending at i can be extended by
// the increasing subsequence ending at j + element at i
if (lislen[j] >= lislen[i] &&
comp(begin[j], begin[i])) {
lislen[i] = lislen[j] + 1;
if (maxlen < lislen[i]) {
maxlen = lislen[i];
maxlen_ind = i;
}
// remember the previous index j to backtrack later
prevs[i] = j;
}
}
}
// backtrack and store the result
if (typeid(out) != typeid(null_output_iterator)){
std::vector<DT> lis(maxlen, 0);
DT n = maxlen;
DT i = maxlen_ind;
while (i != undef && n > 0) {
lis[--n] = i;
i = prevs[i];
}
std::copy(lis.begin(), lis.end(), out);
}
return maxlen;
}
}
#endif