Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP Program for Subset Sum Problem
The Subset Sum Problem is a classic problem in computer science and dynamic programming. Given a set of positive integers and a target sum, the task is to determine whether there exists a subset of the given set whose elements add up to the target sum.
Using Recursive Solution
The recursive approach explores all possible combinations by either including or excluding each element ?
<?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;
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.
In this example, the set is [1, 7, 4, 9, 2]. For sum 16, a subset [7, 9] exists. For sum 25, no valid subset is found as the maximum possible sum is 23.
Using Dynamic Programming
The dynamic programming approach builds a table to store results and avoids redundant calculations ?
<?php
// A Dynamic Programming solution for subset sum problem
// Returns true if there is a subset with sum 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..i-1] with sum equal to j
$subset = 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];
else
$subset[$i][$j] = $subset[$i-1][$j] ||
$subset[$i-1][$j - $set[$i-1]];
}
}
return $subset[$n][$sum];
}
// Driver program
$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.
In this example, the set is [8, 15, 26, 35, 42, 59] and target sum is 50. The subset [8, 42] adds up to 50, so the function returns true.
Comparison
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Recursive | O(2n) | O(n) | Small inputs |
| Dynamic Programming | O(n × sum) | O(n × sum) | Larger inputs |
Conclusion
The recursive solution is simple but has exponential time complexity. The dynamic programming approach is more efficient with O(n×sum) complexity, making it suitable for larger datasets and higher target sums.
