| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.PathWithMaximumProbability; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public 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) |
| 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, n, startNode, endNode); |
| 8 | 42 | | } |
| | 43 | |
|
| | 44 | | private static double GetMaxProbability(Dictionary<int, List<(int Node, double Probability)>> edgesDictionary, |
| | 45 | | int n, int startNode, int endNode) |
| 7 | 46 | | { |
| 7 | 47 | | var probabilityNodesPriorityQueue = new PriorityQueue<(double Probability, int Node), double>(); |
| | 48 | |
|
| 7 | 49 | | probabilityNodesPriorityQueue.Enqueue((1.0, startNode), -1.0); |
| | 50 | |
|
| 7 | 51 | | var maxProbability = new double[n]; |
| | 52 | |
|
| 7 | 53 | | maxProbability[startNode] = 1.0; |
| | 54 | |
|
| 40 | 55 | | while (probabilityNodesPriorityQueue.Count > 0) |
| 39 | 56 | | { |
| 39 | 57 | | var probabilityNode = probabilityNodesPriorityQueue.Dequeue(); |
| | 58 | |
|
| 39 | 59 | | if (probabilityNode.Node == endNode) |
| 6 | 60 | | { |
| 6 | 61 | | return probabilityNode.Probability; |
| | 62 | | } |
| | 63 | |
|
| 235 | 64 | | foreach (var edge in edgesDictionary[probabilityNode.Node]) |
| 68 | 65 | | { |
| 68 | 66 | | var probability = probabilityNode.Probability * edge.Probability; |
| | 67 | |
|
| 68 | 68 | | if (probability <= maxProbability[edge.Node]) |
| 32 | 69 | | { |
| 32 | 70 | | continue; |
| | 71 | | } |
| | 72 | |
|
| 36 | 73 | | maxProbability[edge.Node] = probability; |
| | 74 | |
|
| 36 | 75 | | probabilityNodesPriorityQueue.Enqueue((probability, edge.Node), probability * -1); |
| 36 | 76 | | } |
| 33 | 77 | | } |
| | 78 | |
|
| 1 | 79 | | return 0; |
| 7 | 80 | | } |
| | 81 | | } |