Showing posts with label English. Show all posts
Showing posts with label English. Show all posts

Friday, November 25, 2011

The blue and yellow particles problem

Some time ago, I read the following problem:
In a box, there is a certain amount of blue and yellow particles. The particles can react with each other in three different ways:
  • Two yellow particles can fuse into a blue particle
  • Two blue particles can fuse into a blue particle
  • An yellow and a blue particle can fuse into an yellow particle
Given the initial amount of yellow and blue particles, which color will be the last particle?
I was kinda confused with this problem. My first thought was "How the hell am I supposed to know? Can't the three types of reactions happen in different orders which may eventually lead to last particles with different colors?" The problem wording seemed to tell you that the color of the last particle only depended on the number of initial particles of each color. This appeared to be true, because every time I drew a box with a certain amount of blue and yellow particles, I couldn't find two different sequences of reactions which would produce last particles with different colors.
To solve this problem let's start by writing some math. Hopefully it will lead us to the solution.

The first thing I did was to define three functions to represent the possible reactions. Each function receives and returns a pair of values, nBlues and nYellows which represent the number of particles of each color in the box. The functions argument correspond to the state of the box before the reaction and the function return value represent the state after the reaction. I defined these functions in Mathematica (a computer program) like this:
bb[{nBlues_, nYellows_}] := {nBlues, nYellows} + {-1, 0}
yb[{nBlues_, nYellows_}] := {nBlues, nYellows} + {-1, 0}
yy[{nBlues_, nYellows_}] := {nBlues, nYellows} + {1, -2}
The name of the functions correspond to the reaction they represent.
  • The blue-blue function will return a "box" from which one blue particle was taken because two blue particles fuse into one in the corresponding reaction.
  • The yellow-blue function will return a "box" from which one blue particle was taken because one blue particle and one yellow particle fuse into one yellow particle in the corresponding reaction .
  • The yellow-yellow function will return a "box" from which two yellow particles were taken and one blue particle was added because two yellow particles fuse into one blue particle in the corresponding reaction.
Ok, suppose you want to know the resulting state of having a box with 5 blue particles and 6 yellow particles after a serie of reactions, for example (in left to right order): bb,yb,bb,yy. All you have to do is to write:
yy[bb[yb[bb[{5, 6}]]]] 
Notice that the function at the right is calculated before the function at left. Mathematica will output:
{3, 4}
Mathematica is an algebra system software, so it can also do math with symbols which can represent, for example, variables:
yy[bb[yb[bb[{a, b}]]]]
Output:
{-2 + a, -2 + b}
Now, let's define some notation. Let \(f\) be a function, and \(n\) be a number. We will say that: $$\underbrace{f(f(...f(x)...))}_{f \textrm{ composed } n \textrm{ times}} = \underbrace{f \circ f \circ ...\circ f(x)}_{f \textrm{ composed } n \textrm{ times}} = f^{n}(x) $$ Since all the functions we defined are commutative between each other (this is really easy to prove so let's skip this job and give it to Mathematica):
bb[yb[{a, b}]]
{-2 + a, b}
yb[bb[{a, b}]]
{-2 + a, b}
bb[yy[{a, b}]]
{a, -2 + b}
yy[bb[{a, b}]]
{a, -2 + b}
yy[yb[{a, b}]]
{a, -2 + b}
yb[yy[{a, b}]]
{a, -2 + b}
,we can write any sequence of reactions that happens in the box as:
$$bb^a \circ yb^b \circ yy^c$$ Where \(a\),\(b\) and \(c\) correspond to the number of times each kind of reaction happened. So, for example, the sequence of reactions yb,yy,bb,yy,bb,bb,yb could be represented as \(bb^3 \circ yb^2 \circ yy^2\).
Notice we can only say this because all the functions we have are commutative. Suppose there is another reaction \(dd\) where, for example, the number of yellow particles in the box is doubled. The corresponding function would not be commutative with the rest of the functions we already have, i.e \((dd \circ yy(4,5) = (5,6)) \ne (yy \circ dd(4,5) = (5,8)) \) so we couldn't change the order in a composition of functions and expect the same result.

