I need to implement bfs, dfs and a* algorithm in any language and compare them.
for comparison I need a test case and the Input I need to give is this road map below. I can't understand how should I input this road map in my program.
suggest an approach to program these algorithm using this road map to find the shortest path.
I expect the output to give the shortest path of the road map from ARAD to BUCHAREST.
ROAD MAP
Related
I have a project involving drawing with a robot hand that can only move on the x,y coordinates.
For a given binary image, I've used DFS to explore it. The DFS algorithm returns an array of points that DFS has explored. That array is the path. It works fine.
The problem is that there are jumps in that path(the DFS algorithm returns the path with jumps). For example:
00000000000
00001000000
00001000000
01111000000
00001000000
00001000000
00000000000
The path returned by DFS, is as intended, something like this:
(3,1)->(3,2)->(3,3)->(3,4)->(2,4)->(1,4)->(4,4)->(5,4)
My intention is to not have a jump in this path, so I want to create a function/method that has as input this path(the one with jumps, which is the output of DFS) And outputs another path that is completed with routes from one jump point to another.
For the given example the path should be this:
(3,1)->(3,2)->(3,3)->(3,4)->(2,4)->(1,4)->(2,4)->(3,4)->(4,4)->(5,4)
Is there a good way to do this?
I've tried something but for a complex image it has huge problems(for an output of DFS of only 2000 points, when I try to complete the missing routes, i get at least 10 million points as output path).
The algorithm is intended to work this way:
I iterate through path, when I see a jump point between the current point and the last one, i go back the way I came from until I find it.
Any ideas?
Thank you for your time!
Later edit: Thank you for your time! I figured it out that i'd better use bfs to find the shortest path back, since i actually need it for the robot to take as few steps as possible. It isn't very efficient(it takes about 5 seconds to process a 640x480 binary image) but it works very well(in the end i get about double the number of points that DFS outputs, which I find reasonable).
Your solution is the best if you decide to use a DFS to find the path, but the problem of finding the shortest path to plot points by a robot arm, like you describe, is NP hard, since it's a variant of the traveling salesman problem.
That being said, the fastest algorithms solving this problem can be found by searching for plotter algorithms or reading graph theory books.
These algorithms use similar ideas as the solutions to the traveling salesman, where Euler tours and matchings are used to find the shortest path.
I know A* is the most optimal algorithm for finding the shortest path, but I cannot see how any heuristic can work on a non lattice graph? This made me wonder whether or not A* is in fact capable of being used on an undirected or directed graph. If A* is capable of doing this, what kind of heuristic could one use? If A* is not, what is the current fastest algorithm for calculating the shortest path on a directed or undirected non lattice graph? Please comment if any more information is required.
It might be possible yes, but A* is a popular algorithm for finding shortest paths in grid like graphs. For graphs in general there are a lot of other algorithms for shortest path calculation which will match your case better. It depends on your setting which one to use.
If you plan to do a Single Source Shortest Path (SSSP), where you try to find the shortest path from one node to another one and your graph is unweighted you could use Breath-First-Search. A bidirectional version of this algorithm performs well in practice. If you do SSSP and your graph is weighted Dijkstra's algorithm is a common choice, a bidirectional version could be used as well. For all pairs shortest path other algorithms like Floyd-Warshall or Johnson's algorithm are useful.
If you want to use heuristics and estimate the distance before the search is done you can do pre calculations which are mostly applicable to each of the algorithms mentioned above. Some examples:
shortcut calculation (ARC-Flags, SHARC, KFlags)
hub identification (also transite nodes): pre calculate distances between all hub nodes (has to be done only once in non dynamic graphs), find next hub of source and sink e.g. with dijkstra. add up distance between hubs and source to next hub and sink to next hub
structured look up tables, e.g. distance between hubs and distances between a hub an nodes in a specific distance. After pre calculation you never need to traverse the graph again, instead your shortest path calculation is a number of look-ups. this results in high memory consumption but strong performance. You can tune the memory by using the upper approximations for distance calculations.
I would highly recommend that you identify your exact case and do some research for graph algorithms well suited to this one. A lot of research is done in shortest path for dozens of fields of application.
I want to simulate an algorithm using the MATLAB. This algorithm is designed to find the optimal path among different path in order to be used for routing. The algorithm starts at a specific node (source) then move to a node that is a neighbor to the source node and so on until it reach the destination. I have a n by n matrix (n is the number of nodes) that contains 0 and 1 that is used to determine if the node is neighbor or not to another node (it will have a value of 1 if two nodes are neighbors). so how I can create the different paths from the source to the destination?
That's related with math's theory of graphs. It is familiar to you?
I would say using the BFS's algorithm is going to be the easier way. Between it's applications you find " Finding the shortest path between two nodes u and v (with path length measured by number of edges) ". You can find some theory about it here http://en.wikipedia.org/wiki/Breadth-first_search moreover the algorithm in pseudocode done.
If it isn't quite good for your purpose, you can move to the best available one: Dijkstra's algorithm ( http://en.wikipedia.org/wiki/Dijkstras_algorithm ). But I don't think you need something that sophisticated.
Also I would like to remind you that Matlab isn't the best choice when you have to do loop's stuff, since it somehow is time consuming on it.
I hope this helps.
I am trying to implement dijkstra's shortest path algorithm using map reduce.
I have two questions:
Does this algorithms backtracks to re-evaluate the distances in case the distance turns out to be less for not selected path. For example-> 1->2->5 and 2->3->2 consider these values to be weights and possible 2 paths to a destination path 1 would be selected as 1<2 but overall sum of weights is less for path 2 that is 2->3->2 so want to know if dijkstra's algorithm takes care of backtracking.
Please give me a brief idea of how map and reduce function will be in this case. I am thinking of emitting in map function as and in reduce function and in reduce function I iterate over associated weights to find the least weighted neighbour ..but after that how it function. Please give me a good idea of how it happens from scratch in a cluster and what happens internally.
Dijkstra's does not perform backtracking to re-evaluate the distances.
http://upload.wikimedia.org/wikipedia/commons/5/57/Dijkstra_Animation.gif
that gif should help you understand how Dijkstra's algorithm re-evaluate distances. It avoids the task of backtracking by storing the "shortest path to node n" inside node n.
During traversal if the algorithm comes across node n again, it will simply compare the current "distance" it traversed to get to node n and compare it to the data stored in node n. If it is greater it ignores it and if it is lesser it keeps replaces the data in node n.
Dijkstra's however has a limitation when dealing with negative edges since you could end up with a negative cycle in some circumstances, so that is something you should be wary of.
I am attempting to write (or expand on an existing) graph search algorithm that will let me find the path to get closest to destination node considering there is no guarantee that the nodes will be connected.
To provide a realistic application of this, let's say I need to get from Brampton, Ontario to Hamilton, Ontario. I know my possible options at my start point are Local transit, GO bus or Walking. I know that walking is the least desired way to get to my destination so I look at GO bus first. I know I can take GO to a point close to Hamilton, but at that point the GO bus turns and goes another direction at that closest point is at a place where I have no options (other than walk, but the algorithm would only consider walking for short distances otherwise it will consider the route not feasible)
Using this same example, if the algorithm were to find that I can get there a way that is longer but gets me closer to the destination node (or possible at the destination node) that would be a higher weighted path (The weightings don't matter so much while its searching, only when the results are delivered, it would list by which path was closest to the destination in ascending order). For example, one GO Bus may get me 3km from the destination node, while 3 public transit buses would get me 500m away
So my question is two fold:
1) What algorithm should I start with that does something similar
2) How would I programmaticly explain that it's ok if nodes don't connect so that it doesn't just jump from node A to node R. Would starting from the end and working backward accomplish this
Edit: I forgot to ask how to aim for the best approximate solution because especially with a large graph there will be possibly millions of solutions for this problem.
Thanks,
Michael
Read up on the A* algorithm. It is a generalization of Dijkstra's shortest path algorithm, that allows you to specify a heuristic, which provides a lower bound for distances between two verteces. In your case, the heuristic function would simply return the Euclidean distance.
Run the algorithm and keep track of the vertex with the best characteristic value, which you somehow compute from the graph distance from source and Euclidean distance to target. The only tricky part is to determine when to terminate (unless you want to traverse the entire graph).
Why can't you assume that all nodes are connected? In the real world, they normally are, i.e. you can always walk or call a taxi, etc.?
In this case, you could simply change your model the following way: You have one graph for each transportation method. The nodes that are at the same place are connected with edges of weight 0 (i.e. if you are dropped off by car at an airport or train station).
Then, label each vertex and edge with the transportation type and you can simply use existing routing algorithms. Oh, by the way: A* will not scale well to really big networks. To get something that software like Yahoo/Google/Microsoft Maps actually would use, have a look here. The work of this research group includes the winner of the DIMACS shortest path challenge.
Sounds very much like a travelling salesman problem with additional node characteristics. Just be wary that this type of problem is NP Complete and your best bet would be to go with some sort of approximation algorithm.