1514. Path with Maximum Probability
Path with Maximum Probability (LeetCode 1514) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Shortest Path (Dijkstra's Algorithm) subpattern.
How to solve Path with Maximum Probability
Path with Maximum Probability is a Dijkstra's Algorithm for Shortest Path problem, part of Graph Traversal Patterns (DFS & BFS). Master Dijkstra's algorithm for finding shortest paths in weighted graphs with non-negative edges.
Work towards the solution in these steps:
- Multiplying probabilities will result in precision errors.
- Take log probabilities to sum up numbers instead of multiplying them.
- Use Dijkstra's algorithm to find the minimum path between the two nodes after negating all costs.
The reference solution runs in O(M log N) time and O(M + N) space.
The Path with Maximum Probability solution and editorial walks through the full approach with reference code.
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 answer by at most 1e-5.
Example 1:
Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2 Output: 0.25000 Explanation: There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 0.5 = 0.25.
Example 2:
Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2 Output: 0.30000
Example 3:
Input: n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2 Output: 0.00000 Explanation: There is no path between 0 and 2.
Constraints:
- 2 <= n <= 10^4
- 0 <= start, end < n
- start != end
- 0 <= a, b < n
- a != b
- 0 <= succProb.length == edges.length <= 210^4
- 0 <= succProb[i] <= 1
- There is at most one edge between every two nodes.
Topics and companies
Topics: Array, Graph, Heap (Priority Queue), Shortest Path.
Reported in interviews at Google.
How to practise Path with Maximum Probability on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(M log N) time and O(M + N) space.
Open Path with Maximum Probability in the code editor, or read the Path with Maximum Probability editorial for a worked solution with its approach and complexity analysis.
Where Path with Maximum Probability sits in the DSA pattern sheet
- Graph Traversal Patterns (DFS & BFS) pattern guide
- Shortest Path (Dijkstra's Algorithm) subpattern
- The full DSA patterns sheet
Problems related to Path with Maximum Probability
Other problems that use the same Shortest Path (Dijkstra's Algorithm) and Graph Traversal Patterns (DFS & BFS) techniques:
- Find the Safest Path in a Grid — Medium, same subpattern
- Minimum Obstacle Removal to Reach Corner — Hard, same subpattern
- Minimum Time to Visit a Cell In a Grid — Hard, same subpattern
- Minimum Weighted Subgraph With the Required Paths — Hard, same subpattern
- Network Delay Time — Medium, same subpattern
- Number of Ways to Arrive at Destination — Medium, same subpattern
Path with Maximum Probability FAQ
What is the LeetCode number of Path with Maximum Probability?
Path with Maximum Probability is LeetCode problem 1514, rated Medium.
What pattern is used to solve Path with Maximum Probability?
Path with Maximum Probability is a Dijkstra's Algorithm for Shortest Path problem. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Shortest Path (Dijkstra's Algorithm) subpattern.
What is the time complexity of Path with Maximum Probability?
The reference solution runs in O(M log N) time and O(M + N) space.
Which companies ask Path with Maximum Probability?
Path with Maximum Probability has been reported in interviews at Google.