< Summary

Information
Class: LeetCode.Algorithms.PathWithMaximumProbability.PathWithMaximumProbabilityDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PathWithMaximumProbability\PathWithMaximumProbabilityDepthFirstSearch.cs
Line coverage
93%
Covered lines: 29
Uncovered lines: 2
Coverable lines: 31
Total lines: 78
Line coverage: 93.5%
Branch coverage
90%
Covered branches: 18
Total branches: 20
Branch coverage: 90%
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%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PathWithMaximumProbability\PathWithMaximumProbabilityDepthFirstSearch.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 PathWithMaximumProbabilityDepthFirstSearch : PathWithMaximumProbabilityBase
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m + 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, [startNode], startNode, endNode, 0);
 842    }
 43
 44    private static double GetMaxProbability(Dictionary<int, List<(int Node, double Probability)>> edgesDictionary,
 45        HashSet<int> visitedEdges, int currentNode, int endNode, double probability)
 6546    {
 6547        if (currentNode == endNode)
 1048        {
 1049            return probability;
 50        }
 51
 5552        double maxProbability = 0;
 53
 39154        foreach (var edge in edgesDictionary[currentNode])
 11355        {
 11356            if (!visitedEdges.Add(edge.Node))
 5357            {
 5358                continue;
 59            }
 60
 6061            var currentProbability = probability == 0 ? edge.Probability : probability * edge.Probability;
 62
 6063            if (currentProbability <= maxProbability)
 264            {
 265                continue;
 66            }
 67
 5868            var currentMaxProbability =
 5869                GetMaxProbability(edgesDictionary, visitedEdges, edge.Node, endNode, currentProbability);
 70
 5871            maxProbability = Math.Max(maxProbability, currentMaxProbability);
 72
 5873            visitedEdges.Remove(edge.Node);
 5874        }
 75
 5576        return maxProbability;
 6577    }
 78}