You are on page 1of 7

Khandesh College Education Society’s

College of Engineering & Information Technology, Jalgaon


Department of Computer Engineering
PRN No.: 21510620171124510019 Roll No: 16 Date of Exp:
Class: TE Comp. Sign of Staff Member:
=================================================================
EXPERIMENT NO. 4
Title:- Tug of War.

Aim:- A problem to divide a set of number to two different set where both the subset contains
same number of element and have a minimum difference of sum between them using
backtracking.

Theory:-

In this problem a set of integers are given, we have to break them into two parts, such that the
difference of the sum of two subsets is minimum as possible. So our target is to divide two
groups of nearly equal strength to participate in the Tug of war game.

If the size of subset n is even, it must be divided into n/2, but for the odd value of n, then the size
of one subset must be (n-1)/2, and size of another subset must be (n+1)/2.

There is a set contains N number of elements. We have to divide this set into two different sets
where each of the subsets contains the same number of elements and has a minimum difference
between them.

1. If N is even then each of the subsets will contain N/2 number of elements.
2. If N is odd then one of the subsets will contain (N+1)/2 and others will contain (N-
1)/2 number of elements.

Explanation:-

let given set be {3, 4, 5, -3, 100, 1, 89, 54, 23, 20}, the size of set is 10. Output for this set
should be {4, 100, 1, 23, 20} and {3, 5, -3, 89, 54}. Both output subsets are of size 5 and sum of
elements in both subsets is same (148 and 148).Let us consider another example where n is odd.
Let given set be {23, 45, -34, 12, 0, 98, -99, 4, 189, -1, 4}. The output subsets should be {45, -
34, 12, 98, -1} and {23, 0, -99, 4, 189, 4}. The sums of elements in two subsets are 120 and 121
respectively.
Input

The input begins with a single positive integer on a line by itself indicating the number of the
cases following, each of them as described below. This line is followed by a blank line, and
there is also a blank line between two consecutive inputs.The first line of input contains n the
number of people at the picnic. n lines follow. The first linegives the weight of person 1; the
second the weight of person 2; and so on. Each weight is an integer between 1 and 450. There
are at most 100 people at the picnic.

Output

For each test case, the output must follow the description below. The outputs of two consecutive
cases will be separated by a blank line.Your output will be a single line containing 2 numbers:
the total weight of the people on one team,and the total weight of the people on the other team.
If these numbers differ, give the lesser first.

Example

T=3
N=3
123

Output: set1: {1, 2} set2: {3}

N=4
1234

Output: set1: {1, 4} set2: {2, 3}

N=10
1 4 8 6 -7 -10 87 54 16 100
Output: set1: {{ 4, -7, -10, 87, 54 } set2: {1, 8, 6, 16, 100 }

Sample Input
1
3
100
90
200
Sample Output
190 200
Conclusion:
In this problem to divide a set of number to two different set where both the subset contains
same number of element and have a minimum difference of sum.
/*Student Name: Hitesh Raghunath Dhake Class: T.E
Roll_no: 16 Batch: T1

Program Name:- Tug of War

# Python program for above approach

# function that tries every possible


# solution by calling itself recursively
def TOWUtil(arr, n, curr_elements, no_of_selected_elements,
soln, min_diff, Sum, curr_sum, curr_position):

# checks whether the it is going


# out of bound
if (curr_position == n):
return

# checks that the numbers of elements


# left are not less than the number of
# elements required to form the solution
if ((int(n / 2) - no_of_selected_elements) >
(n - curr_position)):
return

# consider the cases when current element


# is not included in the solution
TOWUtil(arr, n, curr_elements, no_of_selected_elements,
soln, min_diff, Sum, curr_sum, curr_position + 1)

# add the current element to the solution


no_of_selected_elements += 1
curr_sum = curr_sum + arr[curr_position]
curr_elements[curr_position] = True

# checks if a solution is formed


if (no_of_selected_elements == int(n / 2)):
# checks if the solution formed is better
# than the best solution so far
if (abs(int(Sum / 2) - curr_sum) < min_diff[0]):
min_diff[0] = abs(int(Sum / 2) - curr_sum)
for i in range(n):
soln[i] = curr_elements[i]
else:

# consider the cases where current


# element is included in the solution
TOWUtil(arr, n, curr_elements, no_of_selected_elements,
soln, min_diff, Sum, curr_sum, curr_position + 1)

# removes current element before returning


# to the caller of this function
curr_elements[curr_position] = False

# main function that generate an arr


def tugOfWar(arr, n):

# the boolean array that contains the


# inclusion and exclusion of an element
# in current set. The number excluded
# automatically form the other set
curr_elements = [None] * n

# The inclusion/exclusion array


# for final solution
soln = [None] * n

min_diff = [999999999999]

Sum = 0
for i in range(n):
Sum += arr[i]
curr_elements[i] = soln[i] = False
# Find the solution using recursive
# function TOWUtil()
TOWUtil(arr, n, curr_elements, 0,
soln, min_diff, Sum, 0, 0)

# Print the solution


print("The first subset is: ")
for i in range(n):
if (soln[i] == True):
print(arr[i], end = " ")
print()
print("The second subset is: ")
for i in range(n):
if (soln[i] == False):
print(arr[i], end = " ")

# Driver Code
if __name__ == '__main__':

arr = [23, 45, -34, 12, 0, 98,


-99, 4, 189, -1, 4]
n = len(arr)
tugOfWar(arr, n)
Output:-

You might also like