Atenţie! Aceasta este o versiune veche a paginii, scrisă la 2012-08-10 03:54:12.
Revizia anterioară   Revizia următoare  

Meet in the middle

Cosmin
Cosmin Negruseri
10 august 2012

Meet in the middle (sometimes called split and merge) is a clever approach which tries to trade off space for time. Let’s discuss a few applications.

a + b + c = d

Given an array of integers, find out if there are any four numbers such that the sum of the first three equal to the fourth (you can use the same number more than once).

The naive algorithm is O(N4) try all the possible combinations of four numbers. A less naive algorithm brute forces through all n^3 combinations of three numbers and then efficiently checks if their sum is in the original array by using a hash table.
But by now you’ve probably started thinking how meet in the middle can be applied for this problem. The trick is to rewrite the equation as a + b = d - c. Store all the n3 sums a + b + c in a hash set S. Then iterate through all n3 combinations for d, e, f and check if S contains f - d - e. This algorithm has O(n^3) time and space complexity.

A trivial solution is to find first the list of friends for the first user and then the lists of friends for each of his friends. Assuming the network has an average degree of k, this approach does k + 1 requests.
A somewhat faster solution precomputes for each user a list of friend of friends. The number of requests is 1 now but space used is O(nk2) and the size of a reply is O(k2).

A solution that uses the meet in the middle approach has a pretty good balance between memory resources and query execution. We issue two queries, one for the friends of the first user and one for the friends of the second user. Then we intersect the two sets. Now we use just 2 requests, the graph takes O(nk) space, and the reply sizes are O(k).

Breaking 2DES

DES is a encryption standard which uses 56 bit keys. This was enough when the algorithm got invented but currently computers can use a brute force approach to break the encryption. One simple idea to make the encryption more secure is to apply the encryption twice using two different keys. This approach is susceptible to the meet in the middle attack developed by Diffie Hellman. So currently 3DES is used which uses three keys and encrypts the message three times.

Let’s see why 2DES is bad. We call Ek the encryption function using the secret key k and Dk the decryption function using the secret key k. 2DES uses Ek1) = s to encrypt and Dk2) = p to decrypt.

Diffie Hellman’s meet in the middle attack trades of space for time to find out the two secret keys.
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 Ek1 = Dk2 so the secret keys are k1 and k2.
The naive brute force algorithm would go through 256 * 256 iterations by brute forcing through all possible values of k1 and k2 while this algorithm uses 256 * 56 memory to store Ek(p) and does O(256) work to find a match.

Discrete logarithm

Given n a prime number and p, q two integer numbers between 0 and n-1 find k such that p^k = q modulo n.

This problem can be solved using the baby step, giant step algorithm which uses the meet in the middle trick.
We can write k = i ([sqrt(n)] + 1) + j.
Notice that i <= sqrt(n) and j <= sqrt(n).
Now our equality looks like this p (i ([sqrt(n)] + 1) + j) = q modulo n. We can divide by pj and get p(i[sqrt(n)] + 1) = qp-j modulo n.
Now the application of meet in the middle becomes obvious. We can brute force through the numbers on each side of the equality and find a match.
The algorithm takes O(sqrt(n)) space and O(sqrt(n)) time.

Bidirectional search(interview question)

Find the shortest path between two nodes nodes in a large graph which you can’t keep in memory, for example the web graph of the facebook friendship graph, find out the shortest path between them.

One neat idea is instead of starting the search from one node, start from both and see when the two search spaces meet. If the distance between two nodes is k and the average branching factor in the network is p then we explore O(p(k/2)) nodes instead of O(pk) nodes.

Additional problems
1. Friend of a friend(inteview question): Given two user names in a social network find an easy way to test if one is a friend of a friend of the other.
2. Equal partition: Given a set A of 40 real numbers, find out if there is any way to split A in two sets such that the sums of their elements are equal. (O(2^n))
3. Minimal vertex cover: Given a graph of n nodes (n <= 30), find out a set with the smallest number of vertices such that each edge in the graph has at least one node inside the set. (O(3^n/2))
4. Square: You're given an array L which represents the sizes of n planks. You have to answer if there's any way to form a square using the planks without breaking them of overlapping them. (complexity O(4^n/2))
5. 8 puzzle: Solve 8 puzzle. (Each position is solvable in at most 31 moves.)
6. Pancake puzzle: sort pancakes in optimal number of moves.

Try the to solve these problems in the comment section.
http://www.cs.cornell.edu/~wdtseng/icpc/notes/bt3.pdf
http://www.cs.cornell.edu/~wdtseng/icpc/notes/dp1.pdf

Categorii: