Wednesday, December 5, 2012

Slog Problem Solving

I have decided to attempt a programming problem I found on the cdf programming try-outs website, namely: http://www.ecf.utoronto.ca/~huangs/acm/tryout/prob/b/

Problem:

  You are given a collection of unit cubes whose vertex coordinates are integers. Two cubes with a shared face are glued together. A connected mass of cubes forms a "blob". Your task is to count the number of blobs and find the total surface area of all blobs.

Note: A blob may have internal "bubbles." Its total surface area includes internal wall area of bubbles, as well as area contribution from trapped blobs inside bubbles.

Each unit cube has vertex coordinates (x,y,z), (x+1,y,z), ..., (x+1,y+1,z+1). (x,y,z) is called the minimal vertex of a given cube, and is used to specify the cube.
_________________________________________________________________________________
At first the problem seemed very challenging, but by noting that a "blob" is created only when the vertex of one cube (x, y, z) is one unit away from another cube on strictly on one axis (ex. (x + 1, y, z), (x, y + 1, x), or (x, y, z + 1), the problem became very straight-forward.

Here is the python solution below:

def Blob(vertices):
          surfaceArea = len(vertices)*6
          numberOfObjects = len(vertices)
          for vertex in vertices:
                    isConnected = False
                    for k in range(3):
                              x = vertex[:]
                              x[k] += 1
                              if x in vertices:
                                        surfaceArea = surfaceArea - 2
                                        isConnected = True
                    if isConnected:
                              numberOfObjects -= 1
          return (surfaceArea, numberOfObjects)

I later decided to make the problem more complex, by dealing with the vertices with decimals and attempting to produce a somewhat elegant algorithm (It's not though :( ). Here's my explanation:
Keep in mind, as in the last problem, all vertices will be unique. Basically, a blob, in the decimal case, occurs when the distance between two vertices is greater than 0 and less than √(2). Generally, if we think of the vertex of the original unit cube to be (x1, y1, z1), and the vertex of the unit cube that intersects the original cube as (x1 + x2, y1 + y2, z1 + z2) we can compute their difference of coordinates to be (x2, y2, z2). Because we are dealing with a unit cube (all sides of length 1) the lengths of all sides of the volume produced by the unit cubes can be computed as (1 - x2, 1 - y2, 1 - z2). The surface area to be removed from the collective surface area of a blob(s) will be 2 * (1 - x2) * (1 - y2) + 2 * (1 - x2) * (1 - z2) + 2 * (1 - y2) * (1 - z2).

The python program follows:

def BlobDecimal(vertices):
          surfaceArea = len(vertices)*6
          numberOfObjects = len(vertices)
          i = 0
          for vertex_out in vertices:
                    isConnected = False
                    for vertex_in in vertices:
                              distsqr = 0
                              for k in range(3):
                                        distsqr += (vertex_in[k] - vertex_out[k])**2
                              if (distsqr > 0 and distsqr < 2 and all(vertex_out[l] <= vertex_in[l] for l in range(len(vertex_in)))):
                                        diff_vert = (1 - (vertex_in[0] - vertex_out[0]), 1 - (vertex_in[1] - vertex_out[1]), 
                                                            1- (vertex_in[2] - vertex_out[2]))
                                        diff_surface_area = 2*diff_vert[0]*diff_vert[1] + 2*diff_vert[0]*diff_vert[2] +     
                                                                           2*diff_vert[1]*diff_vert[2]
                                        surfaceArea -= diff_surface_area
                                        isConnected = True
                    if isConnected:
                              numberOfObjects -= 1
          return (surfaceArea, numberOfObjects)

Sunday, November 25, 2012

Assignment 3 Blues

DFSA became extremely difficult extremely quickly this week. Even attempting to do question 1 on Assignment 3 was very challenging, and I am still had trouble coming up with a proper proof statement.
I created this diagram to help me understand how the automata in question one works but, I'm still having issues gaining some insight into how to actually tackles the proof that proves this machine needs a minimum of 16 states.... In this diagram the red arrows indicate accepting 0's, and the green accepting of blue. As recommended by Danny Heap the start state is indeed at the state where a string with no 0's resides as this is the only state that doesn't reach an accepting state in three hops. It would be easier to prove question one with this diagram then without, as it can be clearly seen that if two of the four bit string resided in the same state would be impossible as each state has a unique connection to other states...

Sunday, November 18, 2012

DFSA

We received test #2 on Friday, a bit relieved that the marks were adjusted, but it also shows me that I need to brush up on proofs of recursive algorithms. So I'm hoping to make some time to review and revisit parts of that material before this week ends. I think, as mentioned earlier that the first question really was a bit off-putting due to not remembering a simplification for the harmonic series. ah well. Another thing about the test the I think my section will miss out on is correctness proofs. I will look to brushing up on that as well.

The new material on deterministic finite state automata, for whatever reason seems much easier to understand, especially the proof, in comparison to correctness proofs of algorithm. I think it has to do with the fact that it's visual. Perhaps even correctness proofs for all code should be presented with a visual version as it seems much easier to understand (esp. for those of us who it takes a little bit longer to process what the code is actually doing). Even reading the proof in 7.3 for correctness were a breeze compared to chapter 2 topics.

The topics of languages is also very interesting too, and I wish more CSC courses would focus on history of computer languages, and construction of computer languages, instead of being force-fed languages such as Java, Python, etc.

I hoping to also start my slog problem/investigation soon. Perhaps it will be on automata :).

Monday, November 5, 2012

Abysmal Test Result

Although the test was, by and large, easy. I mean, you could solve these ones on your own, in your own time, with references etc. But doing this in a time limited situation was a bit off-putting.  The first question really relied on your knowing how to derive a summation into a simpler form. Unfortunately most of my study involved, say, the topic that were suggested, correctness, proving increasing decreasing, master's theorem, etc. The fact that probably about 50% of the mark for the first question rested upon your know how to simplify a summation in order to later prove closed form for T(n) seemed a bit unfair.

For the test my studying mostly involved understanding correctness proof which at first seemed complex but is quite easy to understand once you see how Complete Induction is used in the proof and that (precondition + termination) implies postcondition. I wish that had been featured on the midterm instead. I'm pretty sure I did not do very well on this one. Although I think I will be more prepared now for the final and will just have to be more aware of the possible questions to be encountered...

Sunday, October 28, 2012

well-ordering

I found an interesting example of well-ordering in some MIT course notes. The particularly interesting question had to do with a proof of Lehman's equation, 8a^4 + 4b^4 + 2c^4 = d^4, and to prove that Lehman's equation really does not have any positive integer values.

The proof uses Well-Ordering to produce a contradiction, by assuming that there is a least solution and in which 'a' has the smallest possible value, and then showing that 'a' has no smallest value. If we look at the equation we notice that the left hand side of the equation will produce a result that is an even number therefore d must be even as well. By well ordering there is a number in the set of integers less than d such that d = 2d', d^4 = 16 d'^4. So 8a^4 + 4b^4 + 2c^4 = 16d'^4.

This is equivalent to 4a^4 + 2b^4 + c^4 = 8d'^4, now it must be so that c^4 is even as 8d'^4 is even. Then there is a number in the set of integers less than c such that c = 2c', c^4 = 16c'^4. So, 4a^4 + 2b^4 + 16c'^4 = 8d'^4, it follows that 2a^4 + b^4 + 8c'^4 = 4d'^4. So by similar logic b = 2b', b^4 = 16b'^4. So 2a^4 + 16b'^4 + 8c'^4 = 4d'^4, it follows that a^4 + 8b'^4 + 4c'^4 = 2d'^4. Then by similar logic a = 2a', a^4 = 16a'^4. So 16a'^4 + 8b'^4 + 4c'^4 = 2d'^4, it follows that 8a'^4 + 4b'^4 + 2c'^4 = d'^4. Where we have shown that a, b, c and d are all even, and that a smaller 'a' satisfies the solution.

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-042j-mathematics-for-computer-science-spring-2010/lecture-notes/MIT6_042JS10_lec03_sol.pdf

I like this problem as I think it illustrates well-ordering well in that to prove an equation invalid we use the properties of a well-ordered set, in which there is a smallest element, to produce a contradiction, by showing that the equation has no smallest number to satisfy it.

Tuesday, October 9, 2012

First Post

In comparison to CSC165 I'm finding CSC236 a lot more straight-forward and easier to follow (mind you, this is only a month into the material). The tutorial assignments have been helpful and so have the quizzes and there haven't been as many surprise moments that I experienced in CSC165. Although the assignment was challenging it wasn't such a gigantic leap from the course readings and lectures, such that, with a little extra effort the problems were solvable.

I'm hoping with the blog to attempt to develop proofs for problems that I find, separate from the course reading and lectures, but closely related to the topic being covered during each week. I'm finding a lot of 'unsolvable proofs' mentioned in course material from my calculus and statistics classes and I think that with these inductive methods it might be fun to give them a shot. Because we have just been introduced to structural induction my next post will be a proof using structural induction. I will have to do some internet hunting-and-gathering for an adequately-challenging problem.

Tomorrow, is the mid-term test, and I'm a bit rusty with the well-ordering proof format, so that will be my main focus for test practice. Thanks for reading!