Path with Maximum Probability - Complete Solution Guide
Path with Maximum Probability is LeetCode problem 1514, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i] . Given two nodes start and end , find the path with the maximum probability of success to go from start to end and return its success probability. If there is no path from start to end , return 0 . Your answer will be accepted if it differs from the correct answe
Detailed Explanation
The problem asks us to find the path with the maximum probability of success between a starting node and an ending node in a weighted, undirected graph. The graph is represented by a list of edges, where each edge connects two nodes, and a corresponding list of success probabilities for traversing each edge. The goal is to return the maximum probability of reaching the end node from the start node, considering all possible paths. If no path exists, the function should return 0. The result should be accurate to within 1e-5.
Solution Approach
The provided solution uses a modified version of Dijkstra's algorithm to find the path with the maximum probability. The algorithm maintains an adjacency list representation of the graph, a priority queue to store nodes to visit based on their current maximum probability, and an array to track the maximum probability of reaching each node from the starting node. It iteratively explores the graph, updating the maximum probability of reaching each node and adding it to the priority queue if a better probability is found.
Step-by-Step Algorithm
- Step 1: Create an adjacency list representation of the graph. For each edge (u, v) with probability p, add v to u's neighbor list with the associated probability, and add u to v's neighbor list with the same probability (since the graph is undirected).
- Step 2: Initialize an array `max_probs` of size n, where `max_probs[i]` represents the maximum probability of reaching node i from the start node. Set `max_probs[start_node]` to 1.0 (the probability of being at the start node is 1). Initialize all other elements of `max_probs` to 0.0.
- Step 3: Create a max-heap (priority queue) and insert the starting node with its probability (1.0). Store the probability as a negative value to simulate a max-heap using a min-heap implementation.
- Step 4: While the priority queue is not empty:
- a. Extract the node `u` with the highest probability (smallest negative probability) from the priority queue.
- b. If the current probability of `u` is less than the stored `max_probs[u]`, it means we've already found a better path to `u`, so continue to the next iteration.
- c. If `u` is the end node, return the current probability.
- d. Iterate through the neighbors `v` of `u` in the adjacency list:
- i. Calculate the new probability of reaching `v` by multiplying the current probability of `u` with the probability of the edge between `u` and `v`.
- ii. If the new probability is greater than the current `max_probs[v]`, update `max_probs[v]` with the new probability and insert `v` into the priority queue with the negative of the new probability.
- Step 5: If the end node is not reached after exploring all possible paths, it means there is no path from the start node to the end node. Return 0.0.
Key Insights
- Insight 1: This problem is a variation of the shortest path problem. Instead of minimizing distance, we need to maximize probability. Since probabilities are multiplied along a path, we can adapt shortest path algorithms to find the path with maximum probability.
- Insight 2: Dijkstra's algorithm, typically used for shortest path problems, can be adapted here. Instead of minimizing the sum of edge weights, we maximize the product of edge probabilities. Using a priority queue (heap) is crucial for efficiently selecting the next node to explore.
- Insight 3: We need to use a max-heap (priority queue ordered by largest probability) to ensure that we always explore the path with the highest current probability first. Since standard priority queues are usually implemented as min-heaps, we can store negative probabilities to simulate a max-heap.
Complexity Analysis
Time Complexity: O(E + NlogN)
Space Complexity: O(N + E)
Topics
This problem involves: Array, Graph, Heap (Priority Queue), Shortest Path.
Companies
Asked at: BlackRock, tcs.