Bot Communication Network Reachability
Zerodha operates a sophisticated network of automated trading bots that communicate critical information and alerts. Efficient and reliable propagation of these alerts is paramount. This problem asks you to determine if an alert originating from a specific bot can reach another designated monitoring bot through the established communication channels.
Implement the function canBotReach which determines if an alert originating from a startBot can successfully propagate to a targetBot within the given communication network.
Your function will receive the following parameters:
numBots: An integer representing the total number of bots in the network, indexed from 0 to numBots - 1.connections: A 2D array of integers, where each inner array [u, v] signifies that bot u can send information directly to bot v. These connections are directed.startBot: An integer representing the bot from which the alert originates.targetBot: An integer representing the bot that needs to receive the alert.The function should return true if targetBot is reachable from startBot, and false otherwise.
0 to numBots - 1.startBot and targetBot will always be valid bot IDs within the 0 to numBots - 1 range.startBot can always reach startBot itself.Examples
Example 1
Input:
numBots = 4, connections = [[0,1],[1,2],[2,3]], startBot = 0, targetBot = 3
Output:
true
Note:
Bot 0 can send to 1, 1 to 2, and 2 to 3. So, 0 -> 1 -> 2 -> 3, alert reaches target.
Example 2
Input:
numBots = 4, connections = [[0,1],[2,3]], startBot = 0, targetBot = 3
Output:
false
Note:
Bot 0 can only reach bot 1. Bot 3 is in a separate component reachable from bot 2.
Example 3
Input:
numBots = 3, connections = [[0,1],[1,0]], startBot = 0, targetBot = 0
Output:
true
Note:
An alert from startBot can always reach startBot itself.
Example 4
Input:
numBots = 5, connections = [[0,1],[0,2],[1,3],[2,4],[3,4]], startBot = 0, targetBot = 4
Output:
true
Note:
Multiple paths exist: 0 -> 2 -> 4, or 0 -> 1 -> 3 -> 4. Target is reachable.
Constraints
1 <= numBots <= 10000 <= connections.length <= 50000 <= u, v < numBots for all [u, v] in connections.startBot and targetBot will always be valid bot indices.[u, v] exists, it does not imply [v, u] exists.[u, u]) in the input connections array.Loading editor...