Analyse the algorithm (in terms of both time and space) of subset sum problem using dynamic programming technique. Also write down the part of mentioned algorithm that contributes most towards the time and space complexity.
Analyse the algorithm (in terms of both time and space) …
The time complexity of the subset sum problem using dynamic programming technique is O(nW), where n is the number of items and W is the target sum. The space complexity is O(nW).
The most time-consuming part of the algorithm is the bottom-up dynamic programming loop, where each cell in the n x W matrix is filled in. This loop takes O(nW) time in total.
The most space-consuming part of the algorithm is the n x W matrix itself, which requires O(nW) space to store the results of the dynamic programming.
Overall, both the time and space complexity are directly proportional to the size of the n x W matrix, making the dynamic programming solution to the subset sum problem an exponential-time algorithm.