Python程序获取所有具有总和x的子集
我们得到一个包含 n 个数字和一个数字 x 的列表,任务是编写一个Python程序来找出列表中所有可能的子集,使得它们的总和为 x。
例子:
Input: arr = [2, 4, 5, 9], x = 15
Output: [2, 4, 9]
15 can be obtained by adding 2, 4 and 9 from the given list.
Input : arr = [10, 20, 25, 50, 70, 90], x = 80
Output : [10, 70]
[10, 20, 50]
80 can be obtained by adding 10 and 70 or by adding 10, 20 and 50 from the given list.
方法#1:
这是一种蛮力方法。找到给定列表的所有可能子集和并检查和是否等于 x。使用这种方法的时间复杂度为 O(2^n),非常大。
Python3
# Python code with time complexity
# O(2^n)to print all subsets whose
# sum is equal to a given value
from itertools import combinations
def subsetSum(n, arr, x):
# Iterating through all possible
# subsets of arr from lengths 0 to n:
for i in range(n+1):
for subset in combinations(arr, i):
# printing the subset if its sum is x:
if sum(subset) == x:
print(list(subset))
# Driver Code:
n = 6
arr = [10, 20, 25, 50, 70, 90]
x = 80
subsetSum(n, arr, x)
Python3
# Efficient Python code to
# print all subsets whose sum
# is equal to a given value
from itertools import combinations
def subsetSum(li, comb, sums):
# Iterating through all subsets of
# list li from length 0 to length of li:
for i in range(len(li)+1):
for subset in combinations(li, i):
# Storing all the subsets in list comb:
comb.append(list(subset))
# Storing the subset sums in list sums:
sums.append(sum(subset))
def calcSubsets(n, arr, x):
# Dividing the list arr into two lists
# arr1 and arr2 of about equal sizes
# by slicing list arr about index n//2:
arr1, arr2 = arr[:n//2], arr[n//2:]
# Creating empty lists comb1 and sums1
# to run the subsetSum function and
# store subsets of arr1 in comb1
# and the subset sums in sums1:
comb1, sums1 = [], []
subsetSum(arr1, comb1, sums1)
# Creating empty lists comb2 and sums2
# to run the subsetSum function and
# store subsets of arr2 in comb2
# and the subset sums in sums2:
comb2, sums2 = [], []
subsetSum(arr2, comb2, sums2)
# Iterating i through the indices of sums1:
for i in range(len(sums1)):
# Iterating j through the indices of sums2:
for j in range(len(sums2)):
# If two elements (one from sums1
# and one from sums2) add up to x,
# the combined list of elements from
# corresponding subsets at index i in comb1
# and j in comb2 gives us the required answer:
if sums1[i] + sums2[j] == x:
print(comb1[i] + comb2[j])
# Driver Code:
n = 6
arr = [10, 20, 25, 50, 70, 90]
x = 80
calcSubsets(n, arr, x)
输出:
[10, 70]
[10, 20, 50]
方法#2:
中间相遇是一种将搜索空间划分为两个大小相等的部分,对这两个部分进行单独搜索,然后组合搜索结果的技术。使用这种技术,两次搜索可能需要比一次大搜索更少的时间,并将时间复杂度从 O(2^n) 变成 O(2^(n/2))。
蟒蛇3
# Efficient Python code to
# print all subsets whose sum
# is equal to a given value
from itertools import combinations
def subsetSum(li, comb, sums):
# Iterating through all subsets of
# list li from length 0 to length of li:
for i in range(len(li)+1):
for subset in combinations(li, i):
# Storing all the subsets in list comb:
comb.append(list(subset))
# Storing the subset sums in list sums:
sums.append(sum(subset))
def calcSubsets(n, arr, x):
# Dividing the list arr into two lists
# arr1 and arr2 of about equal sizes
# by slicing list arr about index n//2:
arr1, arr2 = arr[:n//2], arr[n//2:]
# Creating empty lists comb1 and sums1
# to run the subsetSum function and
# store subsets of arr1 in comb1
# and the subset sums in sums1:
comb1, sums1 = [], []
subsetSum(arr1, comb1, sums1)
# Creating empty lists comb2 and sums2
# to run the subsetSum function and
# store subsets of arr2 in comb2
# and the subset sums in sums2:
comb2, sums2 = [], []
subsetSum(arr2, comb2, sums2)
# Iterating i through the indices of sums1:
for i in range(len(sums1)):
# Iterating j through the indices of sums2:
for j in range(len(sums2)):
# If two elements (one from sums1
# and one from sums2) add up to x,
# the combined list of elements from
# corresponding subsets at index i in comb1
# and j in comb2 gives us the required answer:
if sums1[i] + sums2[j] == x:
print(comb1[i] + comb2[j])
# Driver Code:
n = 6
arr = [10, 20, 25, 50, 70, 90]
x = 80
calcSubsets(n, arr, x)
输出:
[10, 70]
[10, 20, 50]