Strongest Signal Route in a Lossy Link Network
Trees & GraphsAuto-graded · 19 test casesLast updated
Find the route between two nodes in an undirected graph that maximizes the product of per-link delivery probabilities, using a Dijkstra-style search with a max-priority queue.
Strongest Signal Route
A data center consists of n machines numbered 0 to n - 1. Machines are joined by bidirectional links. Each link is described by a triple [a, b, p]: it joins machines a and b, and a message sent across it arrives successfully with probability p, where 0.0 <= p <= 1.0.
The delivery probability of a route is the product of the probabilities of all links used on that route. A route that uses no links (that is, when the source and destination are the same machine) has delivery probability 1.0.
Write a function that returns the highest delivery probability achievable by any route from source to target. If the target cannot be reached from the source, return 0.0.
The input may contain parallel links (several links between the same pair of machines), self-loops, cycles, links with probability 0, and machines in separate disconnected parts of the network. Your answer must handle all of these correctly.
Parameters
n: the number of machines.links: a list of[a, b, p]triples as described above.source: the starting machine.target: the destination machine.
Return value
A float: the maximum delivery probability over all routes from source to target, or 0.0 if no route gives a positive probability or none exists. An answer within 1e-5 of the true value is accepted.
Constraints
1 <= n <= 10^50 <= len(links) <= 2 * 10^5- Each link is
[a, b, p]with0 <= a, b < nand0.0 <= p <= 1.0 0 <= source, target < n- Parallel links and self-loops may appear.
- Answers within
1e-5of the exact value are accepted.
Examples
Example 1
Input: strongest_route(3, [[0, 1, 0.5], [1, 2, 0.5], [0, 2, 0.2]], 0, 2)
Output: 0.25
Example 2
Input: strongest_route(4, [[0, 1, 0.8], [1, 2, 0.5], [2, 3, 0]], 0, 3)
Output: 0
Example 3
Input: strongest_route(3, [[0, 1, 0.4]], 0, 2)
Output: 0
Hints
Hint 1
Multiplying by a number between 0 and 1 never increases a value, so extending a route can never make it more reliable. This is the same monotonic behavior that makes Dijkstra work for non-negative distances.
Hint 2
Adapt Dijkstra: replace addition with multiplication, and always expand the node with the largest known probability. In Python, push negated probabilities onto heapq to simulate a max-heap.
Hint 3
Only push a neighbor when the new probability is strictly greater than the best one recorded so far. This skips zero-probability links, self-loops and useless cycles, and you can ignore stale heap entries when popping.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a shortest-path problem in disguise. Every link probability lies in [0, 1], so extending a route can never raise its delivery probability. That is the same property Dijkstra's algorithm needs from non-negative edge weights, so we run Dijkstra with two changes:
- Combine values by multiplication rather than addition.
- Always expand the node with the largest probability, using a max-heap (negated values in Python's
heapq).
Steps:
- Build an adjacency list, adding every link in both directions.
- Keep
best[v], the highest probability found so far for reachingv. Setbest[source] = 1.0and all others to0.0. - Pop the entry with the highest probability. If it is stale (lower than
best[node]), skip it. - If the popped node is
target, return its probability. - Otherwise, for each neighbor compute
prob * link_p. If it is strictly greater thanbest[neighbor], record it and push it. - If the heap empties, the target is unreachable, so return
0.0.
Why it is correct
When a node is popped with a non-stale probability p, no route to it can be better. Suppose a better route with probability q > p existed. Along that route there is a first node x not yet finalized, whose predecessor was finalized, so x is in the heap with a value at least as large as the prefix probability of that route. Since every remaining factor is at most 1, that prefix value is at least q > p, so x would have been popped before our node, a contradiction.
Equivalently, using weights -log(p) >= 0 turns products into sums and maximization into minimization, which is standard Dijkstra.
Edge cases fall out naturally:
source == target: the first pop is the source with probability1.0, which is returned.- Zero-probability links: the candidate is
0.0, never strictly greater than the initial0.0, so nothing is pushed and such links never help. - Disconnected target: the heap empties and
0.0is returned. - Parallel links: both are in the adjacency list and the better one wins during relaxation.
- Self-loops and cycles: the candidate is never strictly better than the existing value, so they are ignored.
Complexity
- Time:
O((V + E) log E), since each successful relaxation pushes at most once and each heap operation costsO(log E). - Space:
O(V + E)for the adjacency list, thebestarray, and the heap.
Reference solution
import heapq
from typing import List
def strongest_route(n: int, links: List[List[float]], source: int, target: int) -> float:
graph = [[] for _ in range(n)]
for a, b, p in links:
graph[a].append((b, p))
graph[b].append((a, p))
best = [0.0] * n
best[source] = 1.0
heap = [(-1.0, source)]
while heap:
neg_prob, node = heapq.heappop(heap)
prob = -neg_prob
if prob < best[node]:
continue
if node == target:
return prob
for nxt, p in graph[node]:
cand = prob * p
if cand > best[nxt]:
best[nxt] = cand
heapq.heappush(heap, (-cand, nxt))
return 0.0