-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudyOfGraphs.java
More file actions
360 lines (306 loc) · 10.7 KB
/
StudyOfGraphs.java
File metadata and controls
360 lines (306 loc) · 10.7 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import java.util.ArrayList;
import java.util.PriorityQueue;
public class StudyOfGraphs {
static class Edge{
int src;
int dest;
int weight;
public Edge(int s, int d){
this.src = s;
this.dest = d;
}
public Edge(int s, int d, int wt){
this.src = s;
this.dest = d;
this.weight = wt;
}
}
/*
for dijkistra:
public static class Pair implements Comparable<Pair>{
int node;
int dist;
Pair(int node ,int dist){
this.node = node;
this.dist = dist;
}
@Override
public int compareTo(Pair p2) {
return this.dist - p2.dist; //comparioson on basis of distance //ascending order
}
}
*/
// study of graphs
/*
public static void createGraph(ArrayList<Edge> graph[]){
for(int i=0; i<graph.length; i++){
graph[i] = new ArrayList<Edge>(); //new arraylist at every index of array
}
graph[0].add(new Edge(0,2));
graph[1].add(new Edge(1,0));
graph[2].add(new Edge(2,3));
graph[3].add(new Edge(3,0));
}
public static void createGraphforDFS(ArrayList<DFS.Edge> graph[]) {
for (int i = 0; i < graph.length; i++) {
graph[i] = new ArrayList<DFS.Edge>(); //new arraylist at every index of array
}
graph[0].add(new DFS.Edge(0, 1));
graph[0].add(new DFS.Edge(0, 2));
graph[1].add(new DFS.Edge(1, 0));
graph[1].add(new DFS.Edge(1, 3));
graph[2].add(new DFS.Edge(2, 0));
graph[2].add(new DFS.Edge(2, 4));
graph[3].add(new DFS.Edge(3, 1));
graph[3].add(new DFS.Edge(3, 4));
graph[3].add(new DFS.Edge(3, 5));
graph[4].add(new DFS.Edge(4, 2));
graph[4].add(new DFS.Edge(4, 3));
graph[4].add(new DFS.Edge(4, 3));
graph[5].add(new DFS.Edge(5, 3));
graph[5].add(new DFS.Edge(5, 4));
graph[5].add(new DFS.Edge(5, 6));
graph[6].add(new DFS.Edge(6,5));
}
public static boolean isCyclicDirected(ArrayList<Edge>[] graph, boolean vis[], int curr, boolean rec[]){
vis[curr] = true;
rec[curr] = true;
for(int i=0;i<graph[curr].size(); i++){
Edge e = graph[curr].get(i);
if(rec[e.dest]){
return true; //cycle
} else if(!vis[e.dest]){
if(isCyclicDirected(graph , vis , e.dest , rec)){
return true;
}
}
}
rec[curr] = false;
return false;
}
public static void dfs(ArrayList<DFS.Edge> graph[], boolean vis[] , int curr){
System.out.print(curr+" "); //we initailize it on first hand so we use it here
vis[curr] = true;
for(int i=0;i<graph[curr].size();i++){
DFS.Edge e = graph[curr].get(i);
if (vis[e.dest] == false) { //if neighbour is not visited
dfs(graph ,vis, e.dest ); //call dfs again for neighbour considering it as a new start
}
}
}
public static void printAllPaths(ArrayList<DFS.Edge> graph[], boolean[] viz, int curr , String path, int tar ) {
//time complexity O(V^V)
//base case
if (curr == tar) {
System.out.println(path);
return;
}
for (int i = 0; i < graph[curr].size(); i++) {
DFS.Edge e = graph[curr].get(i);
if (!viz[e.dest]) { //destintion is unvisited
viz[curr] = true;
printAllPaths(graph, viz, e.dest,path + e.dest, tar); //update curr and path value and then recall
viz[curr] = false; //make unvisited again
}
}
}
public static void bfs(ArrayList<Edge> graph[] , int V , boolean[] vis , int start){ //time comple O(V+E)
Queue<Integer> q = new LinkedList<>();
q.add(start); //starting point;
while(!q.isEmpty()){
int curr = q.remove();
if(vis[curr] == false) {
System.out.print(curr + " "); //step 1
vis[curr] = true; //step 2
//step 3 below
for(int i=0;i<graph[curr].size();i++){
Edge e= graph[curr].get(i);
q.add(e.dest);
}
}
}
}
public static void dijkstra(ArrayList<Edge> graph[] , int src , int V){ //time compextiy = O(E+ElogV) elogv is becuz of priority queue
PriorityQueue<Pair> pq = new PriorityQueue<>();
int dist[] = new int[V];
for(int i=0;i<V;i++){
if(i!=src){
dist[i] = Integer.MAX_VALUE; //code for infinity
}
}
boolean vis[] = new boolean[V];
pq.add(new Pair(0,0));
while(!pq.isEmpty()){
Pair curr = pq.remove(); //shortest distance
if(!vis[curr.node]){
vis[curr.node] = true;
for(int i=0; i<graph[curr.node].size();i++){
Edge e = graph[curr.node].get(i);
int u = e.src;
int v = e.dest;
if(dist[u] + e.weight < dist[v]){
dist[v] = dist[u] + e.weight; //relaxtion
pq.add(new Pair(v, dist[v]));
}
}
}
}
for(int i=0;i<V;i++){
System.out.print(dist[i] + " ");
}
System.out.println();
}
public static void createGraphforDijkistra(ArrayList<dijkistra.Edge> graph[]){
for(int i=0; i<graph.length; i++){
graph[i] = new ArrayList<dijkistra.Edge>(); //new arraylist at every index of array
}
graph[0].add(new dijkistra.Edge(0,2,2));
graph[0].add(new dijkistra.Edge(0,1,4));
graph[1].add(new dijkistra.Edge(1,3,7));
graph[1].add(new dijkistra.Edge(1,2,1));
graph[2].add(new dijkistra.Edge(2,4,3));
graph[3].add(new dijkistra.Edge(3,5,1));
graph[4].add(new dijkistra.Edge(4,3,2));
graph[4].add(new dijkistra.Edge(4,5,5));
}
*/
public static void main(String[] args) {
/* study of graphs
//considering a new example with 4 vvertices , so making graph accordingly
//Adjacency List O(1) = constant time complexity
int V = 4; //no. of vertices
ArrayList<Edge> graph[] = new ArrayList[V];
createGraph(graph);
boolean vis[] = new boolean[V];
boolean rec[] = new boolean[V];
int[] o={1,2,3,4,6,7,8};
for(int i=0 ; i<V ; i++) {
boolean isCycle = isCyclicDirected(graph,vis, 0, rec);
if (!vis[i]) {
if(isCycle) {
System.out.println(isCycle);
break;
};
}
//time complexity O(e+v)
}
*/
/*
for dfs:
public static void main(String[] args) {
/*
* 1 --- 3
* / | \
* 0 | 5 -- 6
* \ |/
* 2 ---- 4
*
int V = 7; //no. of vertices
ArrayList<DFS.Edge> graph[] = new ArrayList[V];
createGraph(graph);
boolean viz[] = new boolean[V];
// for(int i=0;i<V ; i++){
// if(viz[i] == false){
// dfs(graph,viz,0);
// }
// }
int src = 0 , tar = 5;
printAllPaths(graph , viz , src , "0" , tar);
}
*/
/*
for bfs:
public static void main(String[] args) {
/*
* 1 --- 3
* / | \
* 0 | 5 -- 6
* \ |/
* 2 ---- 4
*
int V = 7; //no. of vertices
ArrayList<BFS.Edge> graph[] = new ArrayList[V];
createGraph(graph);
boolean vis[] = new boolean[V];
for(int i=0;i< vis.length;i++){
if(!vis[i]){ //which will be obviously false for thee first one
bfs(graph ,V ,vis,i);
}
}
}
*/
/*
for dijkistra:
* public static void main(String[] args) {
int V = 6;
ArrayList<Edge> graph[] = new ArrayList[V];
createGraph(graph);
dijkstra(graph , 0 , V);
}
* */
}
public static class primsalgo {
static class Edge{
int src;
int dest;
int weight;
public Edge(int s, int d, int wt){
this.src = s;
this.dest = d;
this.weight = wt;
}
}
public static void createGraph(ArrayList<Edge> graph[]){
for(int i=0;i<graph.length;i++){
graph[i] = new ArrayList<>();
}
graph[0].add(new Edge(0,1,10));
graph[0].add(new Edge(0,2,15));
graph[0].add(new Edge(0,3,30));
graph[1].add(new Edge(1,0,10));
graph[1].add(new Edge(1,3,40));
graph[2].add(new Edge(2,0,15));
graph[2].add(new Edge(2,3,50));
graph[3].add(new Edge(3,1,40));
graph[3].add(new Edge(3,2,50));
}
public static class Pair implements Comparable<Pair>{
int node;
int cost;
public Pair(int node, int cost){
this.node = node;
this.cost = cost;
}
@Override
public int compareTo(Pair p2){
return this.cost - p2.cost;
}
}
public static void primsALgo(ArrayList<Edge> graph[], int V){
PriorityQueue<Pair> pq = new PriorityQueue<Pair>(); //non-mst set (node, cost)
boolean vis[] = new boolean[V]; //mst set
pq.add(new Pair(0,0));
int mstCost = 0;
while(!pq.isEmpty()){
Pair curr = pq.remove() ; //auto gets one with least value
if (!vis[curr.node]) {
vis[curr.node] = true;
mstCost = mstCost + curr.cost ;
for(int i=0; i<graph[curr.node].size(); i++){
Edge e = graph[curr.node].get(i);
if(!vis[e.dest]){
pq.add(new Pair(e.dest , e.weight));
}
}
}
}
System.out.println("minimum cost of mst is " + mstCost);
}
public static void main(String[] args) {
int V=4;
ArrayList<Edge> graph[] = new ArrayList[V];
createGraph(graph);
primsALgo(graph , V);
}
}
}