Sponsored
Sponsored
The approach involves using Dijkstra's algorithm but instead of finding the shortest path, we need to find the path with the maximum product of probabilities. This can be achieved by using a max-heap (or priority queue) to always extend the path with the largest accumulated probability of success. The algorithm initializes a maximum probability set for each node starting from the start
node with a probability of 1, then iteratively updates the probabilities by considering the edges connected to the current node, updating if a path with higher probability is found.
Time Complexity: O(E log V), where E is the number of edges and V is the number of vertices, due to the use of the priority queue.
Space Complexity: O(V + E), for storing the graph and additional data structures like the probability set and heap.
In the JavaScript solution, a custom max heap is implemented to manage nodes by their probability values for ensuring a maximum value path is prioritized. The graph's adjacency list facilitates easy traversal, and the heap operations are used for maintaining paths of the highest known probability during graph search operations. This manual heap control in JavaScript is necessary due to lack of native max-heap data structures in standard JavaScript libraries.
The Bellman-Ford algorithm traditionally calculates shortest paths in a weighted graph and can be adapted here to maximize a product instead. Given the properties of logarithms, maximizing the product of probabilities can be converted to minimizing the sum of the negative logarithm values, allowing direct use of Bellman-Ford's relaxation principle. This transformation reduces the problem of maximizing path probabilities to a more conventional structure that minimization algorithms handle well, iterating across all edges and vertices.
Time Complexity: O(V * E), where V is the number of vertices and E is the number of edges in the graph, each edge potentially causing updates across V-1 iterations.
Space Complexity: O(V), for storing probability values and log-converted results for paths during processing.
1#include <vector>
2#include <cmath>
3#include <limits>
4
5using namespace std;
6
7class Solution {
8public:
9 double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start, int end) {
10 vector<double> logProb(n, -numeric_limits<double>::infinity());
11 logProb[start] = 0;
12
13 for (int i = 0; i < n - 1; ++i) {
14 bool updated = false;
15 for (int j = 0; j < edges.size(); ++j) {
16 int u = edges[j][0], v = edges[j][1];
17 double logProbVal = log(succProb[j]);
18
19 if (logProb[u] != -numeric_limits<double>::infinity() && logProb[u] + logProbVal > logProb[v]) {
20 logProb[v] = logProb[u] + logProbVal;
21 updated = true;
22 }
23 if (logProb[v] != -numeric_limits<double>::infinity() && logProb[v] + logProbVal > logProb[u]) {
24 logProb[u] = logProb[v] + logProbVal;
25 updated = true;
26 }
27 }
28 if (!updated) break;
29 }
30
31 return logProb[end] != -numeric_limits<double>::infinity() ? exp(logProb[end]) : 0.0;
32 }
33};
This C++ implementation involves a careful handling of numerical limits and logarithms. It executes in typical Bellman-Ford style across all edges, allowing for detection and maximization of paths using logarithmic transformations on probabilities. Looping for V-1 times enables comprehensive coverage of potential paths for the initial input conditions, with early termination ensured upon stagnation through exploitation of the updated state flag.