The following equation now emerges:
$$(b_{\textrm{final}},y_{\textrm{final}}) = bb^a \circ yb^b \circ yy^c(b_{\textrm{initial}},y_{\textrm{initial}})$$ It is also not hard to come up with the conclusion that:
\begin{array}{lcl} bb^a(b_{\textrm{initial}},y_{\textrm{initial}}) = (b_{\textrm{initial}}-a, y_{\textrm{initial}})\\ yb^b(b_{\textrm{initial}},y_{\textrm{initial}}) = (b_{\textrm{initial}}-b, y_{\textrm{initial}})\\ yy^c(b_{\textrm{initial}},y_{\textrm{initial}}) = (b_{\textrm{initial}}+c, y_{\textrm{initial}} - 2c)\\ \end{array} I'll prove the third equality. The others are analogous.
\begin{array}{lcl} yy^c(b_{\textrm{initial}},y_{\textrm{initial}})=((...((b_{\textrm{initial}},y_{\textrm{initial}})+\underbrace{(1,-2))+(1,-2))+...)+(1,-2)}_{(1,-2) \textrm{ is summed } c \textrm{ times}})\Leftrightarrow \\ yy^c(b_{\textrm{initial}},y_{\textrm{initial}})= (b_{\textrm{initial}}+c,y_{\textrm{initial}}-2c) \end{array} Now we can have a more workable equation:
$$(b_{\textrm{final}},y_{\textrm{final}}) = bb^a \circ yb^b \circ yy^c(b_{\textrm{initial}},y_{\textrm{initial}}) \Leftrightarrow (b_{\textrm{final}},y_{\textrm{final}}) = (b_{\textrm{initial}}-a-b+c,y_{\textrm{initial}}-2c)$$ In our problem, we want to find out what color the last particle will be. If it is the last one, then we know that $$b_{\textrm{final}} + y_{\textrm{final}} = 1$$ Using all our equations, we can write the following system where all variables are natural numbers:
\begin{cases} b_{\textrm{final}} = b_{\textrm{initial}}-a-b+c \\ y_{\textrm{final}} = y_{\textrm{initial}}-2c \\ b_{\textrm{final}} + y_{\textrm{final}} = 1 \end{cases} We have two possibilities for the \((b_{\textrm{final}},y_{\textrm{final}})\): \((0,1)\) and \((1,0)\).
If \((b_{\textrm{final}},y_{\textrm{final}})=(1,0)\) then:
$$y_{\textrm{initial}} = 2c \wedge b_{\textrm{initial}} = 1 + a + b - c$$ \(2c\) is even, so we can say that
$$(b_{\textrm{final}},y_{\textrm{final}})=(1,0)\Rightarrow y_{\textrm{initial}} \textrm{ is even} $$ If \((b_{\textrm{final}},y_{\textrm{final}})=(0,1)\) then:
$$y_{\textrm{initial}} = 2c + 1 \wedge b_{\textrm{initial}} = a + b - c$$ \(2c + 1\) is odd, so we can say that
$$(b_{\textrm{final}},y_{\textrm{final}})=(0,1)\Rightarrow y_{\textrm{initial}} \textrm{ is odd} $$ The meaning of an implication is that if \(a\) implies \(b\), every time \(a\) is true, \(b\) is also true. And there is only one final result which leads to an even initial number of yellow particles, and one final result which leads to an odd initial number of yellow particles. So
\begin{array}{lcl} y_{\textrm{initial}} \textrm{ is even} \Rightarrow (b_{\textrm{final}},y_{\textrm{final}})=(1,0) \\ y_{\textrm{initial}} \textrm{ is odd} \Rightarrow (b_{\textrm{final}},y_{\textrm{final}})=(0,1) \end{array} are also true statements. Now we can finally give a justified answer to the problem:
Given the initial amount of yellow and blue particles, which color will be the last particle?
If the amount of yellow particles is even, the last particle will be blue. If it is odd, the last particle will be yellow.

In this case, the last particle will be yellow. I guess that's why the yellow particle is smiling :D




Monday, November 14, 2011

Why are graphs useful

Wassup?
I've got 3 problems for you to solve. Ok, you don't need to solve them, but just try to read and understand them.

1. Drawing a figure
Can you draw this figure without lifting your pencil and writing any segment twice?
2. Seven Bridges of Königsberg


Find a walk through the city that would cross each and every bridge once and only once. The islands cannot be reached by any route other than the bridges, and every bridge must be crossed completely every time: one can't walk halfway onto the bridge and then turn around and later cross the other half from the other side.

