地图中最短的路径 [英] Shortest path in a map
问题描述
我已经在mysql中创建了一个邻接表,如下所示
----------------------------------
|节点|相邻节点|距离|
| ---------------------------------
| 1 | 2 | 5 |
| 2 | 3 | 4 |
| 4 | 1 | 2 |
| 2 | 1 | 5 |
| 3 | 4 | 3 |
| 1 | 4 | 2 |
| 4 | 2 | 1 |
----------------------------------
我需要使用任何首选算法在php中找到两个节点之间的最短路径.我实际上应该如何编码?
我发现很难使用数据库中创建的邻接表来实际实现Dijkstra.同样,此代码将在具有大量节点的情况下进行多次访问,因此它也应有效.
I have created a adjacency list in mysql which is as follows
----------------------------------
|node | adjacentnode | distance |
|---------------------------------
| 1 | 2 | 5 |
| 2 | 3 | 4 |
| 4 | 1 | 2 |
| 2 | 1 | 5 |
| 3 | 4 | 3 |
| 1 | 4 | 2 |
| 4 | 2 | 1 |
----------------------------------
I need to find the shortest path between two nodes in php using any preferable algorithm. How am I actually supposed to code that??
I am finding it difficult to actually implement Dijkstra using an adjacency list made in a database. Also this code will be accessed a number of times having a considerably large amount of nodes, so it should be efficient too.
推荐答案
我去年写过这篇文章.希望对您有所帮助.
I wrote this last year. I hope it helps.
<?php
class KruskalsAlgorithm
{
public function shortest_path(
path_list) {
path_list ) {
points = array();
points = array();
这篇关于地图中最短的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!