Leetcode 1971. Find if Path Exists in Graph
You are given an undirected graph with n nodes numbered from 0 to n - 1 and an array of edges where edges[i] = [a_i, b_i] indicates that there is an undirected edge between nodes a_i and b_i. Given two integer nodes start and end, return true if there is a path between start and end otherwise return false.
Example:
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], start = 0, end = 5
Output: false
edges array be empty?
start is the same as end?
true since the node is trivially reachable from itself.start to end.#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <iostream>
using namespace std;
bool dfs(int start, int end, unordered_map<int, vector<int>>& graph, unordered_set<int>& visited) {
if (start == end) return true;
visited.insert(start);
for (int neighbor : graph[start]) {
if (visited.find(neighbor) == visited.end()) {
if (dfs(neighbor, end, graph, visited)) {
return true;
}
}
}
return false;
}
bool validPath(int n, vector<vector<int>>& edges, int start, int end) {
if (start == end) return true;
unordered_map<int, vector<int>> graph;
// Construct the graph
for (const auto& edge : edges) {
graph[edge[0]].push_back(edge[1]);
graph[edge[1]].push_back(edge[0]);
}
unordered_set<int> visited;
return dfs(start, end, graph, visited);
}
int main() {
int n = 6;
vector<vector<int>> edges = \{\{0,1},{0,2},{3,5},{5,4},{4,3}};
int start = 0, end = 5;
bool result = validPath(n, edges, start, end);
cout << (result ? "true" : "false") << endl;
return 0;
}
Thus, the overall time complexity is O(V + E). This is efficient for typical constraints in graph problems where V and E are large.
start node. If we reach the end node, we return true.end, we return false.start is equal to end, we immediately return true.This approach ensures that we efficiently determine if a path exists between two nodes in an undirected graph.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?