I am following tardos book to learn dynamic programming. I have a doubt in the solution construction part of weighted interval scheduling problem.
It is suggested that instead of using another array to store the solution, we should use the same cost array in this way :
Find-Solution(j)
if j = 0 then
output nothing
else
If Vj + M[p(j)] >= M[j-1] then
output j together with the result of find-solution(p(j))
else
output find-solution(j-1)
endif
My problem is that to find p(j) it should take O(n) time, and we can make these recursive calls O(n) time making this algorithm O(n^2). But in book it is claimed that this is O(n).
Also we are doing pretty much the same calculation we did to find the cost array m again. is there a way to eliminate this. If I want to use an array to store solution, what should I store in that ?
I think their analysis is assuming that you have precomputed all values of p and in that case the runtime of the method is indeed O(n). Computing the values of p though will take O(n log n).
See how to compute p here: http://www.cs.cornell.edu/courses/cs4820/2010su/handouts/computation-of-p-j.pdf
To see similar analysis about the algorithm: http://www.cs.uiuc.edu/class/fa08/cs473/Lectures/lecture12.pdf. There it also claims that the computation takes O(n). But you will see in the end his analysis of the total running time including computing p is O(n log n).