I need to find the length of the longest strictly increasing subsequence array from a given array I just need a tip to start. Any type of help will be appreciated
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Haseeb Hashim
el 27 de Mzo. de 2022
Editada: Bruno Luong
el 28 de Mzo. de 2022
How do I start to solve this problem A simple check on array for larger number than the previous one will not work in this case as we have to fin dthe longest strickly increasing subsequence..........
0 comentarios
Respuesta aceptada
Bruno Luong
el 28 de Mzo. de 2022
Editada: Bruno Luong
el 28 de Mzo. de 2022
Dynamic programming, it may take longtime for large array
a = [0,1,2,3,4,1,2,0,4,5,8,9,11,11,4,4,9,9];
i = longestsubseq_helper(a, -Inf)
a(i)
function [i] = longestsubseq_helper(a, lo)
i = [];
maxlength = -Inf;
for j=find(a>lo)
ij = longestsubseq_helper(a(j+1:end), a(j));
if length(ij) > maxlength
maxlength = length(ij);
i = j + [0 ij];
end
end
end
4 comentarios
Torsten
el 28 de Mzo. de 2022
I did not yet try to exactly understand the code, but under octave, I get
i =
1 2 3 4 5 10 11 12 13
Strange, isn't it ?
Bruno Luong
el 28 de Mzo. de 2022
Editada: Bruno Luong
el 28 de Mzo. de 2022
It matches MATLAB result, i returned is the subsequence index, you need to call
a(i)
to get the subsequence values from the original array.
Más respuestas (5)
Torsten
el 28 de Mzo. de 2022
Editada: Torsten
el 28 de Mzo. de 2022
Here is another code that does what the OP wants (not my own work :-))
Maybe interesting for speed comparisons.
a = [0,1,2,3,4,1,2,0,4,5,8,9,11,11,4,4,9,9];
% l: length of the longest strictly monotoneous subsequence
% imon: index vector
% amon: a(imon)
[l,imon,amon] = longestsubseq(a)
function [l,imon,amon] = longestsubseq(a)
n = numel(a);
L = zeros(1,n);
P = zeros(1,n);
for i = 1:n
L(i) = 1;
P(i) = 0;
for j = 1:i-1
if a(j) < a(i) && L(j) >= L(i)
L(i) = L(j) + 1;
P(i) = j;
end
end
end
[l,k] = max(L);
i = 1;
j = k;
imon = zeros(1,l);
while j~=0
imon(i) = j;
j = P(j);
i = i+1;
end
imon = fliplr(imon);
amon = a(imon);
end
7 comentarios
Torsten
el 28 de Mzo. de 2022
I would have inserted comments if I had understood the code :-)
Bruno Luong
el 28 de Mzo. de 2022
Editada: Bruno Luong
el 28 de Mzo. de 2022
I understand the code. In the next "iss" stands for incraseing subsequence, and "liss" stands for longest incraseing subsequence,
L(i) element of L array stores the length of the liss that terminates with a(i).
P(i) keeps track the previous index element (before the last a(i)) of this liss that terminates with a(i).
The first double-loop just computes L and P recursively.
The max command find the longest liss among all liss computed previously (it must terminate somwewhere right?).
Then the while loop just look back to unwrap the sequence from P.
This is O(n^2) algorithm. Not the best one according to wikipediea.
John D'Errico
el 27 de Mzo. de 2022
- Can you use diff on your vector? What property does the result of diff have on strictly increasing sequences? Will all elements in such a sequence be greater than zero?
- Can you test to see if the elements of a vector are greater than zero?
v = rand(1,10)
So what would this do?
result = diff(v) > 0
Are you closer to the result you need? Yes. What property does the increasing subsequence have now? You must now find a ssequence where result is all 1. The LONGEST such subsequence corresponds to the subsequence of interest. Add 1 to that length to know the number of elements in the increasing sequence.
So now we know that the longest subsequence starts at element 3 in the original vector. The end of that subsequence is at element 6.
Now look back at my original vector v. Is that true?
If the sequence need only be non-decreasing, so you could have sequenctial elements that are identical, then just change the test to be:
result = diff(v) >= 0;
Now your problem is simpler. Find all sequences of consecutive ones. Choose the longest such sequence. I'll give you one more hint. What does this do?
startind = strfind([0,result],[0 1])
Can you find the end of all such subsequences in a similar way? How would you need to change that last call?
endind = strfind([result,0],[1 0]) + 1
3 comentarios
John D'Errico
el 27 de Mzo. de 2022
Editada: John D'Errico
el 27 de Mzo. de 2022
The question was not specific, as to if that qualifies.
The question DID mention a comparison to the previous element, so one might presume that a subsequence must be contiguous. Until there is a statement made to the contrary, we need to make wild guesses about intention.
Bruno Luong
el 27 de Mzo. de 2022
Editada: Bruno Luong
el 28 de Mzo. de 2022
a=rand(1,1000000);
idx=find(diff([false diff(a)>0 false]));
[lgtmax, j]=max(idx(2:2:end)-idx(1:2:end));
longestincreasedsubarray = a(idx(2*j-1)+(0:lgtmax-1))
4 comentarios
Bruno Luong
el 28 de Mzo. de 2022
The above code give longest subarray and not subsequence.
Image Analyst
el 27 de Mzo. de 2022
If you have the Image Processing Toolbox (type ver to check if you do), then you can use regionprops():
v = [0,1,2,3,4,1,2,0,4,5,8,9,11,11,4,4,9,9]
% Find out which ones are increasing.
mask = diff(v) > 0;
% Measure lengths of all runs of increasing or stable values.
props = regionprops(mask, 'Area', 'PixelList');
[maxLength, index] = max([props.Area]);
% Determine the longest length
maxLength = maxLength + 1
% Get the indexes over which the run spans:
indexes = [props(index).PixelList(:, 1); props(index).PixelList(end,1) + 1]'
% Determine the values of the vector in that longest run:
values = v(indexes)
9 comentarios
Torsten
el 27 de Mzo. de 2022
Yes, I also tend to your interpretation because the other task would be too hard for an assignment :-)
Image Analyst
el 28 de Mzo. de 2022
@Haseeb Hashim, how about just a simple for loop:
% Initialize arrays
v = [0,1,2,3,4,1,2,0,4,5,8,9,11,11,4,4,9,9];
outputIndexes = -inf * ones(size(v));
outputValue = -inf *ones(size(v));
index = 1;
outputIndexes(1) = 1; % Initialize first elements.
outputValue(1) = v(1);
% Loop trying to find elements that are increasing.
for k = 2 : length(v)
if v(k) > outputValue(index)
% This element is greater than the last greatest one we had found, so record it.
index = index + 1;
outputIndexes(index) = k;
outputValue(index) = v(k);
end
end
% Crop off trailing infs.
outputIndexes = outputIndexes(1 : index)
outputValue = outputValue(1 : index)
1 comentario
Torsten
el 28 de Mzo. de 2022
Editada: Torsten
el 28 de Mzo. de 2022
Doesn't seem to work:
a = [ 0.655006 0.239884 0.873047 0.421119 0.837472 0.971189 0.600860 0.052831 ...
0.644774 0.491795 0.521089 0.587750 0.766062 0.207846 0.108566 0.924829 ...
0.249810 0.500503 0.075515 0.612612]
Output:
outputIndexes =
1 3 6
outputValue =
0.6550 0.8730 0.9712
Correct subsequence:
2 4 10 11 12 13 16
0.2399 0.4211 0.4918 0.5211 0.5878 0.7661 0.9248
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!