-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindIfPathExistsInGraph.java
65 lines (58 loc) · 1.69 KB
/
FindIfPathExistsInGraph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// https://leetcode.com/problems/find-if-path-exists-in-graph/
public class FindIfPathExistsInGraph {
private final int n;
private final int[][] edges;
private final int source;
private final int destination;
public FindIfPathExistsInGraph(
int n,
int[][] edges,
int source,
int destination
) {
this.n = n;
this.edges = edges;
this.source = source;
this.destination = destination;
}
public boolean solution() {
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int[] edge : edges) {
int a = edge[0];
int b = edge[1];
List<Integer> lA = graph.getOrDefault(a, new ArrayList<>());
lA.add(b);
graph.put(a, lA);
List<Integer> lB = graph.getOrDefault(b, new ArrayList<>());
lB.add(a);
graph.put(b, lB);
}
boolean[] visited = new boolean[n];
return dfs(graph, visited, source, destination);
}
private boolean dfs(
Map<Integer, List<Integer>> graph,
boolean[] visited,
int source,
int destination
) {
if (visited[source]) {
return false;
}
if (source == destination) {
return true;
}
visited[source] = true;
for (int neighbour : graph.get(source)) {
if (dfs(graph, visited, neighbour, destination)) {
return true;
}
}
return false;
}
}