Advertisement

Reachable Nodes In Subdivided Graph - LeetCode 882 Solution

Reachable Nodes In Subdivided Graph - Complete Solution Guide

Reachable Nodes In Subdivided Graph is LeetCode problem 882, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an undirected graph (the "original graph" ) with n nodes labeled from 0 to n - 1 . You decide to subdivide each edge in the graph into a chain of nodes, with the number of new nodes varying between each edge. The graph is given as a 2D array of edges where edges[i] = [u i , v i , cnt i ] indicates that there is an edge between nodes u i and v i in the original graph, and cnt i is the total number of new nodes that you will subdivide the edge into. Note that cnt i == 0 means you wil

Detailed Explanation

The problem asks us to find the number of reachable nodes from node 0 in a subdivided graph. We are given the original graph as a list of edges, where each edge is represented by [u, v, cnt]. This means there's an edge between nodes 'u' and 'v' that will be subdivided into 'cnt' new nodes and 'cnt+1' new edges. After subdividing the graph, we need to determine how many nodes (both original and new) are reachable from node 0 within a maximum number of moves, 'maxMoves'. The output is the total count of reachable nodes.

Solution Approach

The solution uses Dijkstra's algorithm to find the shortest distances from node 0 to all other original nodes in the graph. After computing these shortest distances, it iterates through the original edges and calculates the number of subdivision nodes reachable from each endpoint of the edge within the 'maxMoves' limit. The final count is the sum of reachable original nodes and reachable subdivision nodes.

Step-by-Step Algorithm

  1. Step 1: Build an adjacency list representation of the graph. The weight of an edge (u, v) is cnt + 1, where cnt is the number of subdivision nodes between u and v.
  2. Step 2: Initialize a distance map/array 'dist' to store the shortest distances from node 0 to all other nodes. Set all distances to infinity initially, except for dist[0], which is set to 0.
  3. Step 3: Use a priority queue (min-heap) to implement Dijkstra's algorithm. The priority queue stores nodes ordered by their distances from the source node.
  4. Step 4: While the priority queue is not empty, extract the node 'u' with the smallest distance from the queue.
  5. Step 5: For each neighbor 'v' of node 'u', calculate the distance from node 0 to 'v' through 'u'. If this distance is shorter than the current distance to 'v', update dist[v] and add 'v' to the priority queue.
  6. Step 6: After Dijkstra's algorithm completes, count the number of original nodes 'i' for which dist[i] <= maxMoves.
  7. Step 7: Iterate through the original edges and for each edge (u, v, cnt), calculate the number of subdivision nodes reachable from 'u' and 'v'. The number of reachable nodes is min(cnt, reach_from_u + reach_from_v), where reach_from_u = max(0, maxMoves - dist[u]) and reach_from_v = max(0, maxMoves - dist[v]).
  8. Step 8: Add the number of reachable subdivision nodes to the result and return the final count.

Key Insights

  • Insight 1: The problem is essentially a shortest path problem on a modified graph. We need to find the shortest distance from node 0 to every other node.
  • Insight 2: Dijkstra's algorithm is well-suited for finding the shortest paths from a single source node to all other nodes in a graph with non-negative edge weights.
  • Insight 3: After finding the shortest distances to the original nodes, we need to carefully count the reachable subdivision nodes by considering the remaining moves from the two endpoint nodes of each edge.

Complexity Analysis

Time Complexity: O(ElogN)

Space Complexity: O(N+E)

Topics

This problem involves: Graph, Heap (Priority Queue), Shortest Path.

Companies

Asked at: PhonePe.