| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.PathWithMaximumProbability; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed 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) |
| | 8 | 28 | | { |
| | 8 | 29 | | if (edges.Length == 0 || successProbability.Length == 0 || startNode == endNode) |
| | 1 | 30 | | { |
| | 1 | 31 | | return startNode == endNode ? 1.0 : 0; |
| | | 32 | | } |
| | | 33 | | |
| | 7 | 34 | | var edgesDictionary = GetEdgesDictionary(edges, successProbability); |
| | | 35 | | |
| | 7 | 36 | | if (!edgesDictionary.ContainsKey(startNode)) |
| | 0 | 37 | | { |
| | 0 | 38 | | return 0; |
| | | 39 | | } |
| | | 40 | | |
| | 7 | 41 | | return GetMaxProbability(edgesDictionary, [startNode], startNode, endNode, 0); |
| | 8 | 42 | | } |
| | | 43 | | |
| | | 44 | | private static double GetMaxProbability(Dictionary<int, List<(int Node, double Probability)>> edgesDictionary, |
| | | 45 | | HashSet<int> visitedEdges, int currentNode, int endNode, double probability) |
| | 65 | 46 | | { |
| | 65 | 47 | | if (currentNode == endNode) |
| | 10 | 48 | | { |
| | 10 | 49 | | return probability; |
| | | 50 | | } |
| | | 51 | | |
| | 55 | 52 | | double maxProbability = 0; |
| | | 53 | | |
| | 391 | 54 | | foreach (var edge in edgesDictionary[currentNode]) |
| | 113 | 55 | | { |
| | 113 | 56 | | if (!visitedEdges.Add(edge.Node)) |
| | 53 | 57 | | { |
| | 53 | 58 | | continue; |
| | | 59 | | } |
| | | 60 | | |
| | 60 | 61 | | var currentProbability = probability == 0 ? edge.Probability : probability * edge.Probability; |
| | | 62 | | |
| | 60 | 63 | | if (currentProbability <= maxProbability) |
| | 2 | 64 | | { |
| | 2 | 65 | | continue; |
| | | 66 | | } |
| | | 67 | | |
| | 58 | 68 | | var currentMaxProbability = |
| | 58 | 69 | | GetMaxProbability(edgesDictionary, visitedEdges, edge.Node, endNode, currentProbability); |
| | | 70 | | |
| | 58 | 71 | | maxProbability = Math.Max(maxProbability, currentMaxProbability); |
| | | 72 | | |
| | 58 | 73 | | visitedEdges.Remove(edge.Node); |
| | 58 | 74 | | } |
| | | 75 | | |
| | 55 | 76 | | return maxProbability; |
| | 65 | 77 | | } |
| | | 78 | | } |