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.
1#include <vector>
2#include <queue>
3#include <utility>
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<vector<pair<int, double>>> graph(n);
11 for (int i = 0; i < edges.size(); ++i) {
12 int u = edges[i][0], v = edges[i][1];
13 double prob = succProb[i];
14 graph[u].emplace_back(v, prob);
15 graph[v].emplace_back(u, prob);
16 }
17
18 vector<double> maxProb(n, 0);
19 maxProb[start] = 1;
20 priority_queue<pair<double, int>> pq;
21 pq.emplace(1.0, start);
22
23 while (!pq.empty()) {
24 auto [prob, node] = pq.top();
25 pq.pop();
26 if (node == end) return prob;
27
28 for (const auto& [nextNode, edgeProb] : graph[node]) {
29 if (maxProb[nextNode] < prob * edgeProb) {
30 maxProb[nextNode] = prob * edgeProb;
31 pq.emplace(maxProb[nextNode], nextNode);
32 }
33 }
34 }
35
36 return 0.0;
37 }
38};
This C++ solution utilizes a priority queue with a pair consisting of the probability and the node, employing a max-heap by default. The algorithm begins with setting the start node's probability to 1.0 and processes nodes with the current highest probability in the queue, updating neighboring node probabilities if a higher probability path is found. This accomplishes the goal of maximizing the product of probabilities along the path similar to the Dijkstra's algorithm adapted for maximum product scenarios.
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.
This JavaScript method entreats similar Bellman-Ford strategies by appending transformation via logarithms and treating probabilistic graph edges. The iteration construct wraps potential updates under a conditional blocking mechanism that truncates superfluous loops through immediate checks (e.g., non-improvable likelihood), ensuring efficient maximum product calculation for path probabilities across network pathways.