Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 667b164

Browse files
Add solution for Find if Path Exists in Graph
1 parent fc87bf3 commit 667b164

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Algorithm exercises from LeetCode implemented in Java (v11) and JavaScript.
9494
- Shortest Path in Binary Matrix | [Problem](https://leetcode.com/problems/shortest-path-in-binary-matrix) | [Java Solution](src/javacode/solutions/ShortestPathInBinaryMatrix.java)
9595
- Shortest Path to Get Food | [Problem](https://leetcode.com/problems/shortest-path-to-get-food) | [Java Solution](src/javacode/solutions/ShortestPathToGetFood.java)
9696
- Shortest Distance from All Buildings | [Problem](https://leetcode.com/problems/shortest-distance-from-all-buildings) | [Java Solution](src/javacode/solutions/ShortestDistanceFromAllBuildings.java)
97+
- Find if Path Exists in Graph | [Problem](https://leetcode.com/problems/find-if-path-exists-in-graph) | [Java Solution](src/javacode/solutions/FindIfPathExistsInGraph.java)
9798

9899
### DFS
99100
- Path Sum | [Problem](https://leetcode.com/problems/path-sum) | [JS Solution](src/javascript/solutions/pathSum.js)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package javacode.solutions;
2+
3+
import java.util.*;
4+
5+
// [Problem] https://leetcode.com/problems/find-if-path-exists-in-graph
6+
class FindIfPathExistsInGraph {
7+
// BFS
8+
// O(v + e) time, O(v + e) space
9+
public boolean validPathBfs(int n, int[][] edges, int source, int destination) {
10+
HashSet<Integer>[] graph = new HashSet[n];
11+
for (int i = 0; i < n; i++) {
12+
graph[i] = new HashSet<>();
13+
}
14+
for (int[] edge : edges) {
15+
graph[edge[0]].add(edge[1]);
16+
graph[edge[1]].add(edge[0]);
17+
}
18+
boolean[] visited = new boolean[n];
19+
Queue<Integer> nodesToVisit = new ArrayDeque<>();
20+
nodesToVisit.add(source);
21+
visited[source] = true;
22+
while (!nodesToVisit.isEmpty()) {
23+
int nextNodes = nodesToVisit.size();
24+
for (int i = 0; i < nextNodes; i++) {
25+
int node = nodesToVisit.poll();
26+
if (node == destination) {
27+
return true;
28+
}
29+
Set<Integer> connectedNodes = graph[node];
30+
for (int connectedNode : connectedNodes) {
31+
if (!visited[connectedNode]) {
32+
nodesToVisit.add(connectedNode);
33+
visited[node] = true;
34+
}
35+
}
36+
}
37+
}
38+
return false;
39+
}
40+
41+
// DFS
42+
// O(v + e) time, O(v + e) space
43+
public boolean validPath(int n, int[][] edges, int source, int destination) {
44+
HashSet<Integer>[] graph = new HashSet[n];
45+
for (int i = 0; i < n; i++) {
46+
graph[i] = new HashSet<>();
47+
}
48+
for (int[] edge : edges) {
49+
graph[edge[0]].add(edge[1]);
50+
graph[edge[1]].add(edge[0]);
51+
}
52+
return dfs(graph, new boolean[n], source, destination);
53+
}
54+
55+
private boolean dfs(HashSet<Integer>[] graph, boolean[] visited, int source, int destination) {
56+
if (source == destination) {
57+
return true;
58+
}
59+
visited[source] = true;
60+
Set<Integer> connectedNodes = graph[source];
61+
for (int connectedNode : connectedNodes) {
62+
if (!visited[connectedNode] && dfs(graph, visited, connectedNode, destination)) {
63+
return true;
64+
}
65+
}
66+
return false;
67+
}
68+
69+
// Test
70+
public static void main(String[] args) {
71+
FindIfPathExistsInGraph solution = new FindIfPathExistsInGraph();
72+
73+
int[][] edges1 = {{0, 1}, {1, 2}, {2, 0}};
74+
boolean expectedOutput1 = true;
75+
boolean actualOutput1 = solution.validPath(3, edges1, 0, 2);
76+
System.out.println("Test 1 passed? " + (expectedOutput1 == actualOutput1));
77+
78+
int[][] edges2 = {{0, 1}, {0, 2}, {3, 5}, {5, 4}, {4, 3}};
79+
boolean expectedOutput2 = false;
80+
boolean actualOutput2 = solution.validPath(6, edges2, 0, 5);
81+
System.out.println("Test 2 passed? " + (expectedOutput2 == actualOutput2));
82+
}
83+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /