Best search algorithm for solving Towers of Hanoi? [closed] - depth-first-search

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
Which search algorithm is most appropriate for solving the Towers of Hanoi? More specifically, of the uninformed search algorithms breadth-first search, depth-first search, and iterative deepening, which would be best?

I'd argue that none of these algorithms would be the best way to solve Towers of Hanoi. The Towers of Hanoi puzzle is well-studied and we have some great solutions to it, some that work recursively and others that work by looking at patterns in the bits of numbers.
Any of the uninformed search strategies given above will run into trouble fairly quickly solving Towers of Hanoi because the search space is so big. With n disks, there are 3n possible states to the puzzle (each disk can be in one of three spots) and the shortest possible solution to the puzzle has length 2n - 1. You're likely to end up exploring the full search space if you used any of the uninformed strategies, and for any reasonable value of n the cost of holding 3n states in memory (which you'd need for BFS) or a sequence of at 2n - 1 states in memory (for DFS or IDDFS) would be prohibitive. Contrast this with the binary counting solution to Towers of Hanoi or the standard recursive algorithm for Towers of Hanoi, each of which uses only O(n) total memory.

Related

Advantage of using Dinic's O((V^2)E) algorithm over Edmond-Karp algorithm O(V(E^2)) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Is there any advantage of using Dinic's O((V^2)E) algorithm over Edmond-Karp algorithm O(V(E^2))?
In other words, I want to know how is O((V^2)E) better than O(V(E^2)) if it is from a Competitive Programming point of view.
Let's say the total number of vertices in n. "Usually", number of edges in a connected graph tend to be between n and n^2.
Mostly the input graphs are not very sparse, so the number of edges in maximum percentage of the cases would be greater than n (might be O(n log n), or in the worst case, O(n^2)).
So, if you consider the worst case scenario, O(V^2 * E) is O(n^4), whereas O(V*E^2) is O(n^5). Hence you see the advantage of using an O(V^2*E) time algorithm over O(V*E^2).

Google Interview Question: Assign People and Cars optimally on 2D array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
My friend recently got this question from google interview and I think this is a tricky question.
Given People and Cars on 2D array, find an Optimal Assignment of
people and cars where you define what "optimal" means.
If we define Optimal Assignment as the least total Manhattan distance between all pairs (min sum of each pair's distance). What could be a good way to solve this and which algorithm should I use?
Is there any better way to solve this problem more efficiently with different definition of "optimal"?
If you use total manhattan distance (or total pretty much anything else), then this is an instance of the "assignment problem", or "minimum cost bipartite matching".
You can solve it using the Hungarian algorithm or other methods: https://en.wikipedia.org/wiki/Assignment_problem
I believe another condition is that for any person, the distances to any two cars should not be the same. And similarly for any car, the distance to any two people should not be the same. And ultimately you want to give everyone a car.
A greedy solution works like this: compute the distance of every (car, person) distances and sort them. Travel thru the array and pair the (car, person) if neither is paired. The algorithm takes O(mnlog(mn)) for m cars and n people but can be improved with 1. use parallelization when calculating distances 2. keep only a O(n) priority queue for each person. When a person is paired. We no longer add its distances to the queue. Hence the algorithm takes about O(mnlog(n)). The optimality can be proved by induction.

For TSP, how does Held–Karp algorithm reduce the time complexity from Brute-force's O(n!) to O(2^n*n^2)? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have hard time to grasp the key idea of Held-Karp algorithm, how does it reduce the time-complexity?
Is it because it uses Dynamic programming so that time is saved by getting the intermediate result from the cache or because it removes some paths earlier in the calculation?
Also, is it possible to use 2 dimension table to show the calculation for
a simple TSP problem(3 or 4 cities)?
The dynamic programming procedure of the Held–Karp algorithm takes advantage of the following property of the TSP problem: Every subpath of a path of minimum distance is itself of minimum distance.
So essentially, instead of checking all solutions in a naive "top-down", brute force approach (of every possible permutation), we instead use a "bottom-up" approach where all the intermediate information required to solve the problem is developed once and once only. The initial step is the very smallest subpath. Every time we move up to solve a larger subpath, we are able to look up the solutions to all the smaller subpath problems which have already been computed. The time savings come because all of the smaller subproblems have already been solved and these savings compound exponentially (at each greater subpath level). But no "paths are removed" from the calculations–at the end of the procedure all of the subproblems will have been solved. The obvious drawback is that a very large memory size may be required to store all the intermediate results.
In summary, the time savings of the Held–Karp algorithm follow from the fact that it never duplicates solving the solution to any subset (combination) of the cities. But the brute force approach will recompute the solution to any given subset combination many times (albeit not necessarily in consecutive order within a given overall set permutation).
Wikipedia contains a 2D distance matrix example and pseudocode here.

How can you compute the smallest number of queens that can be placed to attack each uncovered square? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This is a variant question from the Elements of Programming Interviews and doesn't come with a solution.
How can you compute the smallest number of queens that can be placed to attack each uncovered square?
The problem is about finding a minimal dominating set in a graph (the queen graph in your case http://mathworld.wolfram.com/QueenGraph.html), this more general problem is NP-Hard. Even if this reduction (on this specific kind of graphs) is unlikely to be NP-Hard, you may expect to not be able to find any efficient (polynomial) algorithm and indeed as up today nobody find one.
As an interview question, I think an acceptable answer would be a backtracking algorithm. You can add small improvements like always stop the search if you already put (n-2)-queens on the board.
For more information and pseudo-code of the algorithm and also more sophisticated algorithms I would suggest to read:
Fernau, H. (2010). minimum dominating set of queens: A trivial programming exercise?. Discrete Applied Mathematics, 158(4), 308-318.
http://www.sciencedirect.com/science/article/pii/S0166218X09003722
The simplest way is probably exhaustive searching with 1,2,3... queens until you find a solution. If you take the symmetries of the board into account you will only need ~10^6 searches to confirm that 4 queens is not enough (at that point you could use the same search until you find a solution for 5 queens or alternately, use a greedy algorithm for 5 queens to find a solution faster).

Are all brute force algorithms exponential? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Every example I have seen of brute force algorithm has exponential run time.
Is this a strict rule, i.e. are all brute force algorithms exponential in run time?
No, certainly not. Consider a linear search algorithm to search a sorted array. You can do better, but a linear search could be considered "brute force".
See https://en.wikipedia.org/wiki/Brute-force_search for further examples and explanation. A relevant quote from that page:
While a brute-force search is simple to implement, and will always find a solution if it exists, its cost is proportional to the number of candidate solutions - which in many practical problems tends to grow very quickly as the size of the problem increases.
Nope. You can also do worse.
Example, finding the shortest tour (travelling salesman) using brute force is Omega(n!), which is not exponential.

Resources