|
| 1 | +# Frog Position After T Seconds |
| 2 | + |
| 3 | +You are given an undirected tree with n vertices labeled from 1 to `n`. A frog starts at vertex 1, at time 0, and makes |
| 4 | +one move per second. |
| 5 | + |
| 6 | +At each step, the frog follows these rules: |
| 7 | + |
| 8 | +1. Move to an unvisited neighbor: |
| 9 | + If the frog has unvisited neighbors, it jumps to one of them, chosen uniformly at random (equal probability for each |
| 10 | + choice). |
| 11 | + |
| 12 | +2. No revisiting: The frog can not jump back to a vertex it has already visited. |
| 13 | +3. Stay when stuck: The frog will keep jumping at its current vertex if there are no unvisited neighbors. |
| 14 | + |
| 15 | +The tree is represented as an array of edges, where `edges[i] = [ai, bi]` means an edge connecting the vertices |
| 16 | +`ai` and `bi`. Your task is to return the probability that, after t seconds, the frog is on the vertex target. |
| 17 | + |
| 18 | +Constraints |
| 19 | + |
| 20 | +- 1 ≤ n ≤ 100 |
| 21 | +- `edges.length` == `n − 1` |
| 22 | +- edges[i].length == 2 |
| 23 | +- 1 ≤ ai, bi ≤ n |
| 24 | +- 1 ≤ t ≤ 50 |
| 25 | +- 1 ≤ target ≤ n |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +## Solution |
| 36 | + |
| 37 | +The essence of the solution lies in simulating the frog’s movement with a level-order BFS, where each level is one |
| 38 | +second. At any second, if the frog is on a node with unvisited neighbors, it must jump to one of them at random, all |
| 39 | +with the same chance, because it cannot revisit nodes, only unvisited neighbors are considered. If a node has no |
| 40 | +unvisited neighbors, the frog stays there for the remaining time. |
| 41 | + |
| 42 | +There are three cases for the `target`: |
| 43 | + |
| 44 | +- The frog arrives exactly at second `t` → that chance is the answer. |
| 45 | +- The frog arrives earlier, and the `target` has no unvisited neighbors → it stays, so that chance still counts at time `t`. |
| 46 | +- The frog arrives earlier, but the `target` does have unvisited neighbors and time remains → it must leave, so that |
| 47 | + chance at time t is 0. |
| 48 | + |
| 49 | +Using the intuition above, we implement the algorithm as follows: |
| 50 | + |
| 51 | +1. Build an adjacency list `tree` from `edges` to quickly look up all neighbors of any node. |
| 52 | +2. Initialize a queue with a single state (node = 1, probability = 1.0), a visited array of size n + 1 with visited[1] = |
| 53 | +true, and set time_left = t. |
| 54 | +3. While the queue is not empty and time_left >= 0: |
| 55 | + - Record the current level_size as this represents all the frog’s positions at this second. |
| 56 | + - Repeat level_size times: |
| 57 | + - Pop (u, p) from the queue. |
| 58 | + - Count `cnt = number` of unvisited neighbors of u (check visited[v] == false for each neighbor v). |
| 59 | + - If u == target: |
| 60 | + - If time_left == 0, return p because the frog arrived exactly on time. |
| 61 | + - If cnt == 0, return p because the frog arrived earlier and can not move, so it remains on the target. |
| 62 | + - Otherwise if time_left > 0 and cnt > 0, then return 0.0 because the frog must leave before the time runs out. |
| 63 | + - If cnt > 0: |
| 64 | + - For each unvisited neighbor v of u: |
| 65 | + - Mark visited[v] = true. |
| 66 | + - Enqueue (v, p / cnt), since the probability is split equally among all unvisited neighbors. |
| 67 | + - Decrement time_left by 1. |
| 68 | +4. If the loop ends without returning (the frog never satisfied the target conditions in time), return 0.0. |
| 69 | + |
| 70 | +Let’s look at the following illustration to get a better understanding of the solution: |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +### Time complexity |
| 80 | + |
| 81 | +The time complexity of this solution is `O(n)`. Building the adjacency list takes linear time in a tree (E = n − 1). |
| 82 | +The BFS then visits each node at most once and inspects each edge at most twice. Even if t is larger than the tree’s |
| 83 | +depth, the traversal stops once the queue is empty, so the work does not grow with t. |
| 84 | + |
| 85 | +### Space complexity |
| 86 | + |
| 87 | +The space complexity is `O(n)`. The adjacency list requires O(n) space, the visited array is O(n), and the BFS queue |
| 88 | +can hold up to O(n) nodes in the worst case. |
0 commit comments