Search code examples
treeprologsubtree

In Prolog program, generating false sub trees?


Im studying prolog in a course. For an exercise, I need in part to generate all subtrees of a given tree. The problem is it generates false subtrees

Here's the code

build_tree3(T):-
T=t(t(t(nil, -5, nil), 4, t(t(nil, 15, nil), -20, t(nil, 10, nil))), 2, t(nil, -8, t(t(nil, 9, nil),12,t(nil, 10, nil)))).

get_sol(Tree, List, N):-
without_root(Tree, List1, N1),
with_root(Tree, List2, N2),!,
max_set(List1, N1, List2, N2, List, N).

max_set(List1, N1, List2, N2, List, N) :-
    (N1>N2,List=List1,N=N1;
     N2>N1,List=List2,N=N2;
    N2=:=N1,N=N1,(List=List1;List=List2)).

without_root(nil, _, 0).
without_root(t(L, _, R), List, N):-
    get_sol(L, LeftList, LeftN),
    get_sol(R, RightList, RightN),
    append(LeftList, RightList, List),
    N is LeftN + RightN.


with_root(nil, [], 0).
with_root(t(L, X, R), [X|List], N):-
    with_root(L, LeftList, LeftN),
    with_root(R, RightList, RightN), 
    append(LeftList, RightList, List),
    N is LeftN + RightN + X.

Here's the console

build_tree3(T), get_sol(T, L, N).

The outcome of it is L = [15,10,12,9,10] , N = 56 ; when it is supposed to be L = [12,9,10] , N = 31;

the tree in normal view


Solution

  • Your solution returns L=[15,10,12,9,10] because it finds maximum independent set , it is not forced to return a subtree. You could change some parts as the code below:

    sol_tree(Tree,List,N):-
        sol_tree_noroot(Tree,L1,N1),
        sol_tree_withroot(Tree,L2,N2),!,
        max_set(L1,N1,L2,N2,List,N).
    
    max_set(List1, N1, List2, N2, List, N) :-
        (N1>N2,List=List1,N=N1;
         N2>N1,List=List2,N=N2;
        N2=:=N1,N=N1,(List=List1;List=List2)).    
    
    sol_tree_noroot(nil,[],0).
    sol_tree_noroot(t(L,_,R),List,N):-
          sol_tree(L,L1,N1),sol_tree(R,R1,N2),!,
          max_set(L1, N1, R1, N2, List, N).
    
    sol_tree_withroot(nil,[],0).
    sol_tree_withroot(t(L,X,R),[X|List],N3):-
         sol_tree_withroot(L,L1,N1),sol_tree_withroot(R,R1,N2),
         max_set2(L1,N1,R1,N2,List,N),
         N3 is N+X.
    
    max_set2(L1,N1,L2,N2,List,N):-
        (N1>0,N2>0,N is N1+N2,append(L1,L2,List);
         N1>=0,N2<0,N is N1 ,List=L1;
         N1<0,N2>=0,N is N2 ,List=L2;
         N1<0,N2<0,N1<N2,N is N2 ,List=L2;
         N1<0,N2<0,N1>N2,N is N1 ,List=L1;
         N1>0,N2=0,N is N1,(List=L1;append(L1,L2,List));
         N1=0,N2>0,N is N2,(List=L2;append(L1,L2,List));
         N1=0,N2=0,N is N1,(List=L1;List=L2;append(L1,L2,List))). 
    

    The idea is that you can skip the root to find a subtree or you can keep the root where in this case you can skip a root because then it will not be a subtree.