Programming_crazy Programming_crazy - 1 year ago 57
PHP Question

Applying shortest path algorithm on cities

I have 6 cities in my record. mumbai, blore, Hbad, delhi, chennai, pune. And record specif to each city.

When user come on my page, I want to show record based on location relevance. I thought to use shortest distance algo for that.

From user's IP, I can get his location (city or lang-lati). I thought of calculating user's distance with each of above cities. That, which is nearest I should select to show record.

Any better way to do this? I am working with php.

Please talk at implementation level ...

Answer Source

You can represent your cities as nodes in a graph and the distances between them as weight on edges, and than apply Dijkstra's algorithm (http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) If you want to get top 5 shortest distances you can use one of Yen's algorithm or A* ( http://en.wikipedia.org/wiki/A*_search_algorithm )

LE: You have to create a weighted graph (oriented or not, depends of you). The node in the graph can be the name of the city (or if you have more details about the city, you can use an object of type City). The distance between the cities will be represented by the weighted edge between them.

Running Dijkstra, you will have to specify a starting node and an ending node (in your casses the city where your user is in that moment and the city where he wants to go). The algorithm will return the shortest distance, and with a small implementation can print the whole path from source to destination.

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download