r/programmingchallenges • • Sep 09 '11

Make a perfect maze.

A perfect maze is one that has one and only one path between any two points in it (without backtracking).

There are quite a few algorithms out there for this, but trying to make your own is good fun.

29 Upvotes

21 comments sorted by

View all comments

Show parent comment

3

u/HigherFive Sep 10 '11

On line 35 you have

return self.parent.getParent()

If you change that to

return self.parent = self.parent.getParent()

(Python's "=" operator returns the value it assigns, right?) the amortized cost of getParent and union goes down from O(log(n)) to O(log* (n)), which is as close to constant time as you can get.

Also shouldn't the class be named "Section" or "Corridor" rather than "Square"? :P

1

u/PlainSight Sep 11 '11

I'm a noob at python, didn't know about the assignment thing, thanks for telling.