Showing posts with label Puzzles. Show all posts
Showing posts with label Puzzles. Show all posts

Thursday, May 10, 2007

O(n)时间内查询n*n网格的局部最小值

Source: Homework of Theoretical Computer Science


Description:

Suppose now that you're given an n*n grid graph G. Each node v is labeled by a real number x_v; you may assume that all these labels are distinct. A node v is a local minimum if the label x_v is less than the labels x_w for all nodes w that are joined to v.
You can determine the value x_v by probing the node v. Show how to find a local minimum of G using only O(n) probes to the nodes of G.


Solution:

First let's think about this, we start at an arbitrary grid, if one of its adjacent grid has a smaller label, then we move to this grid, and repeat the same process. Finally we will reach a local minimum grid. We call the path we are walking through a "decreasing path". This is a simple way to find a local minimum, but the probes we need is O(n^2) instead of O(n). However, we will see this idea useful later.

Now let's consider a subgraph of the G, suppose it is an rectangular grid graph, and v is the smallest grid of the grids on the border of this rectangle. Suppose v' is the grid adjacent to v and inside the rectangle, if v' is smaller than v, then we can conclude that there exist a local minimum inside the triangle. This is because if we can alwasy a "decreasing path" starting from v', ending at at local minimum. This decreasing path will not go across the border of the rectangle, since v' is smaller than all the grids on the border. So, base on this conclusion, we can divide and conquer the problem.

We may assume that the outer of the G has a very large label. At the first step we probe all the grids of the column at the very middle of G. The column divides the graph G into two n*(n/2) rectangular subgraphs. Suppose the smallest grid of the column is v_1, then we probe the two neighbors of v_1 which are not in the same column of v_1. If the two grids are all larger than v_1, then v_1 is a local minimum. Otherwise, at least one of the two neigbhbors is smaller than v_1, so we can determine which rectangular subgraph definitely contains a local minimum, ccording the conclusion we just worked out. And at the second step, we probe all the grids of the row at the very middle of the rectangular subgraph, this row divides the subgraph into two (n/2)*(n/2) square subgraphs. Suppose the smallest grid of the row is v_2, if v_2>v_1, then the decreasing path starting at v_1 will not go across the row, so we choose the square subgraph which contains v_1. (At this situation v_1 will not locate at the common corner of the two squares, since v_1's neighbor is smaller than it and v_2 is larger than it.) If v_2<v_1, then we probe the two neighbor's of v_2 which are not at the same row of v_2, and use the same way as we due with v_1, to select one square subgraph, or assert v_2 itself is a local minimum. (At this situation v_2 will not locate at the common corner of the two squares, since v_1 is the smallest grid of the column and v_2<v_1.) Now we have shrunk the graph in to a (n/2)*(n/2) subgraph, with 3n/2+4 probes at most. We can apply the same process to the subgraph and finally we will get a local minimum. So the total number of probes will be T(n)=T(n/2)+3n/2+4=3n+4log(n)=O(n).

...
Read More

Saturday, April 28, 2007

好人与坏人

Source: An exercise class of Theoretical Computer Science

Description:
Suppose there are some guys, some of which are good and the others are bad. Good guys always tell the truth, whereas bad guys can tell truth or lie, depends on their wish. We know the good guys are more than the bad guys. Each time we can ask a guy that whether another guy is a good guy.
The question is how to determine the good guys in minimum queries.


Solution: (in Chinese for clarity)
把这n个人两两配对(n为奇数时剩下一个),让每一对人分别说对方是不是好人。那么把都说对方是好人的对留下,别的对去掉。则留下的对中要么都是好人,要么都是坏人;去掉的每一对里一定是一个好人和一个坏人,于是去掉的好人和坏人一样多,剩下的好人还是比坏人多。然后看n为奇数时落单的一个人怎么处理:
1. 如果留下的对数为奇数,则好人对必比坏人对多。把落单的一个人去掉,再从这奇数对中每对取出一个人组成新的集合。这个集合里的好人比坏人多,于是形成了一个子问题。
2. 如果留下的对数为偶数,则如果好人对和坏人队一样多,那么落单的一定是好人。如果好人对比坏人对多,那么无法确定落单的是好人还是坏人。从这偶数对中每对取出一人,再加上落单的那个人,组成新的集合,则集合里好人比坏人多(想想为什么)。于是也形成了一个子问题。
应用同样的算法,最后必剩下一个好人。再用这个好人确定其他所有人。总共询问次数最坏不超过2n。

...
Read More

猜帽子问题

Source: An exercise class of Theoretical Computer Science

Description:
There are n people in a room, everyone wears a hat, the color of which could be red, green, or blue, randomly. Each guy can see all the hats except his own. Now there is a spectator coming into the room and asking each of the n guys in private, "which color of hats do you think is on your head?"
Once the n guys are in the hats, their only information is the colors of other guys' hats, and they can do nothing but answer the spectator's question, and of course their answer will not be heard by the other guys. However, before the experiment starts, the n guys can figure out a strategy. What they care is the probability that all of the n guys answer the spectator correctly.

Now your question comes, what strategy should those guys take to make the probability maximal?



Solution:
We label each color with a number of 0,1,or 2, say, red is 0, green is 1, blue is 2. Each guy summarizes the numbers corresponding to the colors he can see, Let S be the sum. This guy will choose number x to be his answer, where x must satisfy that S+x=0 (mod 3).

Obviously, if all of the n guys are using this strategy, then the probability that they all give correct answer is 1/3, because if one of those guys is right, then all of the n guys are right.


Remark:
Actually there is another version of this problem. This version says the n guys stands in a queue and each guy can only see his preceding guys. The spectator ask the guys from the back of the queue to the front, and each guys answer can be heard by his successor. The question of the problem is the same with the original one.
We can use an similar (almost the same) strategy to solve this version of problem. If you are interested in it, think about it.

...
Read More
 
/* google analytics */