3. Safe house


Mr. Vinci wants to close all the doors in his house. He wants to find a route such as he doesn't need to cross any door twice. He doesn't care whether he starts and finishes inside or outside the house. Can you help him?
Guess what... The problems are all the same with just different topologies. I know, it's kinda obvious now that you read them all in a row. But had I shown you problem 3 last week and now problem 2, you probably wouldn't realize they are the same kind of problem, unless you already knew this kind of stuff.

Just like there is a large amount of problems which can be translated into a simple equation, there is a large amount of problems which can be represented by a graph. What's the point? Well, many times, the context in which a problem is presented to you provides unnecessary details for it's resolution. If we can represent those problems in a consistent way that allows us to abstract ourselves from those details, we will be more successful at solving them. And a graph is a great representation for all these problems and many, many more (believe me, even some weirdest looking problems may end up being easily solved when represented through a graph) .
I bet that you can solve any of this problems easily, but just let me increase their dimensions and you will have a hard time unless you create a decent algorithm to do it. (even thought I won't focus on that  matter in this post, keep reading for some clues)

So what is a graph? Son, don't hope for me to explain you, but let me tell you, they aren't the plotting functions graphs in case you are thinking that. Just eat some wikipedia cerials!

In mathematics, a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected objects are represented by mathematical abstractions called vertices, and the links that connect some pairs of vertices are called edges. Typically, a graph is depicted in diagrammatic form as a set of dots for the vertices, joined by lines or curves for the edges.
So how would be the graphs for the problems presented above?
Like this:

Note: It is often good idea to label vertices or/and edges of the graph depending on the problem. In these problems, the focus is on the edges, so I labeled them. The numbers on the first image don't represent a weight of the edges in anyway: they are just labels. You can click on the image to see it in a bigger size.




There are graphs of various types and with different characteristics. For example, there can be weighted graphs when we have associated with each edge a cost (this cost can be money, gas, distance, time, etc..). We can have directed graphs where each edge can only be crossed in one direction... There is much to explore. Eat as many wiki cerials as you want on this.
In our case, our graphs are undirected (or directed if you double the number of edges by replacing each current edge \(x\) for \(x_\nearrow\) and \(x_\swarrow\)) and unweighted, since we don't care about any kind of cost associated with the edges.

My point with this post was to show you that graphs have the potential to reveal a "common denominator" for various problems and allow a -more focused on important things- approach.
But now it would be kinda anti climatic if I didn't talk about the solution for the problems so here we go...

All these problems challenge you to find the same thing, an Eulerian trail:
An Eulerian trail or Euler walk in an undirected graph is a path that uses each edge exactly once. If such a path exists, the graph is called traversable or semi-eulerian.
Euler stated and it was later proven that an undirected graph has an Eulerian trail if and only if it has exactly 2 or 0 vertices with an odd number of edges. Why, might you ask? Well, you can convince yourself that this is true with empirical reasoning (although there are formal mathematical proofs for it).For example:
  • You can think that every time you enter on a vertex, you will also need to leave it. If you enter it twice during your trail, you will also need to leave it twice. Also if you quited a vertex, you had to have entered on it before.The exception for this statements are the first and last vertex - you can leave the first vertex without ever entering on it, and you can enter on the last vertex without ever leaving it. So for every vertex in the middle of the trail, you need  a pair number of edges.
  • You can imagine a squared shaped graph where your path will start and finish on the same vertex. If you add an edge to the graph on that vertex to make your possible path longer, you also have to add an edge somewhere else, otherwise the vertex would have an edge connecting to empty space.
  • This operation took you from a graph with 0 odd degree vertices to a graph with 2 odd degree vertices. You just can't find an Eulerian trail with a different number of odd degree vertices, accept that xD

So the solutions to our problems are: 1-Really? 2-Impossible 3-I mean, really :| ?

There are many algorithms for finding Eulerian paths. Hell, you can even find them with elementary linear algebra, or a backtracking algorithm (not the best methods). If you use the first, you will need a veeeery big system to find the solution to all the variables. So, yeah, not a task for a human when the number of edges starts to get bigger then 5. Computers can do it ;). Check this out for much better algorithms to do it - which, by the way, I'm not familiar with:
http://en.wikipedia.org/wiki/Eulerian_path


