Hi everyone,
Im trying to code the Longest Decreasing Subsequences out of a given List in prolog.
The problem is is that i get duplicates of the answers.
here is my code:
sublist([],[]).
sublist([X|R],[X|S]):- sublist(R,S).
sublist([_|R],S):- sublist(R,S).
% longest(List,SubList)/2 with mode longest(+,-) : SubList will be instantiated
% to a sublist of List which is decreasing and of maximal length.
longest(List,SubList) :- setof([Len,DecrSub],decreasing_sublist(List,Len,DecrSub),Ye), reverse(Ye,Y),funky(Y,SubList).
funky([X|_],Ans) :- last(X,Ans).
funky([[X,_],[Y,Ys]|Zs],Ans) :- X == Y, funky([[Y,Ys]|Zs],Ans).
decreasing_sublist(List,Len,DecrSub) :-
sublist(List,DecrSub),
sort(DecrSub,DecrSubSorted),
reverse(DecrSubSorted,DecrSub),
length(DecrSub,Len).
this is the output i get:
64 ?- longest([5,7,4,9,3,2,1],SubList).
SubList = [7, 4, 3, 2, 1] ;
SubList = [5, 4, 3, 2, 1] ;
SubList = [7, 4, 3, 2, 1] ;
SubList = [5, 4, 3, 2, 1] ;
false.
65 ?- setof([Len,DecrSub],decreasing_sublist([5,7,4,9,3,2,1],Len,DecrSub),Ye), reverse(Ye,Y),funky(Y,SubList).
Ye = [[0, []], [1, [1]], [1, [2]], [1, [3]], [1, [4]], [1, [5]], [1, [...]], [1|...], [...|...]|...],
Y = [[5, [7, 4, 3, 2, 1]], [5, [5, 4, 3, 2, 1]], [4, [9, 3, 2, 1]], [4, [7, 4, 3|...]], [4, [7, 4|...]], [4, [7|...]], [4, [...|...]], [4|...], [...|...]|...],
SubList = [7, 4, 3, 2, 1] ;
Ye = [[0, []], [1, [1]], [1, [2]], [1, [3]], [1, [4]], [1, [5]], [1, [...]], [1|...], [...|...]|...],
Y = [[5, [7, 4, 3, 2, 1]], [5, [5, 4, 3, 2, 1]], [4, [9, 3, 2, 1]], [4, [7, 4, 3|...]], [4, [7, 4|...]], [4, [7|...]], [4, [...|...]], [4|...], [...|...]|...],
SubList = [5, 4, 3, 2, 1] ;
false.
as you can see in 64:- i get duplicates.
but when i take the line of my code and use it, i dont get duplicates.
does someone know how i can fix this?
thanks so much
Yarden.
Im trying to code the Longest Decreasing Subsequences out of a given List in prolog.
The problem is is that i get duplicates of the answers.
here is my code:
sublist([],[]).
sublist([X|R],[X|S]):- sublist(R,S).
sublist([_|R],S):- sublist(R,S).
% longest(List,SubList)/2 with mode longest(+,-) : SubList will be instantiated
% to a sublist of List which is decreasing and of maximal length.
longest(List,SubList) :- setof([Len,DecrSub],decreasing_sublist(List,Len,DecrSub),Ye), reverse(Ye,Y),funky(Y,SubList).
funky([X|_],Ans) :- last(X,Ans).
funky([[X,_],[Y,Ys]|Zs],Ans) :- X == Y, funky([[Y,Ys]|Zs],Ans).
decreasing_sublist(List,Len,DecrSub) :-
sublist(List,DecrSub),
sort(DecrSub,DecrSubSorted),
reverse(DecrSubSorted,DecrSub),
length(DecrSub,Len).
this is the output i get:
64 ?- longest([5,7,4,9,3,2,1],SubList).
SubList = [7, 4, 3, 2, 1] ;
SubList = [5, 4, 3, 2, 1] ;
SubList = [7, 4, 3, 2, 1] ;
SubList = [5, 4, 3, 2, 1] ;
false.
65 ?- setof([Len,DecrSub],decreasing_sublist([5,7,4,9,3,2,1],Len,DecrSub),Ye), reverse(Ye,Y),funky(Y,SubList).
Ye = [[0, []], [1, [1]], [1, [2]], [1, [3]], [1, [4]], [1, [5]], [1, [...]], [1|...], [...|...]|...],
Y = [[5, [7, 4, 3, 2, 1]], [5, [5, 4, 3, 2, 1]], [4, [9, 3, 2, 1]], [4, [7, 4, 3|...]], [4, [7, 4|...]], [4, [7|...]], [4, [...|...]], [4|...], [...|...]|...],
SubList = [7, 4, 3, 2, 1] ;
Ye = [[0, []], [1, [1]], [1, [2]], [1, [3]], [1, [4]], [1, [5]], [1, [...]], [1|...], [...|...]|...],
Y = [[5, [7, 4, 3, 2, 1]], [5, [5, 4, 3, 2, 1]], [4, [9, 3, 2, 1]], [4, [7, 4, 3|...]], [4, [7, 4|...]], [4, [7|...]], [4, [...|...]], [4|...], [...|...]|...],
SubList = [5, 4, 3, 2, 1] ;
false.
as you can see in 64:- i get duplicates.
but when i take the line of my code and use it, i dont get duplicates.
does someone know how i can fix this?
thanks so much
Yarden.