Problem
Ask
Submissions

Problem: Frog Position After T Seconds

Medium
30 min
Explore how to use breadth-first search on an undirected tree to calculate the probability that a frog reaches a target vertex after t seconds. Understand how to model random jumps to unvisited neighbors and handle cases with no available moves. This lesson helps you develop skills in probabilistic tree traversal relevant for coding interviews.

Statement

You are given an undirected tree with n vertices labeled from 11 to nn. A frog starts at vertex 11, at time 00, and makes one move per second.

At each step, the frog follows these rules:

  1. Move to an unvisited neighbor:
    If the frog has unvisited neighbors, it jumps to one of them, chosen uniformly at random (equal probability for each choice).

  2. No revisiting:
    The frog can not jump back to a vertex it has already visited.

  3. Stay when stuck
    The frog will keep jumping at its current vertex if there are no unvisited neighbors.

The tree is represented as an array of edges, where edges[i] = [aia_i, bib_i] means an edge connecting the vertices aia_i and bib_i. Your task is to return the probability that, after t seconds, the frog is on the vertex target.

Note: Your answer will be accepted if its absolute difference from the actual value is at most 10510^{-5}.

Constraints:

  • 1 \leq n \leq100

  • edges.length == n1n - 1

  • edges[i].length == 2

  • 11 \leq aia_i, bib_i \leq n

  • 11\leq t \leq 5050

  • 11 \leq target \leqn

Problem
Ask
Submissions

Problem: Frog Position After T Seconds

Medium
30 min
Explore how to use breadth-first search on an undirected tree to calculate the probability that a frog reaches a target vertex after t seconds. Understand how to model random jumps to unvisited neighbors and handle cases with no available moves. This lesson helps you develop skills in probabilistic tree traversal relevant for coding interviews.

Statement

You are given an undirected tree with n vertices labeled from 11 to nn. A frog starts at vertex 11, at time 00, and makes one move per second.

At each step, the frog follows these rules:

  1. Move to an unvisited neighbor:
    If the frog has unvisited neighbors, it jumps to one of them, chosen uniformly at random (equal probability for each choice).

  2. No revisiting:
    The frog can not jump back to a vertex it has already visited.

  3. Stay when stuck
    The frog will keep jumping at its current vertex if there are no unvisited neighbors.

The tree is represented as an array of edges, where edges[i] = [aia_i, bib_i] means an edge connecting the vertices aia_i and bib_i. Your task is to return the probability that, after t seconds, the frog is on the vertex target.

Note: Your answer will be accepted if its absolute difference from the actual value is at most 10510^{-5}.

Constraints:

  • 1 \leq n \leq100

  • edges.length == n1n - 1

  • edges[i].length == 2

  • 11 \leq aia_i, bib_i \leq n

  • 11\leq t \leq 5050

  • 11 \leq target \leqn