![]() ![]() Let me put it in a more clear way, for calculating optcost(i,j) we assume that the r is taken as root and calculate min of opt(i,r-1)+opt(r+1,j) for all i<=r<=j. Now the actual part comes, we are adding the frequencies of remaining elements because as we take r as root then all the elements other than that are going 1 level down than that is calculated in the subproblem. The term freq is added because it is going to be root and that means level of 1, so freq*1=freq. This can be divided into 2 parts one is the freq+sum of frequencies of all elements from i to j except r. The reason for adding the sum of frequencies from i to j: We add sum of frequencies from i to j (see first term in the above formula) When we make rth node as root, we recursively calculate optimal cost from i to r-1 and r+1 to j. The idea of above formula is simple, we one by one try all nodes as root (r varies from i to j in second term). We need to calculate optCost(0, n-1) to find the result. The optimal cost for freq can be recursively calculated using the following formula. Recommended: Please try your approach on first, before moving on to the solution. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |