# yen算法原理,iggraph/networkx中k条最短路径的实现（Yen算法） Wikipedia上的伪代码是：

function YenKSP(Graph, source, sink, K)://Determine the shortest path from the source to the sink. A = 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;return A;

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")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")totalpath = rootpath + spurpathb.append(totalpath)# should restore the edges# graph.add_edges([(0,i)])