Mai intai trebuie sa te autentifici.
Diferente pentru blog/meet-in-the-middle intre reviziile #14 si #13
Nu exista diferente intre titluri.
Diferente intre continut:
The naive algorithm is $O(N^4^)$ 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 n^3 sums a + b + c in a hash set S. Then iterate through all n^3 combinations for d, e, f and check if S contains f - d - e. This algorithm has O(n^3) time and space complexity.
h2. Bidirectional search(interview question) bq. 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(p^k) nodes.
h2. Friend of a friend(inteview question) bq. Given two user names in a social network find an easy way to test if they one is a friend of a friend of the other.
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.
h2. Bidirectional search(interview question) bq. 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(p^k) nodes.
h2. Additional problems 1. (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))