PHP程序用于子集和问题

WBOY
WBOY 转载
2023-09-19 09:54:01 593浏览

PHP程序用于子集和问题

子集和问题是计算机科学和动态规划中的一个经典问题。给定一组正整数和一个目标和,任务是确定是否存在给定集合的子集,其元素之和等于目标和。

子集和问题的 PHP 程序

使用递归解决方案

示例

<?php
// A recursive solution for the subset sum problem
// Returns true if there is a subset of the set
// with a sum equal to the given sum
function isSubsetSum($set, $n, $sum)
{
   // Base Cases
   if ($sum == 0)
      return true;
   if ($n == 0 && $sum != 0)
      return false;
   // If the last element is greater than the sum, then ignore it
   if ($set[$n - 1] > $sum)
      return isSubsetSum($set, $n - 1, $sum);
   // Check if the sum can be obtained by either including or excluding the last element
   return isSubsetSum($set, $n - 1, $sum) ||
      isSubsetSum($set, $n - 1, $sum - $set[$n - 1]);
}
// Driver Code
$set = array(1, 7, 4, 9, 2);
$sum = 16;
$n = count($set);
if (isSubsetSum($set, $n, $sum) == true)
   echo "Found a subset with the given sum<br>";
else
   echo "No subset with the given sum<br>";
$sum = 25;
$n = count($set);
if (isSubsetSum($set, $n, $sum) == true)
   echo "Found a subset with the given sum.";
else
   echo "No subset with the given sum.";
?>

输出

Found a subset with the given sum.
No subset with the given sum.

在提供的示例中,集合为 [1, 7, 4, 9, 2],目标和为 16 和 25。目标和为 25 的第二次调用返回 false,表示不存在子集加起来为 25。因此输出为 Found a subset with the给定 sum in first call。第二次调用中没有给定总和的子集。

使用动态规划的伪多项式时间

示例

<?php
// A Dynamic Programming solution for
// subset sum problem
// Returns true if there is a subset of
// set[] with sun equal to given sum
function isSubsetSum( $set, $n, $sum)
{
	// The value of subset[i][j] will
	// be true if there is a subset of
	// set[0..j-1] with sum equal to i
	$subset = array(array());
	// If sum is 0, then answer is true
	for ( $i = 0; $i <= $n; $i++)
		$subset[$i][0] = true;
	// If sum is not 0 and set is empty,
	// then answer is false
	for ( $i = 1; $i <= $sum; $i++)
		$subset[0][$i] = false;
	// Fill the subset table in bottom
	// up manner
	for ($i = 1; $i <= $n; $i++)
	{
		for ($j = 1; $j <= $sum; $j++)
		{
			if($j < $set[$i-1])
				$subset[$i][$j] =
					$subset[$i-1][$j];
			if ($j >= $set[$i-1])
				$subset[$i][$j] =
					$subset[$i-1][$j] ||
					$subset[$i - 1][$j -
							$set[$i-1]];
		}
	}
	/* // uncomment this code to print table
	for (int i = 0; i <= n; i++)
	{
	for (int j = 0; j <= sum; j++)
		printf ("%4d", subset[i][j]);
	printf("n");
	}*/
	return $subset[$n][$sum];
}
// Driver program to test above function
$set = array(8,15,26,35,42,59);
$sum = 50;
$n = count($set);
if (isSubsetSum($set, $n, $sum) == true)
	echo "Found a subset with given sum.";
else
	echo "No subset with given sum.";
?>

输出

Found a subset with given sum.

在提供的示例中,集合为 [8, 15, 26, 35, 42, 59],目标总和为 50。函数调用 isSubsetSum($set, $n, $sum) 返回 true,表示集合中存在一个子集 [8, 42],其加起来等于目标总和 50。因此,代码将找到具有给定总和的子集。

结论

总之,有两种不同的方法来解决子集和问题。第一个解决方案是递归方法,检查给定集合中是否存在总和等于目标总和的子集。它利用回溯来探索所有可能的组合。然而,该解决方案在最坏的情况下可能具有指数时间复杂度。

第二种解决方案利用动态规划并以自下而上的方式解决子集和问题。它构造一个表来存储中间结果,并有效地确定是否存在具有给定总和的子集。这种方法的时间复杂度为 O(n*sum),比递归解决方案更有效。这两种方法都可以用来解决子集和问题,动态规划解决方案对于较大的输入更有效。

以上就是PHP程序用于子集和问题的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除