受这篇文章《SQL,NoSQL以及数据库的实质》结尾处题目的启发,我尝试写了一个SQL版本的Dijkstra最短路径算法。算法描述如下:
前提假设:
Hive支持Stored Procedure
或者
Mysql支持Insert into、insert overwrite、create table as select操作
数据结构:
//各个节点之间的距离
Table meta_distances(
src int,
dst int,
distance int
)
//已知最短距离的点(及距离)
Table known_nodes(
node int,
distance int
)
//未知最短距离的点(及暂时的距离)
Table unknown_nodes(
node int,
distance int
)
求节点1到其他所有节点的距离
初始,各表包含的数据:
meta_distances:
各个节点之间的距离
known_nodes:
(1,0)
unknown_nodes:
(2, MaxInt)
(3, MaxInt)
(4, MaxInt)
…...
(n, MaxInt)
pivot_node=1
Create Procedure Dijkstra(IN pivot_node int)
Begin
declare unknown_nodes_count int default 1</