Monday, 23 March 2015

Tippy Implementation

I know it's very late but I would like to share my implementation of Tippy today. Fortunately, though it doesn't feel so untimely since we covered running time analysis and maybe I can add a few words about why my implementation is good. 

So this is how my algorithm checks for the presence of Tippy and some rationale.

First, figure out where the stone was placed last. I do this by remembering the last move that was made.
Now, we know that if there exists a tippy, it MUST involve this stone.
we just need to check whether there exists a tippy here.
I did this by using recursion. Based on the z shape you can see that essentially you are travelling in zigzag patterns. if you think about it as a person driving from the "center", it's like turning left right left right in sequence. There are two general directions and two possible assymetries (go left first or right first).

My partial code is shown below and I think it might make my explanation clearer so it's somewhat worth skimming through. I just want to add that I used a list in list to store the game_state. In retrospect, think tuple in tuple might have been better since things are immutable...

I also want to briefly add a time analysis for my tippy versus if you did it without remembering last move applied. For this coarse analysis, I will suppose say time is proportional to the number of grid spots that are checked for grid size n

Remember last_move:
Maximum number of checks = 1+9+3+4+3= 20
I'm not sure about this... but I think the following is the worst case... marked in yellow is the last position. Notice in my code that I don't exit even when I find 4. I just go till the end. I did this since time is not a huge issue in this assignment and since most of the time unless the players aren't being crazy... this will not happen.


Don't remember last_move:
I don't know for sure since I don't have any such algorithm at hand. I will say we do a "similar" seeding position analysis. Even with the entire grid on the line, we still know that only the last person to have gone could have made a tippy. This means that we can do a similar analysis on the last few players. Suppose we have size n grid. This means that we have n^2 grid positions and thus at maximum around n^2./2 positions in which the last player played. say we do a check around this point. This means we need to check around n^2/2*(const).
Another method is perhaps to do a subgrid analysis where we find a 3x3 matrix inside. Still this means there's a (n-3)(n-3) grids so (n-3)^2*const. still n^2
I don't know how I can show it, but without remembering anything, the analysis must be scaling n^2.
In some sense, the rationale here is similar to the problem of finding an element in a list. We know that we must see all the elements to know that something does not exist in the list.
But still you can see that if we think about each of these actually run, they do multiple checks on the same point. Probably, there is a way to do better, which reduces that constant time, but for sure it cannot be a constant time.


No comments:

Post a Comment