-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickUnion.java
More file actions
148 lines (118 loc) · 3.3 KB
/
Copy pathQuickUnion.java
File metadata and controls
148 lines (118 loc) · 3.3 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
/* *****************************************************************************
* Name: Rafael Neves Moraes
*
* Description: Quick Union Algorithm.
*
* Written: 8/05/2019
*
* % javac QuickUnion.java
* % java QuickUnion exemple.txt
*
* Formatter:
* <file> :
* n -> number of lines
* row col -> list of rows and cols
* Ex.:
* 20
* 7 11
* 18 11
* 12 5
* 9 5
* 5 9
**************************************************************************** */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class QuickUnion
{
private int[] id;
private int[] sz;
private int tIem;
private int t;
public QuickUnion(int n)
{
id = new int[n];
sz = new int[n];
t = n - 1;
tIem = n;
for(int i = 0; i < n; i++) {
id[i] = i;
sz[i] = 1;
}
}
public boolean connected(int p, int q)
{
validateRowCol(p, q);
return root(p) == root(q);
}
public void union(int p, int q)
{
validateRowCol(p, q);
int i = root(p);
int j = root(q);
if(i == j) return;
if(sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; }
else { id[j] = i; sz[i] += sz[j]; }
t -= 1;
}
public int[] getId()
{
return id;
}
public boolean allConnected()
{
if(t <= 0)
return true;
return false;
}
public int getTotalGroups()
{
return t + 1;
}
private int root(int i)
{
i -= 1;
while (i != id[i]) {
id[i] = id[id[i]];
i = id[i];
}
return i;
}
private void validateRowCol(int row, int col)
{
if(row <= 0 || row > tIem )
throw new IllegalArgumentException("row value " + String.valueOf(row) + " out of bounds!");
if(col <= 0 || col > tIem )
throw new IllegalArgumentException("col value " + String.valueOf(col) + " out of bounds!");
}
public static void main(String[] args)
{
try {
FileReader fileReader = new FileReader(args[0]);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int n = Integer.valueOf(bufferedReader.readLine());
if(n <= 0)
throw new IllegalArgumentException("First file argument must be gran than 0!");
QuickUnion qn = new QuickUnion(n);
String line = null;
while((line = bufferedReader.readLine()) != null) {
String[] item = Arrays.stream(line.split(" ")).filter(x -> !x.isEmpty()).toArray(String[]::new);
int row = Integer.parseInt(item[0]);
int col = Integer.parseInt(item[1]);
qn.union(row,col);
}
bufferedReader.close();
System.out.println(String.format("End array: %s", Arrays.toString(qn.getId())));
System.out.println(String.format("Total of groups: %s", qn.getTotalGroups()));
System.out.println(String.format("All poins is connected? %s", qn.allConnected()));
}
catch(FileNotFoundException e) {
System.out.println("Unable to open file");
}
catch(IOException e) {
System.out.println("Error reading file");
}
}
}