use Backtracking so that the python program print the partitioned subsets.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
We will be looking at a problem known as the partition problem. The partition problem is to determine
if a given set can be partitioned into two subsets such that the sum of elements in both subsets is the
same. This is a variant of the 0-1 knapsack problem.

Instead of printing 'Set can be partitioned', use Backtracking so that the python program print the partitioned subsets.

Python program:

# Returns true if there exists a sublist of list `nums[0…n)` with the given sum
def subsetSum(nums, total):
 
    n = len(nums)
 
    # `T[i][j]` stores true if subset with sum `j` can be attained
    # using items up to first `i` items
    T = [[False for x in range(total + 1)] for y in range(n + 1)]
 
    # if the sum is zero
    for i in range(n + 1):
        T[i][0] = True
 
    # do for i'th item
    for i in range(1, n + 1):
 
        # consider all sum from 1 to total
        for j in range(1, total + 1):
 
            # don't include the i'th element if `j-nums[i-1]` is negative
            if nums[i - 1] > j:
                T[i][j] = T[i - 1][j]
            else:
                # find the subset with sum `j` by excluding or including the i'th item
                T[i][j] = T[i - 1][j] or T[i - 1][j - nums[i - 1]]
 
    # return maximum value
    return T[n][total]
 
 
# Returns true if given list `nums[0…n-1]` can be divided into two
# sublists with equal sum
def partition(nums):
 
    total = sum(nums)
 
    # return true if the sum is even and the list can be divided into
    # two sublists with equal sum
    return (total & 1) == 0 and subsetSum(nums, total // 2)
 
 
if __name__ == '__main__':
 
    # Input: a set of items
    nums = [7, 3, 1, 5, 4, 8]
 
    if partition(nums):
        print('Set can be partitioned')
    else:
        print('Set cannot be partitioned')
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY