2

I am trying to develop an algorithm wherein I have a Location Class. In each class, I create a list of its adjacent Locations. I want to know, how can I get the shortest path from one Location to another. I am trying to look for different algorithms but it seems that they doesn't answer my problem.

For example, I have a point A and I want to go to point B,

A - - C - - H - - J
      |
      F- - K- -B 

My idea for this is if B is in the List of adjacent locations of A, then that is the shortest path. If not, it should search the adjacent locations of the adjacent locations of A. But I do not know how to implement this in code or if it is a good algorithm. I also want to display A - C - F - K - B as the route for the shortest path. I am also developing this one on j2me so I am a bit limited on the java features that I can use. If anyone can help me, it will be much appreciated. Thanks

keyser
  • 18,829
  • 16
  • 59
  • 101
arthurbc
  • 95
  • 7

3 Answers3

5

You are on the right track. What you describe is the start of BFS. BFS is a shortest-path algorithm that is both optimal [finds the shortest path] and complete [always finds a path if one exist] for unweighted graph - so it is probably the right choice.

BFS works on graphs. In here your graph is G = (V,E) such that V = {all locations} [the nodes/vertices/locations] and E = {(u,v),(v,u) | u and v are neighbors} [the edges/links/neighbors]

The idea of BFS is simpilar to what you are suggesting: first check if the starting node is also the target. Then check if one of the neighbors of the starting node is the target, then search for their neighbors....

Regarding getting the actual path from BFS: have a look at this post.
The idea is to maintain a map - for each node [location] - the map will indicate how did you get there? which node discovered it? After the BFS finished - follow the map from target to source, and you get the actual path [reversed of course]. The link provided gives more details about this idea.

Community
  • 1
  • 1
amit
  • 175,853
  • 27
  • 231
  • 333
  • I can understand what you mean but I am having a hard time translating it into Java code. Can you help me translate it even if for the pseudocode only? I have read some about BFS but the codes are very hard to understand. – arthurbc Mar 27 '12 at 14:34
  • @arthurbc: Did you have a look at the [pseudo-code](http://en.wikipedia.org/wiki/Breadth-first_search#Pseudocode) in the wikipedia page for it? Anything specific you are having troubles with? Did you understand how to model your problem as a [graph](http://en.wikipedia.org/wiki/Graph_%28data_structure%29)? – amit Mar 27 '12 at 14:38
  • Yes I have a look on the pseudo-code in the wikipedia. But maybe I don't know how to model my problem as a graph. Currently, I just have a list of Locations and each Locations has a list of its adjacent Locations. How can I use this so that I can apply the BFS algorithm? – arthurbc Mar 27 '12 at 14:46
4

Your problem is known in the computing world as a graph search problem, looking for the shortest path between two nodes. Graphs here are not the x and y axis graphs from math, but Nodes (or Locations in your example) connected by edges.

Dijkstra's algorithm is the most commonly used to find the shortest path between two nodes, and for your use case it is simplified slightly because the edges in your scenario all have a weight (or cost) of one. An implementation of this is available in JGraphT, though I'm not sure how easy that is to include in a J2ME environment.

sgmorrison
  • 948
  • 7
  • 11
0

Have a look at the A* algorithm Dijkstra's pathfinding algorithm.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • dijkstra is an overkill in here. it is harder to implement and takes longer run-time then a simple BFS - that is enough for unweighted graphs. Regarding A* - maybe I missed something, but the locations doesn't seem to be on a grid or something - which heuristic do you suggest fits here? – amit Mar 26 '12 at 21:14