Hasta la vista baby.

Friday, November 11, 2011

Solving minesweeper using algebra

Language: English/Inglês What is in this post: Minesweeper linear systemMathematica solving the system
Have you ever wondered how to solve minesweeper in a mathematical way?
I have. It is pretty easy and straightforward. I'll show you a possible algorithm for this.
Consider the following  minesweeper puzzle:


I suppose you know the rules of minesweeper:  when a cell has a number 'x', it means that in the 8 cells around that cell, there is a total of 'x' mines. For example, around the '3' cell, there are 3 mines. The objective of the game is to pinpoint the location of all the mines. But for this, you need to make sure which squares don't have mines so you can click on them to get a number which will give you a new clue.

Ok, let's give names to the cells: I'll call \(x_1y_1\) to the bottom left cell, \(x_1y_2\) to the one above it, and \(x_2y_1\) to the one at it's right, etc...

We need to express mathematically the information that is given to us. Our variables are gonna be the unknown cells around the number cells that are visible.

Unknown cells around the number cells

The name of the each variable will be the name of the cell it represents. Each of the cell variables will have the value 1, if it has a mine, or 0 if it doesn't. It can't have the value 0.5 because a cell can't have half a mine, neither the value 2, because a cell has, at most, one mine.


Now it is easy to write what we see!

\begin{array}{lcl} \forall i,j : x_iy_j = 0 \lor x_iy_j = 1 \\ x_1y_1 + x_1y_2 = 1\\ x_1y_1 + x_1y_2 + x_1y_3 + x_2y_3 + x_3y_3 = 3\\ x_2y_3 + x_3y_3 + x_4y_3 = 1\\ x_3y_3 + x_4y_3 + x_5y_3 + x_5y_2 + x_5y_1 = 2\\ x_5y_2 + x_5y_1 = 2\\ \end{array}
Now I'll tell Wolfram Mathematica (a computer program) to solve this for me:

Simplify[Reduce[
  {x1y1 + x1y2 == 1 && 
    x1y1 + x1y2 + x1y3 + x2y3 + x3y3 == 3 &&
    x2y3 + x3y3 + x4y3 == 1 &&
    x3y3 + x4y3 + x5y3 + x5y2 + x5y1 == 2 &&
    x5y2 + x5y1 == 2 &&
    (x1y1 == 1 || x1y1 == 0) &&
    (x1y2 == 1 || x1y2 == 0) &&
    (x1y3 == 1 || x1y3 == 0) &&
    (x2y3 == 1 || x2y3 == 0) &&
    (x3y3 == 1 || x3y3 == 0) &&
    (x4y3 == 1 || x4y3 == 0) &&
    (x5y3 == 1 || x5y3 == 0) &&
    (x5y2 == 1 || x5y2 == 0) &&
    (x5y1 == 1 || x5y1 == 0) 
     }
  , {x1y1, x1y2, x1y3, x2y3, x3y3, x4y3, x5y3, x5y2, x5y1}
  ]]
The output is:
x1y3 == 1 && x2y3 == 1 && x3y3 == 0 && 
x4y3 == 0 && x5y1 == 1 && x5y2 == 1 && 
x5y3 == 0 && 
((x1y1 == 0 && x1y2 == 1) || (x1y1 == 1 && x1y2 == 0))
This means that \(x_1y_3, x_2y_3, x_5y_1\) and \(x_5y_2\) cells have a mine while \(x_3y_3, x_4y_3\) and \(x_5y_3\) cells don't.
Whether or not the \(x_1y_1\) and \(x_1y_2\) cells have a mine is undetermined, but we know that if \(x_1y_1\) has a mine, \(x_1y_2\) doesn't and vice-versa.

What we do at this moment is to fill the board with the results we are 100% sure about. If we do so we get the following board:


As you can see, when we looked at the beginning board we couldn't immediately say where were the mines. It was not obvious. But using algebra gave us some help.
Now you just have to repeat this process until the board is solved.
Note that not every minesweeper puzzles have a determinable solution: which means sometimes every variable will be undetermined when you solve the system.
Also, in Microsoft minesweeper, there is a counter for how many mines are left not flagged at each moment. This could be helpful in near end game situations where you can't find out any cell value to be determined. However, i didn't include this data in the resolution I purposed for matters of simplicity. If you are curious about how to do it, post a comment.