-
Notifications
You must be signed in to change notification settings - Fork 0
/
BonusTask.java
232 lines (218 loc) · 8.06 KB
/
BonusTask.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
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
// Copyright 2020
// Author: Matei Simtinică
import java.io.*;
import java.util.*;
/**
* Bonus Task
* You have to implement 4 methods:
* readProblemData - read the problem input and store it however you see fit
* formulateOracleQuestion - transform the current problem instance into a SAT instance and write the oracle input
* decipherOracleAnswer - transform the SAT answer back to the current problem's answer
* writeAnswer - write the current problem's answer
*/
public class BonusTask extends Task {
int nodes;
int lines;
public Map<Integer, ArrayList<Integer>> families = new LinkedHashMap<>();
public Map<Integer, ArrayList<Integer>> families2 = new LinkedHashMap<>();
Map<Integer, ArrayList<Integer>> complGraph = new LinkedHashMap<>();
public LinkedHashSet<Integer> max = new LinkedHashSet<>();
public ArrayList<Integer> arrests = new ArrayList<>();
/**
*
* @param famNumb number of families
* @param families data structure to be used for stocking the relations between the families
*/
public void inititializeMaps(int famNumb, Map<Integer, ArrayList<Integer>> families){
for( int i = 0; i < famNumb; i++ ){
families.put(i, new ArrayList<>());
}
}
/**
*
* @param node the number of a family
* @return 1 if the node belongs to he found clique
* -1 otherwise
*/
public int belongsClique(int node){
if ( arrests.contains(node) ){
return 1;
}
else return -1;
}
/**
*
* @param arrayList list containing the nodes that make up the clique
* @return a set containing all the nodes that are connected to the nodes
* from the arrayList
*/
public LinkedHashSet findNodes(ArrayList<Integer> arrayList){
LinkedHashSet<Integer> allNodes = new LinkedHashSet<>();
for (Integer integer : arrayList) {
allNodes.addAll(families.get(integer));
}
allNodes.addAll(arrayList);
// adding th nodes that have no edges, if there are any
for (int i = 0; i < nodes; i ++){
if (families.get(i).size() == 0){
allNodes.add(i);
}
}
return allNodes;
}
/**
*
* @param families the initial graph
* @return the complementary graph
*/
public Map<Integer, ArrayList<Integer>> makeComplementary( Map<Integer, ArrayList<Integer>> families ){
Map<Integer, ArrayList<Integer>> newGraph = new LinkedHashMap<>();
inititializeMaps(nodes, newGraph);
for (int i = 0; i < nodes; i ++){
for (int k = 0; k < nodes; k ++){
if (!families.get(i).contains(k) && k != i){
newGraph.get(i).add(k);
}
}
}
return newGraph;
}
/**
*
* @param arrayList1 list containing the nodes connected to a node
* @param arrayList2 list containing the nodes connected to another node
* @return list containing the common elements of the two lists
*/
public ArrayList<Integer> getCommon(ArrayList<Integer> arrayList1 ,ArrayList<Integer> arrayList2 ){
ArrayList<Integer> common = new ArrayList<>();
LinkedHashSet<Integer> goodCommons = new LinkedHashSet<>();
for (Integer integer : arrayList1) {
if (arrayList2.contains(integer)) {
common.add(integer);
}
}
if (common.size() != 1 && common.size() != 0){
for (int i = 0; i < common.size(); i ++){
boolean ok = true;
for (int j = 0; j < common.size() && j != i; j ++){
if (!complGraph.get(common.get(i)).contains(common.get(j))){
ok = false;
}
}
if (ok){
goodCommons.add(common.get(i));
}
}
common.clear();
common.addAll(goodCommons);
}
return common;
}
/**
*
* @param nodes number of families
* @param families the graph containing the relations between the families
* @return the maxClique found in the graph
*/
public LinkedHashSet<Integer> findCLique(int nodes,
Map<Integer, ArrayList<Integer>> families){
LinkedHashSet<Integer> clique = new LinkedHashSet<>();
LinkedHashSet<Integer> maxClique = new LinkedHashSet<>();
for (int k = 0; k < nodes ; k ++){
for (int i = 0; i < nodes && i != k; i ++){
if (families.get(i).contains(k) &&
families.get(k).contains(i)){
clique.add(i);
clique.add(k);
clique.addAll(getCommon(families.get(i), families.get(k)));
if (maxClique.size() < clique.size()){
maxClique = clique;
}
clique = new LinkedHashSet<>();
}
}
}
return maxClique;
}
@Override
public void solve() throws IOException, InterruptedException {
readProblemData();
formulateOracleQuestion();
askOracle();
decipherOracleAnswer();
writeAnswer();
}
@Override
public void readProblemData() throws IOException {
Scanner scanner = new Scanner(new File(inFilename));
nodes = scanner.nextInt();
lines = scanner.nextInt();
inititializeMaps(nodes,families);
// creating the initial graph
for (int i = 0; i < lines; i ++){
int x = scanner.nextInt();
int y = scanner.nextInt();
families.get(x - 1).add(y - 1);
families.get(y - 1).add(x - 1);
}
}
@Override
public void formulateOracleQuestion() throws IOException {
FileWriter fileWriter1 = new FileWriter(oracleInFilename);
fileWriter1.write("p wcnf ");
// writing the formula
fileWriter1.write(Integer.toString(nodes + 1));
fileWriter1.write(" " + (nodes + 1) + " ");
fileWriter1.write(nodes * 3 + "\n");
fileWriter1.write((nodes * 3) + " 1 0\n");
for (int i = 1; i <= nodes + 1; i ++){
fileWriter1.write("1 " + (i + 1) + " 0\n");
}
fileWriter1.close();
}
@Override
public void decipherOracleAnswer() throws IOException {
FileWriter fileWriter = new FileWriter(oracleOutFilename);
complGraph = makeComplementary(families);
max = findCLique(nodes,complGraph);
inititializeMaps(nodes, families2);
// finding the nodes to be removed
for (int i = 0; i < nodes; i ++){
if (!max.contains(i)){
arrests.add(i);
}
}
ArrayList<Integer> newNodes = new ArrayList<>();
newNodes.addAll(findNodes(arrests));
// checking if the new graph is equivalent to the initial one
if (newNodes.size() == nodes) {
fileWriter.write("True\n");
fileWriter.write(nodes + 1 + "\n");
fileWriter.write("1 ");
for (int i = 1; i <= nodes + 1; i++) {
fileWriter.write((i + 1) * belongsClique(i - 1) + " ");
}
}
fileWriter.close();
}
@Override
public void writeAnswer() throws IOException {
FileWriter fileWriter = new FileWriter(outFilename);
BufferedReader reader = new BufferedReader(new FileReader(oracleOutFilename));
String line = reader.readLine();
// getting the solution from the oracle's answer
if (line.contains("True")){
line = reader.readLine();
int length = Integer.parseInt(line);
line = reader.readLine();
String[] newLine = line.split(" ");
for (int i = 1; i < nodes + 1; i ++ ){
if (Integer.parseInt(newLine[i]) > 0){
int node = Integer.parseInt(newLine[i]) - 1;
fileWriter.write(node + " ");
}
}
}
fileWriter.close();
}
}