0

So, I'm trying to understand the dynamic programming algorithm for finding the minimum weighted triangulation decomposition of a convex polygon. For those of you that don't know, triangulation is where we take a convex polygon, and break it up into triangles. The minimum weighted triangulation is the triangulation of a polygon where the sum of all the edges(or perimeter of every triangle) is the smallest.

It's actually a fairly common algorithm, however I just can't grasp it. Here is the algorithm I'm trying to understand:

http://en.wikipedia.org/wiki/Minimum-weight_triangulation#Variations

Here's another description I'm trying to follow(Scroll down to 5.2 Optimal Triangulations):

http://valis.cs.uiuc.edu/~sariel/teach/notes/algos/lec/05_dprog_II.pdf

So I understand this much so far. I take all my vertices, and make sure they are in clockwise order around the perimeter of the original polygon. I make a function that returns the minimum weight triangulation, which I call MWT(i, j) of a polygon starting at vertex i and going to vertex j. This function will be recursive, so the first call should be MWT(0, n-1), where n is the total number of vertices. MWT should test all the triangles that are made of the points i, j, and k, where k is any vertex between those. Here's my code so far:

def MWT(i, j):
    if j <= i: return 0
    elif j == i+1: return 0

    cheap_cost = float("infinity")
    for k in range(i, j):
        cheap_cost = min(cheap_cost, cost((vertices[i], vertices[j], vertices[k])) + MWT(i, k) + MWT(k, j))
    return cheap_cost

However when I run it it overflows the stack. I'm just completely lost and would appreciate if somebody could help direct me in the right direction.

If you guys need any more info just ask.

robins35
  • 653
  • 12
  • 37

1 Answers1

1

I think that you want to do

for k in range(i+1, j):

not

for k in range(i, j):

because you never want k to be the same as i or j (otherwise you'll just calculate it for the same values that you're currently running).

Xymostech
  • 9,710
  • 3
  • 34
  • 44
  • You're right, however if you see, at the top it says: if j <= i: return 0, so while it does waste a bit of resources, it shouldn't make a difference in the outcome right? – robins35 Mar 03 '13 at 03:34
  • Hmm, well it didn't crash when I did it this way, maybe you're right :) – robins35 Mar 03 '13 at 03:35
  • 1
    No, the problem is when `k=i`, and then it runs `MWT(k, j)`, which is the same thing as `MWT(i, j)`, thus repeating the same calculation. If you look at the wikipedia page, it says that `i – Xymostech Mar 03 '13 at 03:36
  • Wow, I got the right answer completely, I'm still confused why this magically made it work, mind explaining? Thanks again – robins35 Mar 03 '13 at 03:36
  • 1
    @Scriptonaut did my last comment answer your question? – Xymostech Mar 03 '13 at 03:42
  • It did, I'm not sure why I didn't see that. Right now I'm just looking at my algorithm trying to make sense of what it's actually doing. I understand why it was being thrown into that infinite loop, but now looking at it it doesn't make sense to me, why am I adding cost to the two recursive MWT calls? – robins35 Mar 03 '13 at 03:49
  • So where in here are you storing the intermediate values ? I mean dynamic programming is all about divide and conquer and at the same time storing results which you think might be repeated ... – user590849 Mar 24 '13 at 17:13
  • @user590849 In this case, the algorithm doesn't memoize results. It could easily be made to. I'm not really sure how that relates to this answer... – Xymostech Mar 24 '13 at 17:22
  • Well the question does mention dynamic programming so I was wondering how does this answer follow the dynamic programming paradigm ? – user590849 Mar 25 '13 at 04:16
  • @user590849 Their question was "why does this code not work?", and this answer fixed the problem. – Xymostech Mar 25 '13 at 04:31