API Endpoint Reachability Check
In Postman, complex workflows often involve chains of API requests where one request's success or output is a prerequisite for another. Consider a simplified model where we represent these dependencies as a directed graph. Your task is to determine if a specific API endpoint can be reached (i.e., executed) starting from another given endpoint, following the defined dependencies. This is crucial for understanding workflow feasibility and potential execution paths within a collection.
Implement a function canReachEndpoint that takes the total number of endpoints, a list of dependencies, a starting endpoint, and a target endpoint. The function should return true if the targetEndpoint is reachable from the startEndpoint by traversing the dependencies, and false otherwise.
numEndpoints: An integer representing the total number of API endpoints, indexed from 0 to numEndpoints - 1.dependencies: An array of [u, v] pairs, where each pair indicates a directed dependency from endpoint u to endpoint v. This means u can trigger or depends on v.startEndpoint: An integer representing the initial endpoint from which to begin traversal.targetEndpoint: An integer representing the endpoint we are trying to reach.Returns:
A boolean value: true if targetEndpoint is reachable from startEndpoint, false otherwise.
[u, v] means u can lead to v, not v to u.Examples
Example 1
Input:
numEndpoints = 4, dependencies = [[0,1],[1,2],[2,3]], startEndpoint = 0, targetEndpoint = 3
Output:
true
Note:
Path: 0 -> 1 -> 2 -> 3. Endpoint 3 is reachable from endpoint 0.
Example 2
Input:
numEndpoints = 4, dependencies = [[0,1],[1,2]], startEndpoint = 0, targetEndpoint = 3
Output:
false
Note:
Endpoint 3 is not reachable from endpoint 0. There is no path.
Example 3
Input:
numEndpoints = 3, dependencies = [[0,1],[1,0]], startEndpoint = 0, targetEndpoint = 0
Output:
true
Note:
An endpoint is always considered reachable from itself. Also, 0 -> 1 -> 0 is a path.
Constraints
1 <= numEndpoints <= 10000 <= dependencies.length <= numEndpoints * (numEndpoints - 1)0 <= u, v < numEndpoints for all [u, v] in dependencies.0 <= startEndpoint, targetEndpoint < numEndpointsLoading editor...