|
| 1 | +# Explanation of the Program |
| 2 | + |
| 3 | +### **Objective** |
| 4 | +The goal of the program is to count the number of distinct shortest paths from node `0` to node `n-1` in a weighted, undirected graph. The result is returned modulo \( 10^9 + 7 \). |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +### **Key Components** |
| 9 | +1. **Class `Pair`:** |
| 10 | + - Represents a pair of values (distance and node index). |
| 11 | + - Used for storing the distance of a node from the source and the index of the node. |
| 12 | + |
| 13 | +2. **Adjacency List (`adj`):** |
| 14 | + - The graph is represented as an adjacency list where each node's neighbors and edge weights are stored. |
| 15 | + - The adjacency list is built using input data from the `roads` array. |
| 16 | + |
| 17 | +3. **Priority Queue (`pq`):** |
| 18 | + - Implements a min-heap to efficiently get the node with the smallest distance. |
| 19 | + - Helps in applying Dijkstra's algorithm. |
| 20 | + |
| 21 | +4. **Arrays `dist` and `ways`:** |
| 22 | + - `dist[i]`: Stores the shortest distance from the source node (node `0`) to node `i`. |
| 23 | + - `ways[i]`: Stores the number of distinct ways to reach node `i` with the shortest distance. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +### **Step-by-Step Process** |
| 28 | +1. **Input Parsing and Graph Construction:** |
| 29 | + - The graph is built using the `roads` array, where each road connects two nodes (`u` and `v`) with a specific weight (`w`). |
| 30 | + - Two directed edges are created for each road since the graph is undirected. |
| 31 | + |
| 32 | +2. **Initialization:** |
| 33 | + - Initialize the `dist` array with `Long.MAX_VALUE` to signify unreachable nodes. |
| 34 | + - Initialize the `ways` array to `0` (initially no paths to any node). |
| 35 | + - Set `dist[0] = 0` (distance from node `0` to itself is `0`). |
| 36 | + - Set `ways[0] = 1` (only one way to stay at the source). |
| 37 | + |
| 38 | +3. **Dijkstra's Algorithm:** |
| 39 | + - Use the priority queue to process the node with the smallest distance. |
| 40 | + - For each neighbor of the current node: |
| 41 | + - If a shorter path is found, update the distance and reset the number of ways. |
| 42 | + - If a path with the same shortest distance is found, add the number of ways to reach the current node to the neighbor's path count. |
| 43 | + |
| 44 | +4. **Return Result:** |
| 45 | + - The number of ways to reach the destination node (`n-1`) is returned modulo \( 10^9 + 7 \). |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +### **Code Explanation** |
| 50 | +#### 1. **Graph Representation** |
| 51 | +```java |
| 52 | +ArrayList<ArrayList<Pair>> adj = new ArrayList<>(); |
| 53 | +for(int i = 0; i < n; i++) { |
| 54 | + adj.add(new ArrayList<>()); |
| 55 | +} |
| 56 | +for(int i = 0; i < m; i++) { |
| 57 | + adj.get(roads[i][0]).add(new Pair(roads[i][2], roads[i][1])); |
| 58 | + adj.get(roads[i][1]).add(new Pair(roads[i][2], roads[i][0])); |
| 59 | +} |
| 60 | + |
| 61 | +Time Complexity: O((n+m)log(n)) |
| 62 | +The program processes all m roads to build the adjacency list. |
| 63 | +This operation takes O(m) time. |
| 64 | +Dijkstra’s Algorithm: |
| 65 | +Each node and its neighbors are processed through the priority queue. |
| 66 | +Popping a node from the priority queue takes O(log n) time. |
| 67 | +Over all m edges, the operations amount to O((n + m) log n) time. |
| 68 | +Overall Time Complexity: |
| 69 | +The dominant factor is Dijkstra’s algorithm, so the time complexity is O((n + m) log n). |
| 70 | + |
| 71 | +Space Complexity: O(n) |
| 72 | +The adjacency list stores all m edges, requiring O(m) space. |
| 73 | +The dist and ways arrays require O(n) space each. |
| 74 | +The priority queue stores up to O(n) nodes. |
| 75 | +Overall Space Complexity: |
| 76 | +The total space complexity is O(n + m) |
0 commit comments