backtracking algorithm to solve shortest path? - algorithm

I have been extensively searching all over the net last night until today and I can't seem to find a resources discussing how to solve the shortest path problem by specifically using the backtracking algorithm. I tried solving it with this algo but I doesn't make sense to me. If it's the n-queens problem, it wouldn't be this complicated.
So can anyone give some internet links that would point me to some resources? I appreciate it very much.
*UPDATE: Just curious, can the backtracking algorithm really solve the shortest path problem?

It is wired you specified to use backtracking algoritm, in fact dijkstra SPFA or bellman-ford algorithm will be perfect to solve your problem. If you HAVE TO use backtracking, I am afraid that you could only reach a bad time complexity----just try your next road segment, and when the sum length of your chosen segments exceeded "current shortest path", start backtracking.

Backtracking can solve it.But it is very slow...I think you need Dijkstra O(n^2), Dijkstra with heap O(nlogn), Bellman-ford O(ne) or SPFA O(ke)(k≈2).As for me, I prefer SPFA...

Related

Algorithm to visit all nodes in an un-directed graph with greatest efficiency?

So I have the following layout:
graph representation
The objective is to collect all the yellow blocks by moving the white ball around. I'm trying to come up with an algorithm that will calculate an efficient path however I'm not too sure where to start.
Initially I thought about path finding algorithms like Djikstra and A* but they don't seem to fit with my goal. I've also thought about hamiltonian paths which is closer to what I want but still doesn't seem to solve the problem.
Any suggestions on what sort of algorithm can be used would be appreciated.
Your problem has a classic name in the litterature, it is the minimum hamiltonian walk problem. Beware not to mistake it with the minimum hamiltonian path problem, its 'cousin', because it is much more famous, and much, much harder (finding a hamiltonian walk can be done in polynomial time, finding a hamiltonian path is NP-complete). The traveling salesman problem is the other name of the minimum hamiltonian path problem (path, not walk).
There are very few ressources on this problem, but nevertheless you can have a look at an article called 'An algorithm for finding a short closed spanning walk in a graph' by Takamizawa, Nishizeki and Saito from 1980. They provide a polynomial algorithm to find such a path.
If the paper is a bit hard to read, or the algorithm too complex to implement, then I'll suggest that you go for the christofides algorithm, because it runs in polynomial time, and is somehow efficient (it is a 2-approximation if I remember well).
Another possible approach would be to go for a greedy algorithm, like a nearest unvisited neigbor algorithm (start somewhere, go to the nearest node that is not in the walk yet, repeat until everyone is in the walk).
Acutally, I think the easiest and maybe best simple solution is to go greedy.

How to find Hamiltonian Cycle in a Graph

I am trying to solve the Hamiltonian Cycle problem. I am able to find a path with all the vertices, but unable to complete the cycle.
Can someone provide me with an algorithm to find the cycle?
Determining if a graph has a Hamiltonian Cycle is a NP-complete problem. This means that we can check if a given path is a Hamiltonian cycle in polynomial time, but we don't know any polynomial time algorithms capable of finding it.
The only algorithms that can be used to find a Hamiltonian cycle are exponential time algorithms. Some of them are
Brute force search
Dynamic programming
Other exponential but nevertheless faster algorithms that you can find here
This is one of the most basic problems in computer science, there are plenty solutions depending on what you want: start here http://en.wikipedia.org/wiki/Hamiltonian_path_problem#Algorithms
There are also SO answers related:
here and here
I hope this below link which i found will help you lot with clear explanation...... http://www.geeksforgeeks.org/archives/19092
Use a SAT solver if possible. They don't have the good theoretical time limits of the algorithms in the Wikipedia article, but in practice they can often solve them surprisingly quickly.

Hamiltonian paths & social graph algorithm

