मेरे पास पूर्णांकों का एक सेट है। मैं डायनामिक प्रोग्रामिंग का उपयोग करते हुए उस सेट की सबसे लंबे समय तक वृद्धि को प्राप्त करना चाहता हूं ।
मेरे पास पूर्णांकों का एक सेट है। मैं डायनामिक प्रोग्रामिंग का उपयोग करते हुए उस सेट की सबसे लंबे समय तक वृद्धि को प्राप्त करना चाहता हूं ।
जवाबों:
ठीक है, मैं पहले सबसे सरल समाधान का वर्णन करूंगा जो ओ (एन ^ 2) है, जहां एन संग्रह का आकार है। वहाँ भी एक ओ (एन लॉग एन) समाधान मौजूद है, जिसका मैं भी वर्णन करूंगा। अनुभाग कुशल एल्गोरिदम पर इसके लिए यहां देखें ।
मुझे लगता है कि सरणी के सूचकांक 0 से एन - 1. तक हैं, तो चलो DP[i]
LIS की लंबाई (सबसे लंबे समय तक बढ़ती अनुवर्ती) को परिभाषित करते हैं जो सूचकांक के साथ तत्व पर समाप्त हो रहा है i
। गणना करने के लिए DP[i]
हम सभी सूचकांकों को देखते हैं j < i
और यदि DP[j] + 1 > DP[i]
और array[j] < array[i]
(हम इसे बढ़ाना चाहते हैं) दोनों की जांच करें । यदि यह सच है तो हम वर्तमान के लिए अधिकतम अपडेट कर सकते हैं DP[i]
। सरणी के लिए वैश्विक इष्टतम खोजने के लिए आप अधिकतम मूल्य ले सकते हैं DP[0...N - 1]
।
int maxLength = 1, bestEnd = 0;
DP[0] = 1;
prev[0] = -1;
for (int i = 1; i < N; i++)
{
DP[i] = 1;
prev[i] = -1;
for (int j = i - 1; j >= 0; j--)
if (DP[j] + 1 > DP[i] && array[j] < array[i])
{
DP[i] = DP[j] + 1;
prev[i] = j;
}
if (DP[i] > maxLength)
{
bestEnd = i;
maxLength = DP[i];
}
}
मैं सरणी prev
का उपयोग बाद में वास्तविक अनुक्रम को खोजने में सक्षम हूं, न केवल इसकी लंबाई। बस bestEnd
एक लूप का उपयोग करके पुनरावर्ती रूप से वापस जाएं prev[bestEnd]
। -1
मूल्य को रोकने के लिए एक संकेत है।
O(N log N)
समाधान के लिए:चलो S[pos]
सबसे छोटे पूर्णांक के रूप में परिभाषित किया जाता है जो लंबाई के बढ़ते क्रम को समाप्त करता है pos
। अब X
इनपुट सेट के प्रत्येक पूर्णांक के माध्यम से पुनरावृति करें और निम्नलिखित करें:
यदि X
> अंतिम तत्व में है S
, तो X
के अंत में संलग्न करें S
। इस आवश्यक साधन का मतलब हमें एक नया सबसे बड़ा मिल गया है LIS
।
अन्यथा सबसे छोटा तत्व खोजें S
, जो कि >=
है X
, और इसे बदल दें X
। क्योंकि S
किसी भी समय छांटा जाता है, तत्व को द्विआधारी खोज का उपयोग करके पाया जा सकता है log(N)
।
कुल रनटाइम - N
पूर्णांक और उनमें से प्रत्येक के लिए एक द्विआधारी खोज - एन * लॉग (एन) = ओ (एन लॉग एन)
अब एक वास्तविक उदाहरण करते हैं:
पूर्णांक का संग्रह:
2 6 3 4 1 2 9 5 8
कदम:
0. S = {} - Initialize S to the empty set
1. S = {2} - New largest LIS
2. S = {2, 6} - New largest LIS
3. S = {2, 3} - Changed 6 to 3
4. S = {2, 3, 4} - New largest LIS
5. S = {1, 3, 4} - Changed 2 to 1
6. S = {1, 2, 4} - Changed 3 to 2
7. S = {1, 2, 4, 9} - New largest LIS
8. S = {1, 2, 4, 5} - Changed 9 to 5
9. S = {1, 2, 4, 5, 8} - New largest LIS
तो LIS की लंबाई 5
(S का आकार) है।
वास्तविक का पुनर्निर्माण करने के लिए LIS
हम फिर से एक मूल सरणी का उपयोग करेंगे। आज्ञा देना parent[i]
पूर्ववर्ती तत्व के साथ सूचकांक i
में LIS
तत्व के साथ अंत में सूचकांक है i
।
चीजों को सरल बनाने के लिए, हम सरणी में रख सकते S
हैं, वास्तविक पूर्णांक नहीं, लेकिन सेट में उनके सूचकांक (स्थान)। हम नहीं रखते {1, 2, 4, 5, 8}
, लेकिन रखते हैं {4, 5, 3, 7, 8}
।
वह इनपुट [4] = 1 , इनपुट [5] = 2 , इनपुट [3] = 4 , इनपुट [7] = 5 , इनपुट [8] = 8 है ।
अगर हम मूल सरणी को ठीक से अपडेट करते हैं, तो वास्तविक LIS है:
input[S[lastElementOfS]],
input[parent[S[lastElementOfS]]],
input[parent[parent[S[lastElementOfS]]]],
........................................
अब महत्वपूर्ण बात पर - हम मूल सरणी को कैसे अपडेट करें? दो विकल्प हैं:
यदि X
> अंतिम तत्व में S
, तब parent[indexX] = indexLastElement
। इसका मतलब यह है कि नवीनतम तत्व का मूल तत्व अंतिम तत्व है। हम बस X
के अंत में प्रस्तुत करते हैं S
।
अन्यथा सबसे छोटे तत्व के सूचकांक को ढूंढें S
, जो कि >=
है X
, और इसे बदल दें X
। यहाँ parent[indexX] = S[index - 1]
।
DP[j] + 1 == DP[i]
तब DP[i]
साथ बेहतर नहीं होगा DP[i] = DP[j] + 1
। हम अनुकूलन करने की कोशिश कर रहे हैं DP[i]
।
[1,2,5,8]
, 4 सरणी में 1 से पहले आता है, एलआईएस कैसे हो सकता है [1,2,4,5,8]
?
[2,3,4,5,8]
। ध्यान से पढ़ें - S
सरणी DOES NOT
एक वास्तविक अनुक्रम का प्रतिनिधित्व करती है। Let S[pos] be defined as the smallest integer that ends an increasing sequence of length pos.
पेटर मिनचेव के स्पष्टीकरण ने मेरे लिए स्पष्ट चीजों की मदद की, लेकिन मेरे लिए यह मुश्किल था कि सब कुछ क्या था, इसलिए मैंने अति-वर्णनात्मक चर नामों और बहुत सारी टिप्पणियों के साथ पायथन कार्यान्वयन किया। मैंने एक भोली पुनरावर्ती समाधान किया, ओ (एन ^ 2) समाधान, और ओ (एन लॉग एन) समाधान।
मुझे आशा है कि यह एल्गोरिदम को साफ करने में मदद करता है!
def recursive_solution(remaining_sequence, bigger_than=None):
"""Finds the longest increasing subsequence of remaining_sequence that is
bigger than bigger_than and returns it. This solution is O(2^n)."""
# Base case: nothing is remaining.
if len(remaining_sequence) == 0:
return remaining_sequence
# Recursive case 1: exclude the current element and process the remaining.
best_sequence = recursive_solution(remaining_sequence[1:], bigger_than)
# Recursive case 2: include the current element if it's big enough.
first = remaining_sequence[0]
if (first > bigger_than) or (bigger_than is None):
sequence_with = [first] + recursive_solution(remaining_sequence[1:], first)
# Choose whichever of case 1 and case 2 were longer.
if len(sequence_with) >= len(best_sequence):
best_sequence = sequence_with
return best_sequence
def dynamic_programming_solution(sequence):
"""Finds the longest increasing subsequence in sequence using dynamic
programming. This solution is O(n^2)."""
longest_subsequence_ending_with = []
backreference_for_subsequence_ending_with = []
current_best_end = 0
for curr_elem in range(len(sequence)):
# It's always possible to have a subsequence of length 1.
longest_subsequence_ending_with.append(1)
# If a subsequence is length 1, it doesn't have a backreference.
backreference_for_subsequence_ending_with.append(None)
for prev_elem in range(curr_elem):
subsequence_length_through_prev = (longest_subsequence_ending_with[prev_elem] + 1)
# If the prev_elem is smaller than the current elem (so it's increasing)
# And if the longest subsequence from prev_elem would yield a better
# subsequence for curr_elem.
if ((sequence[prev_elem] < sequence[curr_elem]) and
(subsequence_length_through_prev >
longest_subsequence_ending_with[curr_elem])):
# Set the candidate best subsequence at curr_elem to go through prev.
longest_subsequence_ending_with[curr_elem] = (subsequence_length_through_prev)
backreference_for_subsequence_ending_with[curr_elem] = prev_elem
# If the new end is the best, update the best.
if (longest_subsequence_ending_with[curr_elem] >
longest_subsequence_ending_with[current_best_end]):
current_best_end = curr_elem
# Output the overall best by following the backreferences.
best_subsequence = []
current_backreference = current_best_end
while current_backreference is not None:
best_subsequence.append(sequence[current_backreference])
current_backreference = (backreference_for_subsequence_ending_with[current_backreference])
best_subsequence.reverse()
return best_subsequence
def find_smallest_elem_as_big_as(sequence, subsequence, elem):
"""Returns the index of the smallest element in subsequence as big as
sequence[elem]. sequence[elem] must not be larger than every element in
subsequence. The elements in subsequence are indices in sequence. Uses
binary search."""
low = 0
high = len(subsequence) - 1
while high > low:
mid = (high + low) / 2
# If the current element is not as big as elem, throw out the low half of
# sequence.
if sequence[subsequence[mid]] < sequence[elem]:
low = mid + 1
# If the current element is as big as elem, throw out everything bigger, but
# keep the current element.
else:
high = mid
return high
def optimized_dynamic_programming_solution(sequence):
"""Finds the longest increasing subsequence in sequence using dynamic
programming and binary search (per
http://en.wikipedia.org/wiki/Longest_increasing_subsequence). This solution
is O(n log n)."""
# Both of these lists hold the indices of elements in sequence and not the
# elements themselves.
# This list will always be sorted.
smallest_end_to_subsequence_of_length = []
# This array goes along with sequence (not
# smallest_end_to_subsequence_of_length). Following the corresponding element
# in this array repeatedly will generate the desired subsequence.
parent = [None for _ in sequence]
for elem in range(len(sequence)):
# We're iterating through sequence in order, so if elem is bigger than the
# end of longest current subsequence, we have a new longest increasing
# subsequence.
if (len(smallest_end_to_subsequence_of_length) == 0 or
sequence[elem] > sequence[smallest_end_to_subsequence_of_length[-1]]):
# If we are adding the first element, it has no parent. Otherwise, we
# need to update the parent to be the previous biggest element.
if len(smallest_end_to_subsequence_of_length) > 0:
parent[elem] = smallest_end_to_subsequence_of_length[-1]
smallest_end_to_subsequence_of_length.append(elem)
else:
# If we can't make a longer subsequence, we might be able to make a
# subsequence of equal size to one of our earlier subsequences with a
# smaller ending number (which makes it easier to find a later number that
# is increasing).
# Thus, we look for the smallest element in
# smallest_end_to_subsequence_of_length that is at least as big as elem
# and replace it with elem.
# This preserves correctness because if there is a subsequence of length n
# that ends with a number smaller than elem, we could add elem on to the
# end of that subsequence to get a subsequence of length n+1.
location_to_replace = find_smallest_elem_as_big_as(sequence, smallest_end_to_subsequence_of_length, elem)
smallest_end_to_subsequence_of_length[location_to_replace] = elem
# If we're replacing the first element, we don't need to update its parent
# because a subsequence of length 1 has no parent. Otherwise, its parent
# is the subsequence one shorter, which we just added onto.
if location_to_replace != 0:
parent[elem] = (smallest_end_to_subsequence_of_length[location_to_replace - 1])
# Generate the longest increasing subsequence by backtracking through parent.
curr_parent = smallest_end_to_subsequence_of_length[-1]
longest_increasing_subsequence = []
while curr_parent is not None:
longest_increasing_subsequence.append(sequence[curr_parent])
curr_parent = parent[curr_parent]
longest_increasing_subsequence.reverse()
return longest_increasing_subsequence
bisect
। एक एल्गोरिथ्म कैसे काम करता है और इसकी प्रदर्शन विशेषताओं के प्रदर्शन के लिए, मैं चीजों को यथासंभव आदिम रखने की कोशिश कर रहा था।
डीपी समाधान के बारे में बोलते हुए, मुझे आश्चर्य हुआ कि किसी ने भी इस तथ्य का उल्लेख नहीं किया कि एलआईएस को एलसीएस में घटाया जा सकता है । आपको केवल मूल अनुक्रम की प्रतिलिपि को क्रमबद्ध करने की आवश्यकता है, सभी डुप्लिकेट को हटा दें और उनमें से LCS करें। स्यूडोकोड में यह है:
def LIS(S):
T = sort(S)
T = removeDuplicates(T)
return LCS(S, T)
और पूर्ण कार्यान्वयन गो में लिखा गया है। यदि आपको समाधान को फिर से बनाने की आवश्यकता नहीं है, तो आपको पूरे n ^ 2 DP मैट्रिक्स को बनाए रखने की आवश्यकता नहीं है।
func lcs(arr1 []int) int {
arr2 := make([]int, len(arr1))
for i, v := range arr1 {
arr2[i] = v
}
sort.Ints(arr1)
arr3 := []int{}
prev := arr1[0] - 1
for _, v := range arr1 {
if v != prev {
prev = v
arr3 = append(arr3, v)
}
}
n1, n2 := len(arr1), len(arr3)
M := make([][]int, n2 + 1)
e := make([]int, (n1 + 1) * (n2 + 1))
for i := range M {
M[i] = e[i * (n1 + 1):(i + 1) * (n1 + 1)]
}
for i := 1; i <= n2; i++ {
for j := 1; j <= n1; j++ {
if arr2[j - 1] == arr3[i - 1] {
M[i][j] = M[i - 1][j - 1] + 1
} else if M[i - 1][j] > M[i][j - 1] {
M[i][j] = M[i - 1][j]
} else {
M[i][j] = M[i][j - 1]
}
}
}
return M[n2][n1]
}
निम्नलिखित C ++ कार्यान्वयन में कुछ कोड भी शामिल हैं जो कि एक सरणी नामक एक का उपयोग करके वास्तविक सबसे लंबे समय तक बढ़ने वाली बाद में बनाता है prev
।
std::vector<int> longest_increasing_subsequence (const std::vector<int>& s)
{
int best_end = 0;
int sz = s.size();
if (!sz)
return std::vector<int>();
std::vector<int> prev(sz,-1);
std::vector<int> memo(sz, 0);
int max_length = std::numeric_limits<int>::min();
memo[0] = 1;
for ( auto i = 1; i < sz; ++i)
{
for ( auto j = 0; j < i; ++j)
{
if ( s[j] < s[i] && memo[i] < memo[j] + 1 )
{
memo[i] = memo[j] + 1;
prev[i] = j;
}
}
if ( memo[i] > max_length )
{
best_end = i;
max_length = memo[i];
}
}
// Code that builds the longest increasing subsequence using "prev"
std::vector<int> results;
results.reserve(sz);
std::stack<int> stk;
int current = best_end;
while (current != -1)
{
stk.push(s[current]);
current = prev[current];
}
while (!stk.empty())
{
results.push_back(stk.top());
stk.pop();
}
return results;
}
कोई स्टैक के साथ कार्यान्वयन वेक्टर को उल्टा करता है
#include <iostream>
#include <vector>
#include <limits>
std::vector<int> LIS( const std::vector<int> &v ) {
auto sz = v.size();
if(!sz)
return v;
std::vector<int> memo(sz, 0);
std::vector<int> prev(sz, -1);
memo[0] = 1;
int best_end = 0;
int max_length = std::numeric_limits<int>::min();
for (auto i = 1; i < sz; ++i) {
for ( auto j = 0; j < i ; ++j) {
if (s[j] < s[i] && memo[i] < memo[j] + 1) {
memo[i] = memo[j] + 1;
prev[i] = j;
}
}
if(memo[i] > max_length) {
best_end = i;
max_length = memo[i];
}
}
// create results
std::vector<int> results;
results.reserve(v.size());
auto current = best_end;
while (current != -1) {
results.push_back(s[current]);
current = prev[current];
}
std::reverse(results.begin(), results.end());
return results;
}
यहाँ गतिशील प्रोग्रामिंग दृष्टिकोण से समस्या का मूल्यांकन करने के तीन चरण हैं:
यदि हम एक उदाहरण अनुक्रम {0, 8, 2, 3, 7, 9} को इंडेक्स पर लेते हैं:
यहाँ काम कर रहा है C ++ 11 कोड:
#include <iostream>
#include <vector>
int getLongestIncSub(const std::vector<int> &sequence, size_t index, std::vector<std::vector<int>> &sub) {
if(index == 0) {
sub.push_back(std::vector<int>{sequence[0]});
return 1;
}
size_t longestSubSeq = getLongestIncSub(sequence, index - 1, sub);
std::vector<std::vector<int>> tmpSubSeq;
for(std::vector<int> &subSeq : sub) {
if(subSeq[subSeq.size() - 1] < sequence[index]) {
std::vector<int> newSeq(subSeq);
newSeq.push_back(sequence[index]);
longestSubSeq = std::max(longestSubSeq, newSeq.size());
tmpSubSeq.push_back(newSeq);
}
}
std::copy(tmpSubSeq.begin(), tmpSubSeq.end(),
std::back_insert_iterator<std::vector<std::vector<int>>>(sub));
return longestSubSeq;
}
int getLongestIncSub(const std::vector<int> &sequence) {
std::vector<std::vector<int>> sub;
return getLongestIncSub(sequence, sequence.size() - 1, sub);
}
int main()
{
std::vector<int> seq{0, 8, 2, 3, 7, 9};
std::cout << getLongestIncSub(seq);
return 0;
}
यहाँ ओ (एन ^ 2) एल्गोरिथ्म का एक स्काला कार्यान्वयन है:
object Solve {
def longestIncrSubseq[T](xs: List[T])(implicit ord: Ordering[T]) = {
xs.foldLeft(List[(Int, List[T])]()) {
(sofar, x) =>
if (sofar.isEmpty) List((1, List(x)))
else {
val resIfEndsAtCurr = (sofar, xs).zipped map {
(tp, y) =>
val len = tp._1
val seq = tp._2
if (ord.lteq(y, x)) {
(len + 1, x :: seq) // reversely recorded to avoid O(n)
} else {
(1, List(x))
}
}
sofar :+ resIfEndsAtCurr.maxBy(_._1)
}
}.maxBy(_._1)._2.reverse
}
def main(args: Array[String]) = {
println(longestIncrSubseq(List(
0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15)))
}
}
यहाँ एक और O (n ^ 2) JAVA कार्यान्वयन है। वास्तविक पुनरावृत्ति उत्पन्न करने के लिए कोई पुनरावृत्ति / संस्मरण नहीं। बस एक स्ट्रिंग सरणी जो प्रत्येक चरण में वास्तविक LIS को संग्रहीत करती है और प्रत्येक तत्व के लिए LIS की लंबाई को संग्रहीत करने के लिए एक सरणी है। बहुत आसान आसान है। एक नज़र देख लो:
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Created by Shreyans on 4/16/2015
*/
class LNG_INC_SUB//Longest Increasing Subsequence
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Numbers Separated by Spaces to find their LIS\n");
String[] s1=br.readLine().split(" ");
int n=s1.length;
int[] a=new int[n];//Array actual of Numbers
String []ls=new String[n];// Array of Strings to maintain LIS for every element
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(s1[i]);
}
int[]dp=new int[n];//Storing length of max subseq.
int max=dp[0]=1;//Defaults
String seq=ls[0]=s1[0];//Defaults
for(int i=1;i<n;i++)
{
dp[i]=1;
String x="";
for(int j=i-1;j>=0;j--)
{
//First check if number at index j is less than num at i.
// Second the length of that DP should be greater than dp[i]
// -1 since dp of previous could also be one. So we compare the dp[i] as empty initially
if(a[j]<a[i]&&dp[j]>dp[i]-1)
{
dp[i]=dp[j]+1;//Assigning temp length of LIS. There may come along a bigger LIS of a future a[j]
x=ls[j];//Assigning temp LIS of a[j]. Will append a[i] later on
}
}
x+=(" "+a[i]);
ls[i]=x;
if(dp[i]>max)
{
max=dp[i];
seq=ls[i];
}
}
System.out.println("Length of LIS is: " + max + "\nThe Sequence is: " + seq);
}
}
कोड इन एक्शन: http://ideone.com/sBiOQx
इसे डायनेमिक प्रोग्रामिंग का उपयोग करके O (n ^ 2) में हल किया जा सकता है। पायथन कोड उसी के लिए होगा: -
def LIS(numlist):
LS = [1]
for i in range(1, len(numlist)):
LS.append(1)
for j in range(0, i):
if numlist[i] > numlist[j] and LS[i]<=LS[j]:
LS[i] = 1 + LS[j]
print LS
return max(LS)
numlist = map(int, raw_input().split(' '))
print LIS(numlist)
इनपुट के लिए:5 19 5 81 50 28 29 1 83 23
उत्पादन होगा:[1, 2, 1, 3, 3, 3, 4, 1, 5, 3]
5
आउटपुट सूची का list_index इनपुट सूची का list_index है। आउटपुट सूची में दिए गए list_index का मान उस list_index के लिए सबसे लंबे समय तक बढ़ने वाली लंबाई को दर्शाता है।
यहाँ java O (nlogn) कार्यान्वयन है
import java.util.Scanner;
public class LongestIncreasingSeq {
private static int binarySearch(int table[],int a,int len){
int end = len-1;
int beg = 0;
int mid = 0;
int result = -1;
while(beg <= end){
mid = (end + beg) / 2;
if(table[mid] < a){
beg=mid+1;
result = mid;
}else if(table[mid] == a){
return len-1;
}else{
end = mid-1;
}
}
return result;
}
public static void main(String[] args) {
// int[] t = {1, 2, 5,9,16};
// System.out.println(binarySearch(t , 9, 5));
Scanner in = new Scanner(System.in);
int size = in.nextInt();//4;
int A[] = new int[size];
int table[] = new int[A.length];
int k = 0;
while(k<size){
A[k++] = in.nextInt();
if(k<size-1)
in.nextLine();
}
table[0] = A[0];
int len = 1;
for (int i = 1; i < A.length; i++) {
if(table[0] > A[i]){
table[0] = A[i];
}else if(table[len-1]<A[i]){
table[len++]=A[i];
}else{
table[binarySearch(table, A[i],len)+1] = A[i];
}
}
System.out.println(len);
}
}
यह O (n ^ 2) में जावा कार्यान्वयन है। मैंने अभी S में सबसे छोटे तत्व को खोजने के लिए बाइनरी सर्च का उपयोग नहीं किया है, जो कि X की तुलना में> = है। मैंने सिर्फ लूप के लिए उपयोग किया है। द्विआधारी खोज का उपयोग ओ (एन लोगन) में जटिलता बना देगा
public static void olis(int[] seq){
int[] memo = new int[seq.length];
memo[0] = seq[0];
int pos = 0;
for (int i=1; i<seq.length; i++){
int x = seq[i];
if (memo[pos] < x){
pos++;
memo[pos] = x;
} else {
for(int j=0; j<=pos; j++){
if (memo[j] >= x){
memo[j] = x;
break;
}
}
}
//just to print every step
System.out.println(Arrays.toString(memo));
}
//the final array with the LIS
System.out.println(Arrays.toString(memo));
System.out.println("The length of lis is " + (pos + 1));
}
सरणी तत्वों के साथ सबसे लंबे समय तक बढ़ने के लिए जावा में कोड चेकआउट करें
/**
** Java Program to implement Longest Increasing Subsequence Algorithm
**/
import java.util.Scanner;
/** Class LongestIncreasingSubsequence **/
class LongestIncreasingSubsequence
{
/** function lis **/
public int[] lis(int[] X)
{
int n = X.length - 1;
int[] M = new int[n + 1];
int[] P = new int[n + 1];
int L = 0;
for (int i = 1; i < n + 1; i++)
{
int j = 0;
/** Linear search applied here. Binary Search can be applied too.
binary search for the largest positive j <= L such that
X[M[j]] < X[i] (or set j = 0 if no such value exists) **/
for (int pos = L ; pos >= 1; pos--)
{
if (X[M[pos]] < X[i])
{
j = pos;
break;
}
}
P[i] = M[j];
if (j == L || X[i] < X[M[j + 1]])
{
M[j + 1] = i;
L = Math.max(L,j + 1);
}
}
/** backtrack **/
int[] result = new int[L];
int pos = M[L];
for (int i = L - 1; i >= 0; i--)
{
result[i] = X[pos];
pos = P[pos];
}
return result;
}
/** Main Function **/
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Longest Increasing Subsequence Algorithm Test\n");
System.out.println("Enter number of elements");
int n = scan.nextInt();
int[] arr = new int[n + 1];
System.out.println("\nEnter "+ n +" elements");
for (int i = 1; i <= n; i++)
arr[i] = scan.nextInt();
LongestIncreasingSubsequence obj = new LongestIncreasingSubsequence();
int[] result = obj.lis(arr);
/** print result **/
System.out.print("\nLongest Increasing Subsequence : ");
for (int i = 0; i < result.length; i++)
System.out.print(result[i] +" ");
System.out.println();
}
}
इसे डायनेमिक प्रोग्रामिंग का उपयोग करके O (n ^ 2) में हल किया जा सकता है।
इनपुट तत्वों को क्रम में संसाधित करें और प्रत्येक तत्व के लिए टुपल्स की एक सूची बनाए रखें। तत्व के लिए प्रत्येक टपल (ए, बी), मैं दर्शाता है, ए = सबसे लंबे समय तक बढ़ने वाले उप-अनुक्रम की समाप्ति i और B = सूची के पूर्ववर्ती सूची का सूचकांक [i] सूची में सबसे लंबे समय तक बढ़ते उप-अनुक्रम में समाप्त होता है [i] ]।
तत्व 1 से शुरू करें, तत्व 1 के लिए टपल की सूची होगी [(1,0)] तत्व i के लिए, सूची को स्कैन करें 0..i और तत्व सूची को खोजें [k] जैसे कि सूची [k] <सूची [i] ए के लिए ए का मान i, ऐ का अक + 1 होगा और बी का k होगा। यदि ऐसे कई तत्व हैं, तो उन्हें तत्व i के लिए tuples की सूची में जोड़ें।
अंत में, सूची प्राप्त करने के लिए ट्यूपल्स का उपयोग करके ए (अधिकतम एलआईएस की लंबाई की लंबाई) और बैकट्रैक के साथ सभी तत्वों को खोजें।
मैंने http://www.edufyme.com/code/?id=66f041e16a60928b05a7e228a89c3799 पर उसी के लिए कोड साझा किया है
O (n ^ 2) जावा कार्यान्वयन:
void LIS(int arr[]){
int maxCount[]=new int[arr.length];
int link[]=new int[arr.length];
int maxI=0;
link[0]=0;
maxCount[0]=0;
for (int i = 1; i < arr.length; i++) {
for (int j = 0; j < i; j++) {
if(arr[j]<arr[i] && ((maxCount[j]+1)>maxCount[i])){
maxCount[i]=maxCount[j]+1;
link[i]=j;
if(maxCount[i]>maxCount[maxI]){
maxI=i;
}
}
}
}
for (int i = 0; i < link.length; i++) {
System.out.println(arr[i]+" "+link[i]);
}
print(arr,maxI,link);
}
void print(int arr[],int index,int link[]){
if(link[index]==index){
System.out.println(arr[index]+" ");
return;
}else{
print(arr, link[index], link);
System.out.println(arr[index]+" ");
}
}
def longestincrsub(arr1):
n=len(arr1)
l=[1]*n
for i in range(0,n):
for j in range(0,i) :
if arr1[j]<arr1[i] and l[i]<l[j] + 1:
l[i] =l[j] + 1
l.sort()
return l[-1]
arr1=[10,22,9,33,21,50,41,60]
a=longestincrsub(arr1)
print(a)
भले ही एक ऐसा तरीका है जिसके द्वारा आप इसे O (nlogn) समय में हल कर सकते हैं (यह O (n ^ 2) समय में हल करता है) लेकिन फिर भी यह तरीका गतिशील प्रोग्रामिंग दृष्टिकोण देता है जो अच्छा भी है।
यहां बाइनरी सर्च का उपयोग करके मेरा लेटकोड समाधान है: ->
class Solution:
def binary_search(self,s,x):
low=0
high=len(s)-1
flag=1
while low<=high:
mid=(high+low)//2
if s[mid]==x:
flag=0
break
elif s[mid]<x:
low=mid+1
else:
high=mid-1
if flag:
s[low]=x
return s
def lengthOfLIS(self, nums: List[int]) -> int:
if not nums:
return 0
s=[]
s.append(nums[0])
for i in range(1,len(nums)):
if s[-1]<nums[i]:
s.append(nums[i])
else:
s=self.binary_search(s,nums[i])
return len(s)
#include <iostream>
#include "vector"
using namespace std;
// binary search (If value not found then it will return the index where the value should be inserted)
int ceilBinarySearch(vector<int> &a,int beg,int end,int value)
{
if(beg<=end)
{
int mid = (beg+end)/2;
if(a[mid] == value)
return mid;
else if(value < a[mid])
return ceilBinarySearch(a,beg,mid-1,value);
else
return ceilBinarySearch(a,mid+1,end,value);
return 0;
}
return beg;
}
int lis(vector<int> arr)
{
vector<int> dp(arr.size(),0);
int len = 0;
for(int i = 0;i<arr.size();i++)
{
int j = ceilBinarySearch(dp,0,len-1,arr[i]);
dp[j] = arr[i];
if(j == len)
len++;
}
return len;
}
int main()
{
vector<int> arr {2, 5,-1,0,6,1,2};
cout<<lis(arr);
return 0;
}
OUTPUT:
4
सबसे लंबे समय तक बढ़ते परिणाम (जावा)
import java.util.*;
class ChainHighestValue implements Comparable<ChainHighestValue>{
int highestValue;
int chainLength;
ChainHighestValue(int highestValue,int chainLength) {
this.highestValue = highestValue;
this.chainLength = chainLength;
}
@Override
public int compareTo(ChainHighestValue o) {
return this.chainLength-o.chainLength;
}
}
public class LongestIncreasingSubsequenceLinkedList {
private static LinkedList<Integer> LongestSubsequent(int arr[], int size){
ArrayList<LinkedList<Integer>> seqList=new ArrayList<>();
ArrayList<ChainHighestValue> valuePairs=new ArrayList<>();
for(int i=0;i<size;i++){
int currValue=arr[i];
if(valuePairs.size()==0){
LinkedList<Integer> aList=new LinkedList<>();
aList.add(arr[i]);
seqList.add(aList);
valuePairs.add(new ChainHighestValue(arr[i],1));
}else{
try{
ChainHighestValue heighestIndex=valuePairs.stream().filter(e->e.highestValue<currValue).max(ChainHighestValue::compareTo).get();
int index=valuePairs.indexOf(heighestIndex);
seqList.get(index).add(arr[i]);
heighestIndex.highestValue=arr[i];
heighestIndex.chainLength+=1;
}catch (Exception e){
LinkedList<Integer> aList=new LinkedList<>();
aList.add(arr[i]);
seqList.add(aList);
valuePairs.add(new ChainHighestValue(arr[i],1));
}
}
}
ChainHighestValue heighestIndex=valuePairs.stream().max(ChainHighestValue::compareTo).get();
int index=valuePairs.indexOf(heighestIndex);
return seqList.get(index);
}
public static void main(String[] args){
int arry[]={5,1,3,6,11,30,32,5,3,73,79};
//int arryB[]={3,1,5,2,6,4,9};
LinkedList<Integer> LIS=LongestSubsequent(arry, arry.length);
System.out.println("Longest Incrementing Subsequence:");
for(Integer a: LIS){
System.out.print(a+" ");
}
}
}
मैंने डायनामिक प्रोग्रामिंग और मेमोइज़ेशन का उपयोग करके जावा में एलआईएस लागू किया है। कोड के साथ मैंने जटिलता गणना की है अर्थात यह O (n लॉग (base2) n) क्यों है। जैसा कि मुझे लगता है कि सैद्धांतिक या तार्किक व्याख्याएं अच्छी हैं लेकिन समझ के लिए व्यावहारिक प्रदर्शन हमेशा बेहतर होता है।
package com.company.dynamicProgramming;
import java.util.HashMap;
import java.util.Map;
public class LongestIncreasingSequence {
static int complexity = 0;
public static void main(String ...args){
int[] arr = {10, 22, 9, 33, 21, 50, 41, 60, 80};
int n = arr.length;
Map<Integer, Integer> memo = new HashMap<>();
lis(arr, n, memo);
//Display Code Begins
int x = 0;
System.out.format("Longest Increasing Sub-Sequence with size %S is -> ",memo.get(n));
for(Map.Entry e : memo.entrySet()){
if((Integer)e.getValue() > x){
System.out.print(arr[(Integer)e.getKey()-1] + " ");
x++;
}
}
System.out.format("%nAnd Time Complexity for Array size %S is just %S ", arr.length, complexity );
System.out.format( "%nWhich is equivalent to O(n Log n) i.e. %SLog(base2)%S is %S",arr.length,arr.length, arr.length * Math.ceil(Math.log(arr.length)/Math.log(2)));
//Display Code Ends
}
static int lis(int[] arr, int n, Map<Integer, Integer> memo){
if(n==1){
memo.put(1, 1);
return 1;
}
int lisAti;
int lisAtn = 1;
for(int i = 1; i < n; i++){
complexity++;
if(memo.get(i)!=null){
lisAti = memo.get(i);
}else {
lisAti = lis(arr, i, memo);
}
if(arr[i-1] < arr[n-1] && lisAti +1 > lisAtn){
lisAtn = lisAti +1;
}
}
memo.put(n, lisAtn);
return lisAtn;
}
}
जबकि मैंने उपरोक्त कोड -
Longest Increasing Sub-Sequence with size 6 is -> 10 22 33 50 60 80
And Time Complexity for Array size 9 is just 36
Which is equivalent to O(n Log n) i.e. 9Log(base2)9 is 36.0
Process finished with exit code 0
O (NLog (N)) सबसे लंबे समय तक बढ़ने वाले उप अनुक्रम को खोजने के लिए दृष्टिकोण
दें, हमें एक सरणी बनाए रखना चाहिए जहां ith तत्व सबसे छोटी संभव संख्या है जिसके साथ ai आकार का उप अनुक्रम समाप्त हो सकता है।
उद्देश्य पर मैं आगे के विवरणों से बच रहा हूं क्योंकि शीर्ष मतदान जवाब पहले से ही इसे समझाता है, लेकिन यह तकनीक अंततः सेट डेटा संरचना (कम से कम c ++) का उपयोग करके एक स्वच्छ कार्यान्वयन की ओर ले जाती है।
यहाँ c ++ में कार्यान्वयन है (यह मानकर कड़ाई से सबसे लंबे उप अनुक्रम आकार की आवश्यकता है)
#include <bits/stdc++.h> // gcc supported header to include (almost) everything
using namespace std;
typedef long long ll;
int main()
{
ll n;
cin >> n;
ll arr[n];
set<ll> S;
for(ll i=0; i<n; i++)
{
cin >> arr[i];
auto it = S.lower_bound(arr[i]);
if(it != S.end())
S.erase(it);
S.insert(arr[i]);
}
cout << S.size() << endl; // Size of the set is the required answer
return 0;
}