8

I am tracking locations and their connections to other locations.
I keep locations in an NSArray while each location is represented as a Dictionary. Each location has Dictionary has the attributes (locationName, Connections, latitude, longitude) where Connections is an Array of other locations this location is connected TO (not from). I use lat/lon and a Haversine algorithm to determine the distance between two points.

NEXT, I would like to use dijkstra's shortest path algorithm to find the shortest path between a source and destination location (source and destination are selected by the user)

This is not for commercial use and does not need to support hundreds or thousands of locations.

I am looking for some objective C code that will perform this search.

user1278974
  • 215
  • 3
  • 11
  • 4
    We won't write your code for you, but if you provide us with what you've got, we can give suggestions. – SomeKittens Jun 06 '12 at 18:43
  • I understand. Passing in the parameters source, destination, and LocationArray I want to return the shortest path (in terms of distance) between them. Each connection is 'one-way', A---->B means that you can get to B from A, but it is not implicit that you can get from B to A unless explicitly defined in the B location Dictionary. I thnk this may get more confusing if I share my current broken code. This portion (dijkstra's algorithm) needs a rewrite. – user1278974 Jun 06 '12 at 18:53
  • 1
    Go ahead and share it (bad code doesn't mean you're a bad programmer, it just means you're learning). Wikipedia has a great pseudocode example. http://en.wikipedia.org/wiki/Dijkstra's_algorithm – SomeKittens Jun 06 '12 at 19:11
  • Thanks Kittens. I just think focusing on what I have will be a distraction right now. I have a recursive version that is the wrong approach - I did this before I discovered the existence of dijkstra's. So it is home-grown. It needs to be ditched and rewritten, which is where I am now. – user1278974 Jun 06 '12 at 19:22

3 Answers3

4

A quick google found some objective-c code at snyderp / PESGraph which says

PESGraph is a simple graph implementation for Foundation.kit that allows for greating structures of nodes and paths, and then finding the shortest path between them. It includes unit tests that also provide some examples of how to use the code.

Also this question was previously asked on SO theres-an-easy-way-to-apply-a-shortest-path-alghoritm-in-objective-c and the solution pointed to the same git repository that I found through google.

Community
  • 1
  • 1
Peter M
  • 7,309
  • 3
  • 50
  • 91
  • There is no sample code included with the PESGraph repo. Do you manage to actually use it at the end? – inigo333 Sep 19 '16 at 14:40
2

I had to write my own sample code, because I wasn't able to find any good and working example. You can check it here:

https://github.com/aolszak/AOShortestPath

ArturOlszak
  • 2,653
  • 2
  • 21
  • 20
0

Shameless plug: mj-dijkstra A graph representation is a NSDictionary or an object that behaves like a dictionary.

dmitri
  • 3,183
  • 23
  • 28