How to resolve the algorithm Longest increasing subsequence step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Longest increasing subsequence step by step in the Prolog programming language

Table of Contents

Problem Statement

Calculate and show here a longest increasing subsequence of the list: And of the list: Note that a list may have more than one subsequence that is of the maximum length.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Longest increasing subsequence step by step in the Prolog programming language

Source code in the prolog programming language

lis(In, Out) :-
	% we ask Prolog to find the longest sequence
	aggregate(max(N,Is), (one_is(In, [], Is), length(Is, N)), max(_, Res)),
	reverse(Res, Out).


% we describe the way to find increasing subsequence
one_is([], Current, Current).


one_is([H | T], Current, Final) :-
	(   Current = [], one_is(T, [H], Final));
	(   Current = [H1 | _], H1 < H,   one_is(T, [H | Current], Final));
	one_is(T, Current, Final).


  

You may also check:How to resolve the algorithm Mertens function step by step in the BASIC programming language
You may also check:How to resolve the algorithm Taxicab numbers step by step in the Ring programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Erlang programming language
You may also check:How to resolve the algorithm Semiprime step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Red programming language