简体   繁体   中英

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 ...

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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM