Find Closest Node to Given Two Nodes - Complete Solution Guide
Find Closest Node to Given Two Nodes is LeetCode problem 2359, a Medium 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 a directed graph of n nodes numbered from 0 to n - 1 , where each node has at most one outgoing edge. The graph is represented with a given 0-indexed array edges of size n , indicating that there is a directed edge from node i to node edges[i] . If there is no outgoing edge from i , then edges[i] == -1 . You are also given two integers node1 and node2 . Return the index of the node that can be reached from both node1 and node2 , such that the maximum between the distance from node1
Detailed Explanation
The problem asks us to find the node that is reachable from both `node1` and `node2` in a directed graph, such that the maximum of the distances from `node1` and `node2` to that node is minimized. The graph is represented by an array `edges`, where `edges[i]` indicates the node that node `i` points to. If `edges[i]` is -1, it means node `i` has no outgoing edge. We need to return the index of the node that satisfies the minimum maximum distance condition. If multiple nodes satisfy this condition, return the one with the smallest index. If no such node exists (no common reachable node), return -1.
Solution Approach
The solution calculates the distances from `node1` and `node2` to all other nodes separately using the `get_distances` function (or equivalent in other languages). Then, it iterates through all the nodes and checks if the node is reachable from both `node1` and `node2` (i.e., both `dist1[i]` and `dist2[i]` are not -1). If a node is reachable from both, the maximum of the two distances is calculated. The solution maintains the minimum of these maximum distances and the corresponding node index. Finally, it returns the node index with the minimum maximum distance. If no such node exists, it returns -1.
Step-by-Step Algorithm
- Step 1: Create a function `get_distances(start_node)` to calculate the distances from a given `start_node` to all other nodes in the graph.
- Step 2: In `get_distances`, initialize a `distances` array of size `n` with all elements set to -1. This array stores the distances from `start_node` to each node.
- Step 3: Start from the `start_node` and iterate through the graph, updating the `distances` array and incrementing the distance at each step.
- Step 4: Detect cycles by checking if the current node has already been visited (i.e., its distance in the `distances` array is not -1). If a cycle is detected, stop the iteration.
- Step 5: Call `get_distances` twice, once for `node1` and once for `node2`, to get the `dist1` and `dist2` arrays, respectively.
- Step 6: Initialize `min_max_dist` to infinity and `result_node` to -1.
- Step 7: Iterate through all the nodes in the graph.
- Step 8: For each node `i`, check if it is reachable from both `node1` and `node2` (i.e., `dist1[i] != -1` and `dist2[i] != -1`).
- Step 9: If the node is reachable from both, calculate the maximum of the two distances: `max_dist = max(dist1[i], dist2[i])`.
- Step 10: If `max_dist` is less than `min_max_dist`, update `min_max_dist` to `max_dist` and `result_node` to `i`.
- Step 11: After iterating through all the nodes, return `result_node`.
Key Insights
- Insight 1: We need to calculate the distances from both `node1` and `node2` to all other nodes in the graph. This can be efficiently done using a simple iterative approach, as each node has at most one outgoing edge.
- Insight 2: Since cycles are allowed, we need to detect them when calculating distances to avoid infinite loops. This is done by checking if a node has already been visited (distance is not -1) before visiting it again.
- Insight 3: The optimal solution will be a node `i` where `dist1[i] != -1` and `dist2[i] != -1`, implying it's reachable from both `node1` and `node2`. We need to minimize `max(dist1[i], dist2[i])`.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Depth-First Search, Graph.
Companies
Asked at: Juspay.