Diferente pentru blog/meet-in-the-middle intre reviziile #114 si #123

Diferente intre titluri:

Coding contest byte: Meet in the middle
Coding contest trick: Meet in the middle

Diferente intre continut:

Meet in the middle (sometimes called split and merge) is a clever idea that uses caching to get efficient solutions. Much like divide et impera it divides the problem in two and then tries to merge the results. The benefit is that by using quite a bit of extra memory you can tackle problems of twice the size you could before.
Meet in the middle (sometimes called split and merge) is a clever idea that uses caching to get efficient solutions. Much like divide et impera it splits the problem in two and then tries to merge the results. The benefit is that by using quite a bit of extra memory you can tackle problems of twice the size you could before.
Let's go through a few applications.
Before we go on I want to mention that the additional problems are the best part of the article. Now let's go through a few applications of the trick.
h2. 4sum (popular interview question)
== code(c) |
def 4sum(A):
  S = {}
  sums = {}
  for a in A:
    for b in A:
      S[a + b] = (a, b)
      sums[a + b] = (a, b)
  for c in A:
    for d in A:
      if -(c + d) in sums:
        print (S[-(c + d)][0], S[-(c + d)][1], c, d)
        print (sums[-(c + d)][0], sums[-(c + d)][1], c, d)
        return
  print "No solution."
h2. Bidirectional search
bq. Find the shortest path between two nodes nodes in a large graph which you can’t keep in memory, for example the Facebook friendship graph.
bq. Find the shortest path between two nodes nodes in a large graph, for example the Facebook friendship graph.
!blog/meet-in-the-middle?12fig26.gif!
The breadth first search algorithm is a standard approach for this problem. If the distance between two nodes is $k$ and the average degree in the network is $p$ BFS explores $O(p^k^)$ nodes.
A better solution starts from both nodes and sees when the two search spaces meet.  This reduces the number of states explored to $O(p^k/2^)$.
A better solution starts from both nodes and sees when the two search frontiers meet.  This reduces the number of states explored to $O(p^k/2^)$.
The approach works well with both path finding problems on explicit graphs and with implicit state graphs like the ones you find in games.
h2. Breaking 2DES
!<blog/meet-in-the-middle?2des.png 70%!
DES is an encryption standard which uses 56 bit keys. Today computers can use a brute force approach to break the encryption. One simple approach to make the encryption more secure is to apply it twice, using two different keys. This approach is susceptible to the meet in the middle attack developed by Diffie-Hellman. 3DES is less susceptible as it encrypts the message 3 times using 2 keys.
DES is an encryption standard which uses 56 bit keys. Today computers can use a brute force approach to break the encryption. One simple approach to make the encryption more secure is to apply it twice, using two different keys. This approach is susceptible to the meet in the middle attack developed by Diffie-Hellman. 3DES works around this problem by encrypting the message 3 times using 2 keys.
Let’s see why 2DES is vulnerable. Let $Ek$ be the encryption function using the secret key $k$ and $Dk$ the decryption function using the secret key $k$. 2DES uses two keys, k and K. Ek&#40;EK&#40;p)) = s does the encryption and DK&#40;Dk&#40;s)) = p does the decryption.
For the pattern p it tries all the possible keys to obtain a set of numbers corresponding Ek(p). Also for the pattern s it uses all the possible keys to decrypt s, Dk(s).
If we find any match in the two sets it means that Eki(p) = Dkj(s) so the secret keys are ki and kj.
The naive brute force algorithm does $2^56^ * 2^56^$ iterations going through all possible values of k1 and k2 while this algorithm uses $2^56^ * 56$ memory to store all Eki(p) and does $2^56^$ work to find a match.
This is quite a bit of space and quite a bit of computation time. But a for a large enough company or country it starts being within the realm of posibility.
Breaking 2DES was basically the DOUBLE problem from the International Olympiad in Informatics 2001.
The problem DOUBLE the International Olympiad in Informatics 2001 was basically asking to break 2DES for keys of size 24 which is quite feasible.
h2. Discrete logarithm
The naive solution goes through all possible values of k and takes $O(n)$ time.
The baby-step, giant-step algorithm solves the problem more efficiently using the meet in the middle trick.
Let's write k = $i([sqrt(n)] + 1) + j$
Let's write k = $i[sqrt(n)] + j$
Notice that $i <= sqrt(n)$ and $j <= sqrt(n)$.
Replacing k in the equality we get $p^(i ([sqrt(n)] + 1) + j)^ = q (mod n)$.
Dividing by $p^j^$ we get $p^(i[sqrt(n)] + 1)^ = qp^-j^ (mod n)$.
Replacing k in the equality we get $p^(i[sqrt(n)] + j)^ = q (mod n)$.
Dividing by $p^j^$ we get $p^i[sqrt(n)]^ = qp^-j^ (mod n)$.
At this point we can brute force through the numbers on each side of the equality and find a colision.
The algorithm takes $O(sqrt(n))$ space and $O(sqrt(n))$ time.
# *Square fence* You're given an array L which represents the lengths of n planks. You have to answer if there's any way to form the edges of a square fence using all the planks without breaking or overlapping them.  (Hint: complexity $O(4^n/2^)$)
# *8 puzzle* The 8 puzzle is a sliding tile game of 3x3 slots with 8 tiles and one empty slot. At each step you can move one of the orthogonally neighbouring tiles to the empty slot. The game starts from a random initial configuration and the purpose is to get to the final sorted configuration in the fewest number of moves. Figure out an efficient algorithm that solves the 8 puzzle. (Hint: Each position is solvable in at most 31 moves) In the picture we see a sequence of moves that solves the puzzle.
!blog/meet-in-the-middle?8puzzle.png!
# *4 reversals* We are given two equal length strings S and T. Figure out if we can get string T starting from string S and applying 4 reversal operations. (Hint: complexity O(n^5^))
# *4 reversals* We are given two equal length strings S and T. Figure out if we can get string T starting from string S and applying 4 substring reversal operations. (Hint: complexity O(n^5^))
The additional problems are the best part of the article so try to solve them in the comment section.
Try to solve them in the comment section.

Diferente intre securitate:

public
protected

Topicul de forum nu a fost schimbat.