1514 Path With Maximum Probability

1514 Path With Maximum Probability#

Problem#

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.

Examples#

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.

Constraint#

2 <= n <= 10^4
0 <= start, end < n
start != end
0 <= a, b < n
a != b
0 <= succProb.length == edges.length <= 2*10^4
0 <= succProb[i] <= 1
There is at most one edge between every two nodes.

Analysis#

Three methods can be used for solving this shortest weighted path problems:

  • BFS

  • DFS

  • Dijkstra

    • However, Dijkstra’s algorithm is for DAG, how to use it for undirected graph?

      • construct directed graph from undirected graph by treating it as bidirectional graph

Solution#

# Dijkstra's algorithm 1
def maxProbability(n, edges, succProb, start, end):
    # build a graph
    graph = {}
    prob = {}
    for i in range(n):
        graph[i] = []
    for u, v in edges:
        graph[u].append(v)
        graph[v].append(u)
    for i in range(len(edges)):
        u, v = edges[i]
        prob[(u, v)] = succProb[i]
        prob[(v, u)] = succProb[i]
    # dijkstra
    distances = [0]*n 
    distances[start] = 1
    # define a queue
    queue = [(distances[start], start)]

    while queue:
        # pop the node
        distance, node = queue.pop(0)

        # explore the neighbors
        for neighbor in graph[node]:
            distance_neighbor = distance * prob[(node, neighbor)]
            # enqueue the neighbor if the distance is larger
            if distance_neighbor > distances[neighbor]:
                distances[neighbor] = distance_neighbor
                queue.append((distances[neighbor], neighbor))
    
    return distances[end]

# test
n = 3
edges = [[0,1],[1,2],[0,2]]
succProb = [0.5,0.5,0.2]
start = 0
end = 2
print(maxProbability(n, edges, succProb, start, end))

n = 3
edges = [[0,1]]
succProb = [0.5]
start = 0
end = 2
print(maxProbability(n, edges, succProb, start, end))
0.25
0

The above code calculates the max probability for all the nodes, and then return the value for end node, which can be optimized by removing unneeded calculations.

We have to rely on a max priority heap or max method to achieve this type of early returns.

  • if use max, the time complexity will be O(n), which lead to an overall complexity of O(n^2)

  • if use max-heap, the time complexity will be O(log(n)), and the overall complexity is O(nlog(n))

def maxProbability(n, edges, succProb, start, end):
    # build a graph
    graph = {}
    prob = {}
    for i in range(n):
        graph[i] = []
    for u, v in edges:
        graph[u].append(v)
        graph[v].append(u)
    for i in range(len(edges)):
        u, v = edges[i]
        prob[(u, v)] = succProb[i]
        prob[(v, u)] = succProb[i]
    
    # dijkstra
    distances = [0]*n
    distances[start] = 1
    # 
    queue = [(distances[start], start)]

    while queue:
        # pop the max prob node
        distance, node = max(queue)
        queue.remove((distance, node))

        # early return
        if node == end:
            return distance

        # explore the neighbors
        for neighbor in graph[node]:
            distance_neighbor = distance * prob[(node, neighbor)]
            # enqueue the neighbor if the distance is larger
            if distance_neighbor > distances[neighbor]:
                distances[neighbor] = distance_neighbor
                queue.append((distances[neighbor], neighbor))
                
    return distances[end]

# test
n = 3
edges = [[0,1],[1,2],[0,2]]
succProb = [0.5,0.5,0.2]
start = 0
end = 2
print(maxProbability(n, edges, succProb, start, end))

n = 3
edges = [[0,1]]
succProb = [0.5]
start = 0
end = 2
print(maxProbability(n, edges, succProb, start, end))
0.25
0