首页 > 搜索 > yen算法原理,iggraph/networkx中k条最短路径的实现(Yen算法)

yen算法原理,iggraph/networkx中k条最短路径的实现(Yen算法)

互联网 2020-10-30 01:17:16
在线算命,八字测算命理

经过深入的研究,在this、this等基础上,提出了实现k个最短路径算法,以找到第一、第二、第三路径。。。无向循环加权图中的第k条最短路径。大约2000个节点。

Wikipedia上的伪代码是:

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 shortest path. for i from 0 to size(A[i]) − 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) 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 that were removed from the graph. restore edges to Graph; // Sort the potential k-shortest paths by cost. B.sort(); // Add the lowest cost path becomes the k-shortest path. A[k] = B[0];return A;

主要的问题是,我还不能为这个脚本编写正确的python脚本(删除边并将它们正确地放回原位),所以我只能像往常一样在Igraph上重新同步:

def yenksp(graph,source,sink, k):global distance"""Determine the shortest path from the source to the sink."""a = graph.get_shortest_paths(source, sink, weights=distance, mode=ALL, output="vpath")[0]b = [] #Initialize the heap to store the potential kth shortest path#for xk in range(1,k):for xk in range(1,k+1):#for i in range(0,len(a)-1):for i in range(0,len(a)):if i != len(a[:-1])-1:spurnode = a[i]rootpath = a[0:i]#I should remove edges part of the previous shortest paths, but...:for p in a:if rootpath == p:graph.delete_edges(i) spurpath = graph.get_shortest_paths(spurnode, sink, weights=distance, mode=ALL, output="vpath")[0]totalpath = rootpath + spurpathb.append(totalpath)# should restore the edges# graph.add_edges([(0,i)])
免责声明:非本网注明原创的信息,皆为程序自动获取互联网,目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责;如此页面有侵犯到您的权益,请给站长发送邮件,并提供相关证明(版权证明、身份证正反面、侵权链接),站长将在收到邮件12小时内删除。

相关阅读

一周热门

查看更多