I have written in Python the following recursive function for computing a solution within a DP algorithm for a weighted interval scheduling problem where the intervals are "sorted_operations". I am following the "Algorithm Design" book by Kleinberg and Tardos, and OPT and p_list have been already calculated. It seems to work for relatively small instances, but as soon as my size of the problem increases I exceed the "maximum recursion depth" and I get an error. Since increasing the sys.setrecursionlimit crashes my kernel, I am wondering if there are other ways to write this function.
solution_set = []
def compute_solution(j):
if j<=0:
pass
else:
if sorted_operations[j]['weight'] + OPT[p_list[j]] > OPT[j - 1]:
solution_set.append(sorted_operations[j])
print(j)
compute_solution(p_list[j])
else:
compute_solution(j - 1)
compute_solution(len(sorted_operations) - 1)
p_list,sorted_operations(or at least a subset of it) andOPTI can't really offer a recommendation or solution. Also, what do you mean when you say the "size of your problem increases"? What is it increasing from and what to? Iflen(sorted_operations)~ 1,000 then I'd say you have a problem but iflen(sorted_operations)~ 1,000,000 maybe not.