Skip to content

Commit 79a2adb

Browse files
solves find path if exists in graph
1 parent a45ece0 commit 79a2adb

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,10 @@
469469
| 1941 | [Check if All Characters Have Equal Number of Occurrences](https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences) | [![Java](assets/java.png)](src/CheckIfAllCharactersHaveEqualNumberOfOccurrences.java) | |
470470
| 1945 | [Sum of Digits of String After Convert](https://leetcode.com/problems/sum-of-digits-of-string-after-convert) | [![Java](assets/java.png)](src/SumOfDigitsOfStringAfterConvert.java) | |
471471
| 1952 | [Three Divisors](https://leetcode.com/problems/three-divisors) | [![Java](assets/java.png)](src/ThreeDivisors.java) | |
472-
| 1957 | [Delete Characters to Make Fancy String](https://leetcode.com/problems/delete-characters-to-make-fancy-string) | [![Java](assets/java.png)](src/CheckIfStringIsAPrefixOfArray.java) | |
473-
| 1961 | [Check If String Is a Prefix of Array](https://leetcode.com/problems/check-if-string-is-a-prefix-of-array) | [![Java](assets/java.png)](src/NumberOfStringsThatAppearAsSubstringsInWord.java) | |
474-
| 1967 | [Number of Strings That Appear as Substrings in Word](https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word) | | |
475-
| 1971 | [Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph) | | |
472+
| 1957 | [Delete Characters to Make Fancy String](https://leetcode.com/problems/delete-characters-to-make-fancy-string) | [![Java](assets/java.png)](src/DeleteCharactersToMakeFancyString.java) | |
473+
| 1961 | [Check If String Is a Prefix of Array](https://leetcode.com/problems/check-if-string-is-a-prefix-of-array) | [![Java](assets/java.png)](src/CheckIfStringIsAPrefixOfArray.java) | |
474+
| 1967 | [Number of Strings That Appear as Substrings in Word](https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word) | [![Java](assets/java.png)](src/NumberOfStringsThatAppearAsSubstringsInWord.java) | |
475+
| 1971 | [Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph) | [![Java](assets/java.png)](src/FindIfPathExistsInGraph.java) | |
476476
| 1974 | [Minimum Time to Type Word Using Special Typewriter](https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter) | | |
477477
| 1979 | [Find Greatest Common Divisor of Array](https://leetcode.com/problems/find-greatest-common-divisor-of-array) | | |
478478
| 1984 | [Minimum Difference Between Highest and Lowest of K Scores](https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores) | | |

src/FindIfPathExistsInGraph.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// https://leetcode.com/problems/find-if-path-exists-in-graph
2+
// T: O(V + E)
3+
// S: O(V + E)
4+
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
public class FindIfPathExistsInGraph {
11+
public boolean validPath(int n, int[][] edges, int start, int end) {
12+
UnDirectedGraph graph = UnDirectedGraph.from(n, edges);
13+
return graph.pathFrom(start, end);
14+
}
15+
16+
private static final class UnDirectedGraph {
17+
private final Map<Integer, Vertex> vertices = new HashMap<>();
18+
19+
public static UnDirectedGraph from(int vertices, int[][] edges) {
20+
UnDirectedGraph graph = new UnDirectedGraph(vertices);
21+
for (int[] edge : edges) {
22+
graph.vertices.get(edge[0]).addEdge(edge[1]);
23+
graph.vertices.get(edge[1]).addEdge(edge[0]);
24+
}
25+
return graph;
26+
}
27+
28+
private UnDirectedGraph(int vertices) {
29+
for (int i = 0 ; i < vertices ; i++) {
30+
this.vertices.put(i, new Vertex(i));
31+
}
32+
}
33+
34+
public boolean pathFrom(int start, int end) {
35+
return pathTo(vertices.get(start), end, new HashSet<>());
36+
}
37+
38+
private boolean pathTo(Vertex from, int to, Set<Integer> visited) {
39+
if (visited.contains(from.value)) return false;
40+
if (from.value == to) return true;
41+
visited.add(from.value);
42+
for (int edge : from.edges) {
43+
if (pathTo(vertices.get(edge), to, visited)) {
44+
return true;
45+
}
46+
}
47+
return false;
48+
}
49+
50+
private static final class Vertex {
51+
private final int value;
52+
private final Set<Integer> edges = new HashSet<>();
53+
54+
Vertex(int value) {
55+
this.value = value;
56+
}
57+
58+
public void addEdge(int to) {
59+
edges.add(to);
60+
}
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)