< Summary

Information
Class: MoreStructures.Graphs.ShortestDistance.ShortestDistanceFinderHelper
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/Graphs/ShortestDistance/ShortestDistanceFinderHelper.cs
Line coverage
100%
Covered lines: 41
Uncovered lines: 0
Coverable lines: 41
Total lines: 63
Line coverage: 100%
Branch coverage
100%
Covered branches: 26
Total branches: 26
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
ValidateParameters(...)100%10100%
BuildShortestPath(...)100%6100%
RelaxOutgoingEdgesOfVertex(...)100%10100%

File(s)

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

#LineLine coverage
 1using MoreStructures.PriorityQueues;
 2using MoreStructures.PriorityQueues.Extensions;
 3
 4namespace MoreStructures.Graphs.ShortestDistance;
 5
 6internal static class ShortestDistanceFinderHelper
 7{
 8    public static void ValidateParameters(IGraph graph, int start, int? end = null)
 22079    {
 220710        var numberOfVertices = graph.GetNumberOfVertices();
 220711        if (start < 0 || start >= numberOfVertices)
 1812            throw new ArgumentException(
 1813                "Must be non-negative and smaller than the total number of vertices in the graph.", nameof(start));
 218914        if (end != null && (end < 0 || end >= numberOfVertices))
 1815            throw new ArgumentException(
 1816                "Must be non-negative and smaller than the total number of vertices in the graph.", nameof(end));
 217117    }
 18
 19    public static IList<int> BuildShortestPath(int end, BestPreviouses bestPreviouses, bool reverse = true)
 131620    {
 131621        var shortestPath = new List<int> { end };
 22
 131623        int current = end;
 353824        while (bestPreviouses.Values.TryGetValue(current, out var bestPrevious))
 353825        {
 353826            current = bestPrevious.PreviousVertex;
 353827            if (current < 0)
 131628                break;
 29
 222230            shortestPath.Add(current);
 222231        }
 32
 131633        if (reverse)
 99834            shortestPath.Reverse();
 131635        return shortestPath;
 131636    }
 37
 38    public static void RelaxOutgoingEdgesOfVertex(
 39        IGraph graph,
 40        Func<int, int, int> distancesFunc,
 41        BestPreviouses bestPreviouses,
 42        HashSet<int> added, IUpdatablePriorityQueue<int> vertexes, int lastAdded)
 431743    {
 2167144        foreach (var (vertex, edgeStart, edgeEnd) in graph.GetAdjacentVerticesAndEdges(lastAdded, true))
 437245        {
 437246            if (added.Contains(vertex))
 2247                continue;
 48
 435049            var edgeDistance = distancesFunc(edgeStart, edgeEnd);
 435050            if (edgeDistance < 0)
 2451                throw new InvalidOperationException(
 2452                    $"Negative edges are not supported: distance of ({edgeStart}, {edgeEnd}) = {edgeDistance}.");
 53
 432654            var newDistance = bestPreviouses.Values[lastAdded].DistanceFromStart + edgeDistance;
 432655            if (!bestPreviouses.Values.TryGetValue(vertex, out var bestPrevious) ||
 432656                bestPrevious.DistanceFromStart > newDistance)
 428057            {
 428058                bestPreviouses.Values[vertex] = new(newDistance, lastAdded);
 428059                vertexes.PushOrUpdate(vertex, -newDistance);
 428060            }
 432661        }
 429362    }
 63}