Number of Restricted Paths From First to Last Node in JavaAn undirected weighted linked graph is given. A positive integer n indicates that there are n nodes in the graph, numbered from 1 to n. We are also provided an array of edges, where each edges[i] = [ui, vi, weighti] indicates that there is an edge connecting nodes ui and vi, with weight equal to weight i. An edge exists between zi and zi+1 where 0 <= i <= k-1, and a path from node start to node end is a sequence of nodes [z0, z1, z2,..., zk] such that z0 = start and zk = end. The total of the weights on a path's edges determines its length. The shortest path length between node n and node x is shown by the variable distanceToLastNode(x). A path is considered restricted if it additionally meets the condition that distanceToLastNode(zi) > distanceToLastNode(zi+1), with 0 <= i <= k-1. Return how many limited pathways there are between node 1 and node n. Return that amount modulo 109 + 7, as it might be too much. Example 1: Input: int n = 5 int edge = [[1,2,3],[1,3,3],[2,3,1],[1,4,2],[5,2,2],[3,5,1],[5,4,10]] Output: The number of restricted paths from the first to last node is 3. Explanation: The node number is shown in black on each circle, while the distanceToLastNode value is shown in blue. The following three pathways are restricted:
Example 2: Input: int n = 7 int edge = [[1,3,1],[4,1,2],[7,3,4],[2,5,3],[5,6,1],[6,7,2],[7,5,3],[2,6,4]] Output: The number of restricted paths from the first to last node is 1. Explanation: The node number is shown in black on each circle, while the distanceToLastNode value is shown in blue. The following three pathways are restricted:
Approach: Shortest Path Calculation (Dijkstra's Algorithm)We begin by applying Dijkstra's algorithm to determine the shortest path between each node and node n. The next node with the shortest distance is efficiently obtained by using a priority queue (min-heap). We calculate the number of restricted pathways from node 1 to node n using the calculated distances. To make certain that, before processing a node, all nodes with shorter distances have already been processed, we employ a priority queue to process nodes in the order in which they are separated from node n. If the distance to n from y is more than the distance from x (distance[y] > distance[x]) for each node x and each neighbour y, then we can move from x to y along a restricted path. The number of limited paths to x is then added to update the number of restricted paths to y. Algorithm: Step 1: For expressing the graph, create an adjacency list. Every node will have a list of pairs in it, with each pair consisting of the weight of the edge connecting it to a neighbouring node. Step 2: Add the matching pairings to the adjacency list for both nodes for each edge in the input. Step 3: Build an array called "distance" to hold the shortest path between each node and node n. Initialize all distances to infinity, except for node n, which has a value of 0. Step 4: After setting the count for node n to 1, create an array ans to count the number of restricted pathways from each node to node n. Step 5: Put Dijkstra's method into practice using a priority queue (min-heap). Add node n first, ensuring that its distance is 0. Step 6: While the priority queue is still filled, apply Dijkstra's Algorithm. Extract the node that is closest in step 6.1. Step 6.2: Proceed to the following iteration if this distance exceeds the node's recorded distance. Step 6.3: Update the neighbour's distance and add it to the priority queue if a shorter path is discovered to reach them. Step 6.4: Add the number of restricted paths for the current node to the number of restricted paths for the neighbour if the distance between them is larger than the distance to the current node. Step 7: Return how many paths are restricted from node 1 to node n. Implementation:FileName: RestrictedPaths.java Output: The number of restricted paths is: 3 Complexity Analysis: The Time Complexity of the above code is O((n+m)logn) and the Space Complexity of the above code is O(n+m). |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India