-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCycleInDirectedGraph.java
146 lines (108 loc) · 3.78 KB
/
CycleInDirectedGraph.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
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
/*
Problem Description
Given an directed graph having A nodes. A matrix B of size M x 2 is given which represents the M edges
such that there is a edge directed from node B[i][0] to node B[i][1].
Find whether the graph contains a cycle or not, return 1 if cycle is present else return 0.
NOTE:
The cycle must contain atleast two nodes.
There are no self-loops in the graph.
There are no multiple edges between two nodes.
The graph may or may not be connected.
Nodes are numbered from 1 to A.
Your solution will run on multiple test cases. If you are using global variables make sure to clear them.
Problem Constraints
2 <= A <= 105
1 <= M <= min(200000,A(A-1))
1 <= B[i][0], B[i][1] <= A
Input Format
The first argument given is an integer A representing the number of nodes in the graph.
The second argument given a matrix B of size M x 2 which represents the M edges such that there is a edge directed from node B[i][0] to node B[i][1].
Output Format
Return 1 if cycle is present else return 0.
Example Input
Input 1:
A = 5
B = [ [1, 2]
[4, 1]
[2, 4]
[3, 4]
[5, 2]
[1, 3] ]
Input 2:
A = 5
B = [ [1, 2]
[2, 3]
[3, 4]
[4, 5] ]
Example Output
Output 1:
1
Output 2:
0
Example Explanation*
Explanation 1:
The given graph contain cycle 1 -> 3 -> 4 -> 1 or the cycle 1 -> 2 -> 4 -> 1
Explanation 2:
The given graph doesn't contain any cycle.
*/
public class Solution {
public int solve(int A, ArrayList<ArrayList<Integer>> B) {
HashMap<Integer,ArrayList<Integer>> adjMap = new HashMap<>();
for(int i=0; i<B.size(); i++){
int x = B.get(i).get(0);
int y = B.get(i).get(1);
if(adjMap.containsKey(x)){
ArrayList<Integer> list = adjMap.get(x);
list.add(y);
adjMap.put(x,list);
}
else{
ArrayList<Integer> list = new ArrayList<>();
list.add(y);
adjMap.put(x,list);
}
}
int[] state = new int[A+1];
Queue<Integer> queue = new LinkedList<>();
for(int i=1; i<=A; i++){
if(state[i] == 0){
queue.add(i);
state[i] = 1; //processing
if(checkCycle(queue,state,adjMap) == true){
return 1;
}
}
}
return 0;
}
private boolean checkCycle(Queue<Integer> queue,int[] state,
HashMap<Integer,ArrayList<Integer>> adjMap){
while(queue.size() > 0){
int size = queue.size();
ArrayList<Integer> al = new ArrayList<>();
for(int j=0; j<size; j++){
int node = queue.poll();
al.add(node);
if(adjMap.containsKey(node)){
ArrayList<Integer> list = adjMap.get(node);
for(int i=0; i<list.size(); i++){
if(state[list.get(i)] == 0){ //untouched
queue.add(list.get(i));
state[list.get(i)] = 1; //processing
}
else if(state[list.get(i)] == 1){
//ignore
}
else if(state[list.get(i)] == 2){
return true; //cycle found (difference in result with UnDiretced graph)
}
}
}
}
for(int j=0; j<al.size(); j++){
state[al.get(j)] = 2; //processed
}
}
return false;
}
}