Frog Position After T Seconds Problem
Description
LeetCode Problem 1377.
Given an undirected tree consisting of n vertices numbered from 1 to n. A frog starts jumping from vertex 1. In one second, the frog jumps from its current vertex to another unvisited vertex if they are directly connected. The frog can not jump back to a visited vertex. In case the frog can jump to several vertices, it jumps randomly to one of them with the same probability. Otherwise, when the frog can not jump to any unvisited vertex, it jumps forever on the same vertex.
The edges of the undirected tree are given in the array edges, where edges[i] = [a_i, b_i] means that exists an edge connecting the vertices a_i and b_i.
Return the probability that after t seconds the frog is on the vertex target. Answers within 10^(-5) of the actual answer will be accepted.
Example 1:

Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 2, target = 4
Output: 0.16666666666666666
Explanation: The figure above shows the given graph. The frog starts at vertex 1, jumping with 1/3 probability to the vertex 2 after second 1 and then jumping with 1/2 probability to vertex 4 after second 2. Thus the probability for the frog is on the vertex 4 after 2 seconds is 1/3 * 1/2 = 1/6 = 0.16666666666666666.
Example 2:

Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 1, target = 7
Output: 0.3333333333333333
Explanation: The figure above shows the given graph. The frog starts at vertex 1, jumping with 1/3 = 0.3333333333333333 probability to the vertex 7 after second 1.
Constraints:
1 <= n <= 100edges.length == n - 1edges[i].length == 21 <= a_i, b_i <= n1 <= t <= 501 <= target <= n
Solution
Approach: BFS by Seconds
Because the graph is a tree and the frog never goes back, after s seconds it is somewhere at depth s (or stuck earlier on a leaf). There is only one path from vertex 1 to target, so we can follow the probability level by level with a BFS:
- Start with vertex 1 and probability
1. - For each vertex
uin the current level, its unvisited neighbours are its children. The frog picks each of them with probabilityp / (number of children).
When we reach target after s seconds with probability p, there are two cases:
- If
s == t, ortargethas no unvisited neighbour (a leaf, where the frog keeps jumping in place), the frog is still there at secondt: returnp. - Otherwise the frog must jump away before second
t, so the answer is0.
If the BFS runs out of seconds before reaching target, the frog cannot get there in time and the answer is also 0.
The tricky case is the root: vertex 1 has no parent, so all of its neighbours are children. Counting "unvisited neighbours" handles it without a special case, and so does n = 1, where the frog sits on vertex 1 forever.
Complexity: O(n) time and O(n) extra space for the adjacency lists and the BFS levels.
class Solution:
def frogPosition(self, n: int, edges: List[List[int]], t: int, target: int) -> float:
adj = [[] for _ in range(n + 1)]
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
visited = [False] * (n + 1)
visited[1] = True
level = [(1, 1.0)] # (vertex, probability) for the vertices the frog can be on after `second` seconds
for second in range(t + 1):
next_level = []
for u, p in level:
children = [v for v in adj[u] if not visited[v]]
if u == target:
# The frog has to stay here: either time is up, or it has nowhere left to jump.
return p if second == t or not children else 0.0
for v in children:
visited[v] = True
next_level.append((v, p / len(children)))
level = next_level
return 0.0 # target is farther than t jumps from vertex 1