-
Notifications
You must be signed in to change notification settings - Fork 0
/
1971. Find if Path Exists in Graph
130 lines (108 loc) · 3.55 KB
/
1971. Find if Path Exists in Graph
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a
2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by
at most one edge, and no vertex has an edge to itself.
You want to determine if there is a valid path that exists from vertex source to vertex destination.
Given edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.
ex-1
Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
Output: true
Explanation: There are two paths from vertex 0 to vertex 2:
- 0 → 1 → 2
- 0 → 2
ex-2:
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
Output: false
Explanation: There is no path from vertex 0 to vertex 5.
Constraints:
1 <= n <= 2 * 105
0 <= edges.length <= 2 * 105
edges[i].length == 2
0 <= ui, vi <= n - 1
ui != vi
0 <= source, destination <= n - 1
There are no duplicate edges.
There are no self edges.
//code
//approach-1(using dfs )
class Solution {
public boolean validPath(int n, int[][] edges, int source, int destination) {
ArrayList<ArrayList<Integer>> adj=new ArrayList<>();
for(int i=0;i<=n;i++) {
adj.add(new ArrayList<Integer>());
}
for(int i=0;i<edges.length;i++){
int u=edges[i][0];
int v=edges[i][1];
addEdgeList(adj,u,v);
}
boolean visited[]=new boolean[n+1];
for(int i=0;i<=n;i++) {
visited[i]=false;
}
return check(adj,source,destination,visited);
}
boolean check(ArrayList<ArrayList<Integer>> adj,int source,int dest,boolean vis[]){
if(source==dest)
return true;
if(vis[source]==true)
return false;
vis[source]=true;
//explore all posible path from sourse
for(int i=0;i<adj.get(source).size();i++) {
int cur=adj.get(source).get(i);
if(check(adj,cur,dest,vis)==true)
return true;
}
return false;
}
void addEdgeList(ArrayList<ArrayList<Integer>> adj,int source,int dest) {
//bidirectional
adj.get(source).add(dest);
adj.get(dest).add(source);
}
}
//using BFS
class Solution {
public boolean validPath(int n, int[][] edges, int source, int destination) {
ArrayList<ArrayList<Integer>> adj=new ArrayList<>();
for(int i=0;i<=n;i++) {
adj.add(new ArrayList<Integer>());
}
for(int i=0;i<edges.length;i++){
int u=edges[i][0];
int v=edges[i][1];
addEdgeList(adj,u,v);
}
boolean visited[]=new boolean[n+1];
for(int i=0;i<=n;i++) {
visited[i]=false;
}
return bfs(adj,source,destination,visited);
}
boolean bfs(ArrayList<ArrayList<Integer>> adj,int source,int dest,boolean visited[]){
Queue<Integer> q=new LinkedList<>();
visited[source]=true;
if(source==dest)
return true;
q.add(source);
while(!q.isEmpty()) {
int curr=q.remove();
for(int i=0;i<adj.get(curr).size();i++) {
int neighbor=adj.get(curr).get(i);
if(visited[neighbor]==false) {
visited[neighbor]=true;
//dist[neighbor]=dist[curr]+1;
q.add(neighbor);
if(neighbor==dest)
return true;
}
}
}
return false;
}
void addEdgeList(ArrayList<ArrayList<Integer>> adj,int source,int dest) {
//bidirectional
adj.get(source).add(dest);
adj.get(dest).add(source);
}
}