Intuition
The problem requires finding the closest meeting node between two given nodes in a directed graph represented by an array of edges. The closest meeting node is the node with the smallest index that both nodes can reach, or the node that both nodes can reach with the smallest maximum distance from either node.
Approach
- Breadth-First Search (BFS) from Both Nodes: The approach involves performing BFS from both
node1 and node2 simultaneously. This helps in tracking the distances from each node to all other nodes they can reach.
- Tracking Visited Nodes and Distances: For each node, we keep track of the nodes visited and the distances from the starting node. This helps in identifying common nodes that both BFS traversals have encountered.
- Finding the Closest Meeting Node: Among the common nodes found, we select the one with the smallest maximum distance from either
node1 or node2. If there are multiple nodes with the same maximum distance, we choose the one with the smallest index.
Solution Code
/**
* @param {number[]} edges
* @param {number} node1
* @param {number} node2
* @return {number}
*/
var closestMeetingNode = function(edges, node1, node2) {
const n = edges.length;
const dist1 = new Array(n).fill(Infinity);
const dist2 = new Array(n).fill(Infinity);
// BFS from node1
let queue = [node1];
dist1[node1] = 0;
while (queue.length > 0) {
const current = queue.shift();
const neighbor = edges[current];
if (neighbor !== -1 && dist1[neighbor] === Infinity) {
dist1[neighbor] = dist1[current] + 1;
queue.push(neighbor);
}
}
// BFS from node2
queue = [node2];
dist2[node2] = 0;
while (queue.length > 0) {
const current = queue.shift();
const neighbor = edges[current];
if (neighbor !== -1 && dist2[neighbor] === Infinity) {
dist2[neighbor] = dist2[current] + 1;
queue.push(neighbor);
}
}
let minDistance = Infinity;
let result = -1;
for (let i = 0; i < n; i++) {
if (dist1[i] !== Infinity && dist2[i] !== Infinity) {
const maxDist = Math.max(dist1[i], dist2[i]);
if (maxDist < minDistance || (maxDist === minDistance && i < result)) {
minDistance = maxDist;
result = i;
}
}
}
return result;
};
Explanation
- Initialization: We initialize two distance arrays,
dist1 and dist2, to keep track of the distances from node1 and node2 to all other nodes, respectively. These arrays are initially filled with Infinity to represent that the nodes are initially unreachable.
- BFS from node1: We perform BFS starting from
node1, updating the distances in dist1 for each node encountered. The BFS ensures that we explore nodes level by level, which helps in accurately recording the shortest path distances.
- BFS from node2: Similarly, we perform BFS starting from
node2, updating the distances in dist2.
- Finding the Closest Meeting Node: We iterate through all nodes to find nodes that are reachable from both
node1 and node2 (i.e., nodes where both dist1 and dist2 are not Infinity). For these nodes, we calculate the maximum distance from either node1 or node2 and keep track of the node with the smallest maximum distance. If multiple nodes have the same maximum distance, we choose the one with the smallest index.
- Result: The result is the node with the smallest maximum distance, or
-1 if no such node exists.
This approach efficiently finds the closest meeting node by leveraging BFS to explore the graph from both starting nodes and then comparing the distances to identify the optimal meeting point.