algorithm - Finding shortest cycles containing two nodes -
let g=(e,v) directed graph non-negative edge costs. let s vertex. need find algorithm finds each vertex v, shortest cycle contains both s , v. cycle may contain same edge several times.
the obious solution run dijkstra s in order find shortest path s each v. then, each v run dijkstra again in order find shortst path v s. shortest cycle combination of two.
this works take o(|v||e| + |v|^2*log|v|). there better solution?
for directed graph, can use floyd-warshall algorithm find shortest path between 2 pairs.
or more efficient solution run dijsktra on reversed graph (g'=(v,e') such each (v,u) in e, (u,v) in e'), , concat 2 solution (one in reverse of course). this running dijkstra twice.
Comments
Post a Comment