I have a random undirected social graph.
I want to find a Hamiltonian path if possible. Or if not possible (or not possible to know if possible in polynomial time) a series of paths. In this "series of paths" (where all N nodes are used exactly once), I want to minimize the number of paths and maximize the average length of the paths. (So no trivial solution of N paths of a single node).
I have generated an adjacency matrix for the nodes and edges already.
Any suggestions? Pointers in the right direction? I realize this will require heuristics because of the NP-complete (?) nature of the problem, and I am OK with a "good enough" answer. Also I would like to do this in Java.
Thanks!
If I'm interpreting your question correctly, what you're asking for is still NP-hard, since the best solution to the "multiple paths" problem would be a Hamiltonian path, and determining whether one exists is known to be NP-hard. Moreover, even if you're guaranteed that a Hamiltonian path doesn't exist, solving this problem could still be NP-hard, since I could give you a graph with a single disconnected node floating in space, for which the best solution is a trivial path containing that node and a Hamiltonian path in the remaining graph. As a result, unless P = NP, there isn't going to be a polynomial-time algorithm for your problem.
Hope this helps, and sorry for the negative result!
Angluin and Valiant gave a near linear-time heuristic that works almost always in a sufficiently dense Erdos-Renyi random graph. It's described by Wilf, on page 121. Probably your random graph is not Erdos-Renyi, but the heuristic might work anyway (when it "fails", it still gives you a (hopefully) long path; greedily take this path and run A-V again).
Use a genetic algorithm (without crossover), where each individual is a permutation of the nodes. This gives you "series of paths" at each generation, evolving to a minimal number of paths (1) and a maximal avg. length (N).
As you have realized there is no exact solution in polynomial time. You can try some random search methods though. My recommendation, start with genetic algorithm and try out tabu search.

Heuristic and A* algorithm

I was reading about dijkstra algorithm and A* star algorithm. I know that the difference is the heuristic used. But what is a heuristic and how this influence the algorithms? Heuristic is just an way to measure the distance? But dijkstra considers the distance too? Sorry, but my question is about heuristic and what it means and why to use them... (I had read about it, but don't understant)
Other question: When should be used each one?
Thank you
In this context, a heuristic is a way of providing the algorithm with some form of extra evaluative information, so that the algorithm can find a 'good enough' solution, without exhaustively searching every possible solution.
Dijkstra's Algorithm does not use a heuristic. It expands outwards from the start node, and examines every node in the graph in order to find the shortest path. While this is accurate, it can be computationally expensive.
By comparison, the A* algorithm uses a distance + cost heuristic, to guide the algorithm in its choice of the next node to explore. This means that the algorithm finds a possible search solution without examining every node on the graph. It is therefore much cheaper to run, but at a loss of complete accuracy. It works because the result is usually close enough to the optimal solution, and is found cheaper than an exhaustive search of the entire graph.
As to when you should use each, it really depends on the application. However, using the A* algorithm requires an admissible heuristic, so this may not be applicable in situations where such information is unavailable to the algorithm.
A heuristic basically means an idea or an intuition! Any strategy that you use to solve a hard problem is a heuristic! In some fields (like combinatorial problems) it refers to the strategy that can help you solve a NP hard problem suboptimally within a polynomial time.
Dijkstra solves the single-source routing problem, i.e. it gives you the cost of going froma single point to any other point in the space.
A* solves a single-source single-target problem. It gives you a minimum distance path from a given point to another given point.
A* is usually faster than Dijkstra provided you give it an admissible heuristic, this is, an estimate of the distance to the goal, that never overestimates said distance. Contrary to a previous answer given here, if the heuristic used is admissible, A* is complete and will give you the optimal answer.

Algorithm: shortest path between all points

Suppose I have 10 points. I know the distance between each point.
I need to find the shortest possible route passing through all points.
I have tried a couple of algorithms (Dijkstra, Floyd Warshall,...) and they all give me the shortest path between start and end, but they don't make a route with all points on it.
Permutations work fine, but they are too resource-expensive.
What algorithms can you advise me to look into for this problem? Or is there a documented way to do this with the above-mentioned algorithms?
Have a look at travelling salesman problem.
You may want to look into some of the heuristic solutions. They may not be able to give you 100% exact results, but often they can come up with good enough solutions (2 to 3 % away from optimal solutions) in a reasonable amount of time.
This is obviously Travelling Salesman problem. Specifically for N=10, you can either try the O(N!) naive algorithm, or using Dynamic Programming, you can reduce this to O(n^2 2^n), by trading space.
Beyond that, since this is an NP-hard problem, you can only hope for an approximation or heuristic, given the usual caveats.
As others have mentioned, this is an instance of the TSP. I think Concord, developed at Georgia Tech is the current state-of-the-art solver. It can handle upwards of 10,000 points within a few seconds. It also has an API that's easy to work with.
I think this is what you're looking for, actually:
Floyd Warshall
In computer science, the Floyd–Warshall algorithm (sometimes known as
the WFI Algorithm[clarification needed], Roy–Floyd algorithm or just
Floyd's algorithm) is a graph analysis algorithm for finding shortest
paths in a weighted graph (with positive or negative edge weights). A
single execution of the algorithm will find the lengths (summed
weights) of the shortest paths between all pairs of vertices though it
does not return details of the paths themselves
In the "Path reconstruction" subsection it explains the data structure you'll need to store the "paths" (actually you just store the next node to go to and then trivially reconstruct whichever path is required as needed).

Resources