def create_ranking2(edge_weight, k, adj, num):
sink = len(adj)
heaps = [[] for i in xrange(sink + 1)]
heaps[0] = [(0, [])]
for current in xrange(sink):
for child in adj[current]:
for length, path in heaps[current]:
new_path = list(path)
new_path.append(current)
# this can be done better using this heapreplace
ew = edge_weight[0, num[(current, child)]]
heapq.heappush(heaps[child], (length + ew, new_path))
heaps[child] = heapq.nlargest(k, heaps[child])
# TODO what with equal lenght paths?
# result: heaps[sink]
return [(length, tuple(zip(nodes, nodes[1:] + [sink]))) for length, nodes in heaps[sink]]
评论列表
文章目录