2 using System.Collections.Generic;
5 using System.Threading.Tasks;
34 public Func<Arc, double>
Cost {
get;
private set; }
39 private const string NegativeCycleMessage =
"A negative cycle was found.";
40 private readonly Dictionary<Node, double> distance;
41 private readonly Dictionary<Node, Arc> parentArc;
52 distance =
new Dictionary<Node, double>();
53 parentArc =
new Dictionary<Node, Arc>();
55 foreach (var n
in sources)
80 var t = u; u = v; v = t;
81 var dt = du; du = dv; dv = dt;
84 if (!
double.IsPositiveInfinity(du) && c < 0)
86 var cycle =
new Path(
Graph);
104 p =
Graph.Other(parentArc[p], p);
106 var cycle =
new Path(
Graph);
111 Arc a = parentArc[x];
113 x =
Graph.Other(a, x);
127 return parentArc.ContainsKey(node);
132 public IEnumerable<Node>
ReachedNodes {
get {
return parentArc.Keys; } }
139 if (
NegativeCycle != null)
throw new InvalidOperationException(NegativeCycleMessage);
141 return distance.TryGetValue(node, out result) ? result :
double.PositiveInfinity;
149 if (
NegativeCycle != null)
throw new InvalidOperationException(NegativeCycleMessage);
151 return parentArc.TryGetValue(node, out result) ? result :
Arc.
Invalid;
159 if (
NegativeCycle != null)
throw new InvalidOperationException(NegativeCycleMessage);
160 if (!
Reached(node))
return null;
168 result.AddFirst(arc);
169 node =
Graph.Other(arc, node);