Create all the possible combinations of array a- Software Engineering

Create all the possible combinations of array a- Software Engineering

The subset-sum problem is defined as follows: given a set B of n positive integers and an integer K, can you find a subset of B whose elements’ summation is equal to K? Design an algorithm to solve this problem. Address its correctness and running time.

Input: set B of n positive integers {b1, b2,….., bn} and an integer K.

Output: whether there exist such a subset of B called B’ its elements summation is equal to K.

B’= BA, where A = {a1, a2,…….., an} in which AB= b1a1 +b2a2 +……+ bnan. Where ai is either 0 or 1.

Algorithm:
– For i= 1 to 2n (We have 2n different combinations set to be checked)
1. Create all the possible combinations of Array A and do:
– Compute Sum =
– If Sum = K then there is a subset sum to K. This subset B’= {b1a1, b2a2, ……, bnan}when ai representing 1.
– return the subset B’
– Otherwise return there is no subset sum to K.

The run time is O(2n) since it needs to go through all possible subsets to find the subset that sum to K.

Order from us and get better grades. We are the service you have been looking for.