< Summary

Information
Class: LeetCode.Algorithms.PathWithMaximumProbability.PathWithMaximumProbabilityDijkstra
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PathWithMaximumProbability\PathWithMaximumProbabilityDijkstra.cs
Line coverage
93%
Covered lines: 31
Uncovered lines: 2
Coverable lines: 33
Total lines: 81
Line coverage: 93.9%
Branch coverage
88%
Covered branches: 16
Total branches: 18
Branch coverage: 88.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaxProbability(...)80%111080%
GetMaxProbability(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PathWithMaximumProbability\PathWithMaximumProbabilityDijkstra.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.PathWithMaximumProbability;
 13
 14/// <inheritdoc />
 15public class PathWithMaximumProbabilityDijkstra : PathWithMaximumProbabilityBase
 16{
 17    /// <summary>
 18    ///     Time complexity - O((m + n) log n), where m is the number of edges and n is the number of nodes
 19    ///     Space complexity - O(m + n), where m is the number of edges and n is the number of nodes
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <param name="edges"></param>
 23    /// <param name="successProbability"></param>
 24    /// <param name="startNode"></param>
 25    /// <param name="endNode"></param>
 26    /// <returns></returns>
 27    public override double MaxProbability(int n, int[][] edges, double[] successProbability, int startNode, int endNode)
 828    {
 829        if (edges.Length == 0 || successProbability.Length == 0 || startNode == endNode)
 130        {
 131            return startNode == endNode ? 1.0 : 0;
 32        }
 33
 734        var edgesDictionary = GetEdgesDictionary(edges, successProbability);
 35
 736        if (!edgesDictionary.ContainsKey(startNode))
 037        {
 038            return 0;
 39        }
 40
 741        return GetMaxProbability(edgesDictionary, n, startNode, endNode);
 842    }
 43
 44    private static double GetMaxProbability(Dictionary<int, List<(int Node, double Probability)>> edgesDictionary,
 45        int n, int startNode, int endNode)
 746    {
 747        var probabilityNodesPriorityQueue = new PriorityQueue<(double Probability, int Node), double>();
 48
 749        probabilityNodesPriorityQueue.Enqueue((1.0, startNode), -1.0);
 50
 751        var maxProbability = new double[n];
 52
 753        maxProbability[startNode] = 1.0;
 54
 4055        while (probabilityNodesPriorityQueue.Count > 0)
 3956        {
 3957            var probabilityNode = probabilityNodesPriorityQueue.Dequeue();
 58
 3959            if (probabilityNode.Node == endNode)
 660            {
 661                return probabilityNode.Probability;
 62            }
 63
 23564            foreach (var edge in edgesDictionary[probabilityNode.Node])
 6865            {
 6866                var probability = probabilityNode.Probability * edge.Probability;
 67
 6868                if (probability <= maxProbability[edge.Node])
 3269                {
 3270                    continue;
 71                }
 72
 3673                maxProbability[edge.Node] = probability;
 74
 3675                probabilityNodesPriorityQueue.Enqueue((probability, edge.Node), probability * -1);
 3676            }
 3377        }
 78
 179        return 0;
 780    }
 81}