Question

In: Computer Science

Sets can be though of as lists that don't contain any repeated elements. For example, [a,4,6]...

Sets can be though of as lists that don't contain any repeated elements. For example, [a,4,6] is a set, but [a,4,6,a] is not (as it contains 2 occurrences of a). Write a Prolog program, powerset/2 that generates the set of all subsets of a list in sorted order. (Note the input list may not, itself, be a set.) Example1: powerset([a,b],X). should yield: X = [[],[a],[a,b],[b],[b,a]]. Example2: powerset([a,b,a],X). should also yield (note input list is not actually a set): X = [[],[a],[a,b],[b],[b,a]]. Example3: powerset([a,b,a,c],X). should yield: X = [[], [a], [a, b], [a, b, c], [a, c], [a, c, b], [b], [b, a], [b, a, c], [b, c], [b, c, a], [c], [c, a], [c, a, b], [c, b], [c, b, a]]. Also, the predicate should terminate execution immediately after producing its first (and only) solution.

Solutions

Expert Solution

%% ?- subset([a,b],[a,b,c])
%% yes
%% ?- subset([c,b],[a,b,c])
%% yes
%% ?- subset([],[a,b,c])
%% yes
%% Your program should be capable of generating all subsets of an input set by
%% backtracking. For example, if you give it as input
%% ?- subset(X,[a,b,c])
%% it should successively generate all eight subsets of [a,b,c].
%% Generates all subsets of the given set (once each)
%% base case
subset_gen([], []).
%% inductive case
subset_gen(Subset, [H | Set]) :- subset_gen(Subset, Set).
subset_gen([H |Subset], [H | Set]) :- subset_gen(Subset, Set).
%% Checks if the first argument is a subset of the second
%% base case
subset_check([], _).
%% inductive case
subset_check([H | Subset], Set) :-
member(H, Set),
subset_check(Subset, Set).
%% main
subset(Subset, Set) :-
var(Subset),
subset_gen(Subset, Set).
subset(Subset, Set) :-
nonvar(Subset),
subset_check(Subset, Set).
%% 2. Using the subset predicate you have just written, and findall/3 , write a
%% predicate powerset/2 that takes a set as its first argument, and returns the
%% powerset of this set as the second argument. (The powerset of a set is the
%% set of all its subsets.) For example:
%% ?- powerset([a,b,c],P)
%% should return
%% P = [[],[a],[b],[c],[a,b],[a,c],[b,c],[a,b,c]]
%% It doesn’t matter if the sets are returned in some other order. For example,
%% P = [[a],[b],[c],[a,b,c],[],[a,b],[a,c],[b,c]]
%% is fine too
%% main
powerset(Set, Powerset) :- setof(Subset, subset(Subset, Set), Powerset).

Related Solutions

Write a very general sort method that can sort any type of data arrays/lists. For example,...
Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language doesn’t support...
Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the...
Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the same sorting algorithms as discussed in class / in the textbook. (a) What is the running time of Insertion Sort when all elements in a given list of length N are equal? Explain your answer. (b) Give a Θ-bound on the running time of Mergesort for the case that all elements in a given list of length N are equal (assuming N is a...
Define the subtraction predicate: set_difference(Set1,Set2,SetDifference) where all the three sets are represented as lists. For example,...
Define the subtraction predicate: set_difference(Set1,Set2,SetDifference) where all the three sets are represented as lists. For example, set_difference([a,b,c,d],[b,d,e,f],[a,c]). should return true. Also, the predicate should terminate as soon as the first solution is found when unifying variables for these arguments. That is, set_difference([a,b,c,d],[b,d,e,f],X). should terminate immediately after finding the first solution, X = [a,c].
Can bank fraud be committed even though the fraudster does not obtain any money or other...
Can bank fraud be committed even though the fraudster does not obtain any money or other benefit from the bank? Use details to support your answer.
The user enters some text into a textbox. It can contain any characters (letters, digits, spaces,...
The user enters some text into a textbox. It can contain any characters (letters, digits, spaces, punctuation marks, and there is no length limit). When a button Count is hit , display the number of upper-case letters in the inputted phrase. This must be done in visual studios using C#.
Find an example of application of Hypothesis testing that you can relate to (any example would...
Find an example of application of Hypothesis testing that you can relate to (any example would be OK using Z test you can consult HW Batch No. 4 or textbook). Show all five steps of Hypothesis testing with numerical example (you can use fictitious numbers if you have to). And find the P-Value and re-affirm Step 4 with P-Value.
What is meant by self consistency ? can you give any real life example or example...
What is meant by self consistency ? can you give any real life example or example from physics to explain self consistency ?
USE JAVA. Write a very general sort method that can sort any type of data arrays/lists....
USE JAVA. Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language...
Code: C++ Write a very general sort method that can sort any type of data arrays/lists....
Code: C++ Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language...
Can explain 5 point. What engineering sustainability do for contain toxic/ harmful material with an example......
Can explain 5 point. What engineering sustainability do for contain toxic/ harmful material with an example... Thankyou..
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT