use Backtracking so that the python program print the partitioned subsets.
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...
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
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Recommended textbooks for you
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
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…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
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)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
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…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
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)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
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](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY