/// <summary> /// If possible, relax the given edge using the supplied base cost and edge-weight function. /// </summary> /// <param name="edge">The edge to relax.</param> /// <param name="cost">The base cost to reach the edge destination vertex.</param> /// <param name="ew">The edge weigher.</param> /// <param name="forbidNegatives">If true, negative values will forbid the link.</param> /// <returns>True if the edge was relaxed, otherwise false.</returns> public bool RelaxEdge(E edge, IWeight cost, IEdgeWeigher <V, E> ew, bool forbidNegatives = true) { V v = edge.Dst; IWeight hopCost = ew.GetWeight(edge); if ((!hopCost.IsViable) || (hopCost.IsNegative && forbidNegatives)) { return(false); } IWeight newCost = cost.Merge(hopCost); int compareResult = -1; if (HasCost(v)) { IWeight oldCost = GetCost(v); compareResult = newCost.CompareTo(oldCost); } if (compareResult <= 0) { UpdateVertex(v, edge, newCost, compareResult < 0); } return(compareResult < 0); }
/// <summary> /// Sums the given edges using the given weigher. /// </summary> /// <param name="weigher">The weigher to use.</param> /// <param name="edges">The edges to sum.</param> /// <returns>The sum of path cost between the given edges.</returns> private IWeight CalculatePathCost(IEdgeWeigher <V, E> weigher, IEnumerable <E> edges) { IWeight totalCost = weigher.InitialWeight; foreach (E edge in edges) { totalCost = totalCost.Merge(weigher.GetWeight(edge)); } return(totalCost); }