< Summary

Information
Class: MoreStructures.Graphs.ShortestDistance.PotentialBasedShortestDistanceFinder
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/Graphs/ShortestDistance/PotentialBasedShortestDistanceFinder.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 64
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Finder()100%1100%
.ctor(...)100%1100%
Find(...)100%1100%
Find(...)100%4100%
get_OriginalDistances()100%1100%
get_Potentials()100%1100%
.ctor(...)100%1100%
get_Item(...)100%1100%

File(s)

/home/runner/work/MoreStructures/MoreStructures/MoreStructures/Graphs/ShortestDistance/PotentialBasedShortestDistanceFinder.cs

#LineLine coverage
 1namespace MoreStructures.Graphs.ShortestDistance;
 2
 3/// <summary>
 4/// An <see cref="IPotentialBasedShortestDistanceFinder"/> implementation, wrapping a
 5/// <see cref="IShortestDistanceFinder"/> algorithm and using a potential function as a heuristic to enhance graph
 6/// exploration.
 7/// </summary>
 8/// <remarks>
 9/// Common ground for all A* variants, such as <see cref="AStarShortestDistanceFinder"/> and
 10/// <see cref="BidirectionalAStarShortestDistanceFinder"/>.
 11/// <br/>
 12/// Check <see cref="IPotentialBasedShortestDistanceFinder"/> general documentation for the requirements and desired
 13/// properties for the heuristic.
 14/// </remarks>
 15public abstract class PotentialBasedShortestDistanceFinder : IPotentialBasedShortestDistanceFinder
 16{
 17    /// <summary>
 18    /// A <see cref="IShortestDistanceFinder"/> instance, used to run the shortest distance algorithm on the provided
 19    /// graph.
 20    /// </summary>
 103821    protected IShortestDistanceFinder Finder { get; }
 22
 23    /// <inheritdoc cref="PotentialBasedShortestDistanceFinder"/>
 24    /// <param name="finder">
 25    ///     <inheritdoc cref="Finder" path="*"/>
 26    /// </param>
 6027    protected PotentialBasedShortestDistanceFinder(IShortestDistanceFinder finder)
 6028    {
 6029        Finder = finder;
 6030    }
 31
 32    /// <inheritdoc/>
 33    public (int, IList<int>) Find(IGraph graph, IGraphDistances distances, int start, int end) =>
 425634        Find(graph, distances, v => 0, start, end);
 35
 36    /// <inheritdoc/>
 37    public (int, IList<int>) Find(
 38        IGraph graph, IGraphDistances distances, Func<int, int> potentials, int start, int end)
 103839    {
 103840        var alteredDistances = new PotentialFunctionAlteredGraphDistances(distances, potentials);
 103841        var (alteredDistance, shortestDistancePath) = Finder.Find(graph, alteredDistances, start, end);
 102442        var actualDistance = alteredDistance == int.MaxValue || alteredDistance == int.MinValue
 102443            ? alteredDistance
 102444            : alteredDistance + potentials(start) - potentials(end);
 102445        return (actualDistance, shortestDistancePath);
 102446    }
 47
 48    private sealed class PotentialFunctionAlteredGraphDistances : IGraphDistances
 49    {
 281150        private IGraphDistances OriginalDistances { get; }
 562251        private Func<int, int> Potentials { get; }
 52
 103853        public PotentialFunctionAlteredGraphDistances(
 103854            IGraphDistances originalDistances, Func<int, int> potentials)
 103855        {
 103856            OriginalDistances = originalDistances;
 103857            Potentials = potentials;
 103858        }
 59
 60        public int this[(int edgeStart, int edgeEnd) edge] =>
 281161            OriginalDistances[edge] - Potentials(edge.edgeStart) + Potentials(edge.edgeEnd);
 62    }
 63
 64}