-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_24.java
More file actions
177 lines (158 loc) · 4.56 KB
/
Copy pathDay_24.java
File metadata and controls
177 lines (158 loc) · 4.56 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Directed Graph Cycle
class Solution {
public boolean dfs(int node,boolean[] vis,boolean[] pathVis,List<List<Integer>> adj) {
vis[node] = true;
pathVis[node] = true;
for(var it : adj.get(node)){
if(pathVis[it]) return true;
else if(!vis[it]){
if(dfs(it,vis,pathVis,adj)) return true;
}
}
pathVis[node] = false;
return false;
}
public boolean isCyclic(int V, int[][] edges) {
int n = edges.length;
if(n == 0) return false;
int m = edges[0].length;
List<List<Integer>> adj = new ArrayList<>();
for(int i=0;i<V;i++){
adj.add(new ArrayList<>());
}
for(var it : edges){
adj.get(it[0]).add(it[1]);
}
boolean vis[] = new boolean[V];
boolean pathVis[] = new boolean[V];
for(int i=0;i<V;i++){
if(dfs(i,vis,pathVis,adj)) return true;
}
return false;
}
}
// Find Eventual Safe States
class Solution {
public boolean dfs(int node,boolean[] vis,boolean[] pathVis,boolean[] check,int[][] adj) {
vis[node] = true;
pathVis[node] = true;
for(var it : adj[node]){
if(pathVis[it]) return true;
else if(!vis[it]){
if(dfs(it,vis,pathVis,check,adj)) return true;
}
}
pathVis[node] = false;
check[node] = true;
return false;
}
public List<Integer> eventualSafeNodes(int[][] graph) {
int V = graph.length;
List<Integer> ans = new ArrayList<>();
boolean[] vis = new boolean[V];
boolean[] pathVis = new boolean[V];
boolean[] check = new boolean[V];
for(int i=0;i<V;i++){
if(!vis[i]){
dfs(i,vis,pathVis,check,graph);
}
}
for(int i=0;i<V;i++){
if(check[i]){
ans.add(i);
}
}
return ans;
}
}
// Is Graph Bipartite?
class Solution {
public boolean dfs(int node,int[] color,int[][] adj) {
for(var it : adj[node]){
if(color[it] == color[node]) return false;
else if(color[it] == -1){
color[it] = 1 - color[node];
if(!dfs(it,color,adj)) return false;
}
}
return true;
}
public boolean isBipartite(int[][] graph) {
int V = graph.length;
int[] color = new int[V];
Arrays.fill(color,-1);
for(int i=0;i<V;i++){
if(color[i] == -1){
color[i] = 0;
if(!dfs(i,color,graph)) return false;
}
}
return true;
}
}
// Topological Sort
class Solution {
public void dfs(int node,boolean[] vis,Stack<Integer> st,List<List<Integer>> adj){
vis[node] = true;
for(var it : adj.get(node)){
if(!vis[it]){
dfs(it,vis,st,adj);
}
}
st.push(node);
}
public ArrayList<Integer> topoSort(int V, int[][] edges) {
ArrayList<Integer> ans = new ArrayList<>();
int n = edges.length;
if(n == 0) return ans;
int m = edges[0].length;
List<List<Integer>> adj = new ArrayList<>();
for(int i=0;i<V;i++){
adj.add(new ArrayList<>());
}
for(var it : edges){
adj.get(it[0]).add(it[1]);
}
boolean[] vis = new boolean[V];
Stack<Integer> st = new Stack<>();
for(int i=0;i<V;i++){
if(!vis[i]){
dfs(i,vis,st,adj);
}
}
while(!st.isEmpty()){
ans.add(st.pop());
}
return ans;
}
}
// Course Scheduleclass Solution {
public boolean canFinish(int numc, int[][] pr) {
int n = pr.length;
if(n == 0) return true;
List<List<Integer>> adj = new ArrayList<>();
for(int i=0;i<numc;i++){
adj.add(new ArrayList<>());
}
int[] indeg = new int[numc];
for(var it : pr){
adj.get(it[1]).add(it[0]);
indeg[it[0]]++;
}
Queue<Integer> q = new LinkedList<>();
for(int i=0;i<numc;i++){
if(indeg[i] == 0) q.offer(i);
}
int cnt = 0;
while(!q.isEmpty()){
int node = q.poll();
cnt++;
for(var it : adj.get(node)){
indeg[it]--;
if(indeg[it] == 0) q.offer(it);
}
}
if(cnt == numc) return true;
return false;
}
}