Skip to content

Instantly share code, notes, and snippets.

@afressancourt
afressancourt / yen.py
Created April 28, 2015 08:42
Yen's algorithm implementation for Python2 / iGraph, adapted from ALenfant's work (https://gist.github.com/ALenfant/5491853) I had to modify it to make it run on Python 2 with the corresponding iGraph library. I added the possibility to deal with loops in undirected graphs and the possibility to detect that all possible pathes were discovered in…
def path_cost(graph, path, weights=None):
pathcost = 0
if weights is None:
pathcost = len(path)-1
else:
for i in range(len(path)):
if i > 0:
edge = graph.es.find(_source=min(path[i-1], path[i]),
_target=max(path[i-1], path[i]))