Frog Position After T Seconds

Given an undirected tree consisting of n vertices numbered from 1 to n. A frog starts jumping from the 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] = [fromi, toi] means that exists an edge connecting directly the vertices fromi and toi.

Return the probability that after t seconds the frog is on the vertex target.

 

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. 

Example 3:

Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 20, target = 6
Output: 0.16666666666666666

 

Constraints:


Solution:

class Solution {
    static class Node {
        int pos;
        double prob;
        
        public Node(int pos, double prob) {
            this.pos = pos;
            this.prob = prob;
        }
    }
    
    public double frogPosition(int n, int[][] edges, int t, int target) {
        Map<Integer, Set<Integer>> graph = new HashMap();
        for (int[] edge : edges) {
            int from = edge[0], end = edge[1];
            graph.putIfAbsent(from, new HashSet());
            graph.get(from).add(end);
            graph.putIfAbsent(end, new HashSet());
            graph.get(end).add(from);
        }
        Node start = new Node(1, 1.0);
        Deque<Node> queue = new ArrayDeque();
        boolean[] visited = new boolean[101];
        queue.offer(start);
        visited[1] = true;
        int time = 0;
        while (!queue.isEmpty()) {
            if (time > t) break;
            int size = queue.size();
            for (int i = 0; i < size; i ++) {
                Node curr = queue.poll();
                // System.out.println(curr.pos + ", " + curr.prob);
                if (curr.pos == target && t == time) {
                    return curr.prob;
                }
                Set<Integer> neighbors = graph.getOrDefault(curr.pos, new HashSet());
                int seen = 0;
                for (int next : neighbors) {
                    if (visited[next]) seen ++;
                }
                for (int next : neighbors) {
                    if (!visited[next]) {
                        visited[next] = true;
                        Node nextNode = new Node(next, curr.prob * (1.0 / (neighbors.size() - seen)));
                        queue.offer(nextNode);
                    }
                }
                if (seen == neighbors.size()) {
                    queue.offer(curr);
                }
            }
            time ++;
        }
        return 0.0;
    }
}