您的位置:首页 > 其它

YEN算法和删除算法分别实现K可靠最短路径算法

2016-04-02 20:19 766 查看
在上学期,用马丁的删除算法实现了K可靠最短路径。这学期为了进行进一步算法比较,决定用最原始的YEN算法来计算K最短路径,算法最早提出人是YEN,于是算法因此而命名。在查阅了大量资料后,终于搞明白了算法的思想,终取用java和C#实现它。步骤用DIjkstra算法求出最短路径 k=1;这条path中的任意两节点构成的边都可以作为偏离边,此边的开始节点为偏离节点然后依次将这条path上的偏离边去掉求最短路径。去最短的为次短路径,k=2;依次循环,知道K求出这里要注意两点:在我们寻找偏离边u -> v' 的时候,如果 u == xi.pre(也就是当 要找的偏离边 和 xi 的偏离边是从同一点出发时),则要注意 u -> v' 不仅要和 u -> xi.v不同,而且要和 xi 的所有祖先状态中从点u 出发的那条边都不同,不然新发展的状态岂不是和 xi 的祖先状态重复了。可能有很多偏离路径都是从同一点偏离出来的,但是它们的偏离边都不相同。要在程序中实现这一点,可以在每个状态中记录下所有祖先状态的偏离边。
function YenKSP(Graph, source, sink, K):
// Determine the shortest path from the source to the sink.
A[0] = Dijkstra(Graph, source, sink);
// Initialize the heap to store the potential kth shortest path.
B = [];

for k from 1 to K:
// The spur node ranges from the first node to the next to last node in the previous k-shortest path.
for i from 0 to size(A[k − 1]) − 1:

// Spur node is retrieved from the previous k-shortest path, k − 1.
spurNode = A[k-1].node(i);
// The sequence of nodes from the source to the spur node of the previous k-shortest path.
rootPath = A[k-1].nodes(0, i);

for each path p in A:
if rootPath == p.nodes(0, i):
// Remove the links that are part of the previous shortest paths which share the same root path.
remove p.edge(i, i + 1) from Graph;

for each node rootPathNode in rootPath except spurNode:
remove rootPathNode from Graph;

// Calculate the spur path from the spur node to the sink.
spurPath = Dijkstra(Graph, spurNode, sink);

// Entire path is made up of the root path and spur path.
totalPath = rootPath + spurPath;
// Add the potential k-shortest path to the heap.
B.append(totalPath);

// Add back the edges and nodes that were removed from the graph.
re
8a77
store edges to Graph;
restore nodes in rootPath to Graph;

if B is empty:
// This handles the case of there being no spur paths, or no spur paths left.
// This could happen if the spur paths have already been exhausted (added to A),
// or there are no spur paths at all - such as when both the source and sink vertices
// lie along a "dead end".
break;
// Sort the potential k-shortest paths by cost.
B.sort();
// Add the lowest cost path becomes the k-shortest path.
A[k] = B[0];
B.pop();

return A;
参考资料:http://blog.csdn.net/pi9nc/article/details/12256019
http://blog.csdn.net/kzm2008/article/details/5460152[/code] 

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 dijkstra