-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jury_Pool2.java
executable file
·183 lines (149 loc) · 4.47 KB
/
Jury_Pool2.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
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
/*
* The structure of this class is similar to the Jury_Pool1 class. There are two crucial differences.
* You have to use a new graph reflecting 2-relationship.
* You have to compute 3 fields. The first field easy it is simply the original graph size.
* The second field is the size of the pool of potential jurors and the third field is a list of juror
* id's satisfying the condition that there is no 2-relations between any two. These fields are exactly
* like the corresponding fields in Jury_Pool1. The names are slightly modified.
*/
package jury;
import java.util.ArrayList;
import org.omg.CORBA.ObjectHolder;
public class Jury_Pool2 {
int initialSize = 0;
int juryPool2Size = 0;
int[] juryPool2List;
public Jury_Pool2(String s) {
Jury_Relation2Graph candidateRelation = new Jury_Relation2Graph(s);
/*
The string 's' represents the path to the input file.
*/
initialSize = candidateRelation.graphSize;
juryPool2Size = calcPool2size(candidateRelation);
juryPool2List = calcPool2List(candidateRelation);
}
public int findMinIdx (int input[], ArrayList<Integer> ignore) {
int min = -1;
int minIdx = -1;
if (ignore.size()==0) {
min = input[0];
minIdx = 0;
}
for (int i = 0; i < input.length; i++) {
if(!ignore.contains(i) && input[i]!=0) {
min = input[i];
minIdx = i;
break;
}
}
for (int i = 0; i < input.length; i++) {
if ((!ignore.contains(i)) && (input[i] != 0 && input[i] <min)) {
min = input[i];
minIdx = i;
}
}
return minIdx;
}
boolean isCut(int[][] arr, int n) {
for (int i = 0; i < arr.length; i++) {
if(arr[n][i]!=-1)
return false;
}
return true;
}
public int findMinMat (int [][] arr) {
int minIdx = 0;
int minSum=0;
boolean firstTime = true;
for (int i = 0; i < arr[0].length; i++) {
if(isCut(arr, i)) {
continue;
}
int sum = 0;
for (int j = 0; j < arr[0].length; j++) {
if(i!=j) {
if(arr[i][j]==-1)
sum+=0;
else
sum+=arr[i][j];
}
}
if (sum==0) {
continue;
}
if(firstTime) {
minSum = sum;
minIdx = i;
firstTime=false;
}
else {
if (minSum==0) {
minSum = sum;
}
if(sum<minSum) {
minSum = sum;
minIdx = i;
}
}
}
return minIdx;
}
public void cutTheMat(int arr[][], int n) {
for (int j = 0; j < arr[0].length; j++) {
arr[n][j]=-1;
}
for (int j = 0; j < arr[0].length; j++) {
arr[j][n]=-1;
}
}
public ArrayList<Integer> calculate (Jury_Relation2Graph jG){
int bal[][] = new int[jG.finalProduct[0].length][jG.finalProduct[0].length];
for (int i=0; i<jG.finalProduct[0].length; i++) {
for (int j = 0; j < jG.finalProduct[0].length; j++) {
bal[i][j] = jG.finalProduct[i][j];
}
}
for (int i=0; i<bal[0].length; i++) {
int min = findMinMat(bal);
for (int j = 0; j < bal[0].length; j++) {
if(min!=j && bal[min][j]==1) {
cutTheMat(bal, j);
}
}
}
ArrayList<Integer> jList = new ArrayList<Integer>();
for (int i=0; i<bal[0].length; i++) {
for (int j=0; j<bal[0].length; j++) {
if(bal[i][j]==1)
jList.add(i+1);
}
}
return jList;
}
int calcPool2size(Jury_Relation2Graph jG) {
int poolSize = 0;
/*
The variable poolSize is given as a suggestion. You are free to change it if you wish.
Of course you have to return some variable of integer type. It has been initialized to 0.
Note: 0 is not an acceptable value for the jury pool size.
You can write any other classes and methods
to help you calculate.
*/
poolSize = calculate(jG).size();
return poolSize;
}
int[] calcPool2List(Jury_Relation2Graph jG) {
int[] poolList = new int[juryPool2Size];
/*
* We have an integer array poolList. To create this array you will have to calculate
* juryPoolSize first. You can change it. But make sure the function returns an array
* of integers that represents an acceptable jury pool.
* It is suggested that you create some classes and methods of your own and call them here. This
* method and the one preceding can be considered 'wrapper' methods.
*/
ArrayList<Integer> x = calculate(jG);
for (int i=0; i<juryPool2Size; i++)
poolList[i] = x.get(i);
return poolList;
}
}