跳到主要内容

Path with Maximum Probability

描述​

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<=1042 <= n <= 10^4
  • 0≤start,end<n0 \leq \text{start}, \text{end} < n
  • start != end
  • 0≤a,b<n0 \leq a, b < n
  • a != b
  • 0≤succProb.length=edges.length≤2×1040 \leq \text{succProb.length} = \text{edges.length} \leq 2 \times 10^4
  • 0≤succProb[i]≤10 \leq \text{succProb}[i] \leq 1
  • There is at most one edge between every two nodes.

分析​

由于每条边的概率是介于[0,1]之间的正数,且路径上的概率是累乘起来的,那么对原图G中的每条边的权重取对数然后取反,就得到了一个边权在[0,∞)[0, \infty)之间的图G',图G中“从起点到终点成功概率最大”的路径对应了图G'中“从起点到终点边权之和最小”的路径。由于图G'中没有负数边权,用 Dijkstra 算法最合适不过了。

代码​

# Path with Maximum Probability
# Dijkstra
# Time Complexity: O(ElogN), Space Complexity: O(N + E)
from collections import defaultdict
import heapq
import math

class Solution:
def maxProbability(self, n: int, edges: list[list[int]], succProb: list[float], start: int, end: int) -> float:
# adjacency list, dict<vertex_id, dict<vertex_id, weight>>
graph = defaultdict(dict)
for i, edge in enumerate(edges):
w = -math.log(succProb[i])
# Undirected
graph[edge[0]][edge[1]] = w
graph[edge[1]][edge[0]] = w

dist = self.dijkstra(graph, start)
return math.exp(-dist[end]) if end in dist else 0

def dijkstra(self, graph, start):
"""Standard Dijkstra algorithm.

Args:
graph: Adjacency list, dict<vertex_id, dict<vertex_id, weight>>.
start: The starting vertex ID.
Returns:
dist: dict<vertex_id, distance>.
"""
# dict<vertex_id, distance>
dist = {}
# vertex_id -> father_vertex_id
father = {}

# tuple(distance, vertex_id), min heap, sorted by distance from start to vertex_id
pq = [(0.0, start)]
dist[start] = 0.0

while pq:
_, u = heapq.heappop(pq)
if u not in graph: # leaf node
continue

for v, w in graph[u].items():
if v not in dist or dist[u] + w < dist[v]:
shorter = dist[u] + w
dist[v] = shorter
father[v] = u
heapq.heappush(pq, (shorter, v))

return dist

相关题目​