-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGRAPHS.java
More file actions
207 lines (164 loc) · 5.86 KB
/
GRAPHS.java
File metadata and controls
207 lines (164 loc) · 5.86 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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class GRAPHS {
static class Edge {
int src ;
int des ;
int weight ;
public Edge(int s , int d , int w){
this.des = d ;
this.src = s ;
this.weight = w;
}
}
public static void createGraph(ArrayList <Edge> graph []){
for (int i = 0 ; i <graph.length ; i++){
graph[i] = new ArrayList<Edge>();
}
graph[0].add(new Edge(0, 2 , 2));
graph[1].add(new Edge(1, 2 , 0));
graph[1].add(new Edge(1, 3 , 10));
graph[2].add(new Edge(2, 0 , 2));
graph[2].add(new Edge(2, 1 , 10));
graph[2].add(new Edge(2, 3 , -1));
graph[3].add(new Edge(3, 1 , 0 ));
graph[3].add(new Edge(3, 2, -1));
}
public static void BFS (ArrayList <Edge> graph[] , int V , boolean [] visited , int start){
Queue <Integer> q = new LinkedList<>();
q.add(start);
while (!q.isEmpty()) {
int curr = q.remove();
if (visited[curr]==false){
System.out.print(curr + " ");
visited[curr] = true ;
for (int index = 0; index < graph[curr].size(); index++) {
Edge e = graph[curr].get(index);
q.add(e.des);
}
}
}
}
public static void DFS (ArrayList <Edge> graph[], int curr ,boolean visited[]){
System.out.print(curr + " ");
visited[curr] = true ;
for (int i = 0; i < graph[curr].size(); i++) {
Edge e = graph[curr].get(i);
if (visited[e.des] == false ){
DFS(graph, e.des, visited);
}
}
}
public static void AllPath(ArrayList < Edge> graph[], int curr ,boolean visited[] , String path , int target){
if (curr == target){
System.out.println(path);
return;
}
for (int i = 0; i < graph[curr].size(); i++) {
Edge e = graph[curr].get(i);
if (!visited[e.des]){
visited[curr] = true;
AllPath(graph, e.des, visited, path + e.des, target);
visited[curr]= false ;
}
}
}
public static boolean CycleDetection (ArrayList <Edge> graph [] , int curr ,boolean visited[] , boolean rec[] ){
visited[curr] = true;
rec[curr] = true ;
for (int i = 0; i < graph[curr].size(); i++) {
Edge e = graph[curr].get(i);
if (rec[e.des]){
return true ;
}
else if (!visited[e.des]){
if (CycleDetection(graph, e.des, visited, rec)){
return true ;
}
}
}
rec[curr]= false ;
return false ;
}
public static boolean isCycleDetection (ArrayList <Edge> graph [] , int curr ,boolean visited[] , int parent ){
visited[curr] = true;
for (int i = 0; i < graph[curr].size(); i++) {
Edge e = graph[curr].get(i);
if (visited[curr] && e.des != parent ){
return true ;
}
else if (!visited[e.des]){
if (isCycleDetection(graph, e.des, visited, parent)){
return true ;
}
}
}
return false ;
}
public static void topSort (ArrayList < Edge > graph [] , int v){
boolean visited [] = new boolean[v] ;
Stack <Integer > stack = new Stack<>() ;
for (int i = 0; i < v; i++) {
if (!visited[i]){
topSortUtil(graph, i, visited, stack);
}
}
while (!stack.isEmpty()){
System.out.print(stack.pop() + " ");
}
}
private static void topSortUtil (ArrayList <Edge> graph [] ,int curr , boolean visited[], Stack<Integer > stack ){
visited[curr] = true ;
for (int i = 0; i < graph[curr].size(); i++) {
Edge e =graph[curr].get(i);
if(!visited[e.des]){
topSortUtil(graph, e.des, visited, stack);
}
}
stack.push(curr);
}
public static void main(String[] args) {
/*int V = 4 ;
ArrayList <Edge> graph [] = new ArrayList[V];
createGraph(graph);
// 2's neighbours
for (int i = 0; i < graph[2].size() ; i++) {
Edge e = graph[2].get(i);
System.out.print(e.des + " ");
}*/
/* int V = 4 ;
ArrayList <Edge> graph [] = new ArrayList[V];
createGraph(graph);
boolean visited [] = new boolean[V];
for (int i = 0; i < V; i++) {
if (visited[i]==false){
BFS(graph, V , visited , i) ;
}
}
System.out.println();
*/
/*int V = 4 ;
ArrayList <Edge> graph [] = new ArrayList[V];
createGraph(graph);
boolean visited [] = new boolean[V];
for (int i = 0; i < V; i++) {
if (visited[i]==false){
DFS(graph, i, visited);
}
}
System.out.println();
*/
int V = 4 ;
ArrayList <Edge> graph []= new ArrayList[V];
createGraph(graph);
/* int target = 3;
int source = 0;
AllPath(graph, source,new boolean [V], "0", target);
*/
//System.out.println(CycleDetection(graph, 0, new boolean[V], new boolean[V]));
// topSort(graph, V);
// System.out.println(isCycleDetection(graph, 0,new boolean[V], -1)) ;
}
}