-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
359 lines (291 loc) · 12.5 KB
/
Main.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import java.io.*;
import java.util.*;
//Name Surname: Selenay Alparslan
//Student ID: 110510194
public class Main {
/////////////////////////////////QUICK SORT///////////////////////////////////////
public static void quickSort(ArrayList<Integer> list, int start, int end) {
if (start < end) {
int pi = sorting(list, start, end);
quickSort(list, start, pi-1);
quickSort(list, pi+1, end);
}
}
public static int sorting(ArrayList<Integer> list, int start, int end) {
int pivot = list.get(end);
int i = (start-1);
for (int j=start; j<end; j++) {
if (list.get(j) < pivot) {
i++;
//swapping
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
//changing pivot
int temp = list.get(i+1);
list.set(i+1, list.get(end));
list.set(end, temp);
return i+1;
}
/////////////////////////////////HEAP SORT///////////////////////////////////////
public static void heapSort(ArrayList<Integer> list, ArrayList<ArrayList<Integer>> common) {
int size = list.size();
// Build heap (rearrange array)
for (int i=size/2-1; i>=0; i--)
heap(list, size, i, common);
// One by one extract an element from heap
for (int i=size-1; i>0; i--) {
// Move current root to end
int temp = list.get(0);
list.set(0, list.get(i));
list.set(i, temp);
Collections.swap(common.get(0), 0, i);
Collections.swap(common.get(1), 0, i);
// call max heapify on the reduced heap
heap(list, i, 0, common);
}
}
public static void heap(ArrayList<Integer> list, int n, int i, ArrayList<ArrayList<Integer>> common){
int largest = i; //Initialize largest item as root
int left = 2*i + 1;
int right = 2*i + 2;
//When left child is larger than root(largest)
if (left < n && list.get(left) > list.get(largest)) largest = left;
//If right child is larger than root(largest)
if (right < n && list.get(right) > list.get(largest)) largest = right;
//If largest is not root
if (largest != i) {
int swap = list.get(i);
list.set(i, list.get(largest));
list.set(largest, swap);
Collections.swap(common.get(0), i, largest);
Collections.swap(common.get(1), i, largest);
//Recursively heapify sub-trees
heap(list, n, largest, common);
}
}
/////////////////////////////////MERGE SORT///////////////////////////////////////
public static ArrayList<Integer> mergeSort(ArrayList<Integer> whole) {
ArrayList<Integer> left = new ArrayList<Integer>();
ArrayList<Integer> right = new ArrayList<Integer>();
int mid;
if (whole.size() == 1) return whole;
else {
mid = whole.size()/2;
for(int i=0; i<mid; i++) {
left.add(whole.get(i));
}
for(int i=mid; i<whole.size(); i++) {
right.add(whole.get(i));
}
left = mergeSort(left);
right = mergeSort(right);
merging(left, right, whole);
}
return whole;
}
public static void merging(ArrayList<Integer> left, ArrayList<Integer> right, ArrayList<Integer> whole) {
int leftIndex = 0;
int rightIndex = 0;
int wholeIndex = 0;
while (leftIndex < left.size() && rightIndex < right.size()) {
if (left.get(leftIndex).compareTo(right.get(rightIndex)) < 0) {
whole.set(wholeIndex, left.get(leftIndex));
leftIndex++;
}
else {
whole.set(wholeIndex, right.get(rightIndex));
rightIndex++;
}
wholeIndex++;
}
ArrayList<Integer> array;
int restIndex;
if (leftIndex >= left.size()) {
array = right;
restIndex = rightIndex;
}
else {
array = left;
restIndex = leftIndex;
}
for (int i=restIndex; i<array.size(); i++) {
whole.set(wholeIndex, array.get(i));
wholeIndex++;
}
}
/////////////////////////////////INSERTION SORT///////////////////////////////////////
public static void insertionSort(ArrayList<Integer> list) {
int n = list.size();
for(int i=1; i<n; i++) {
int a = list.get(i);
int j = i-1;
while (j>=0 && list.get(j)>a) {
list.set(j+1, list.get(j));
j--;
}
list.set(j+1, a);
}
}
public static void main(String[] args) throws IOException {
File control = new File("control.txt");
FileWriter fw = new FileWriter(control);
File mainFile = new File("FinalExamGraphFile.txt");
Scanner bigFile = new Scanner(mainFile);
Graph wholeGraph = new Graph(32836);
//Reading big FinalExamGraphFile.txt file and loading friendships into graph
ArrayList<ArrayList<Integer>> friendship = new ArrayList<ArrayList<Integer>>();
while(bigFile.hasNext()) {
int a = bigFile.nextInt();
int b = bigFile.nextInt();
int c = bigFile.nextInt();
ArrayList<Integer> friends = new ArrayList<Integer>();
friends.add(a);
friends.add(b);
friendship.add(friends);
wholeGraph.addEdge(a,b,c);
}
//Searching for adjacents of an ID and printing them out
System.out.println("Please enter an ID to search: ");
Scanner userInput = new Scanner(System.in);
int toSearch = userInput.nextInt();
wholeGraph.askForAdjacents(toSearch);
System.out.println("\n------------------------------ Please wait for sorting ------------------------------");
System.out.println("(If you wish, you can check 'control.txt' file to see common friends that pairs have.)\n");
//Loading Positive.txt file
File positiveFile = new File("Positive.txt");
Scanner readPositive = new Scanner(positiveFile);
Graph positiveGraph = new Graph(15000);
ArrayList<ArrayList<Integer>> positives = new ArrayList<ArrayList<Integer>>();
while(readPositive.hasNext()) {
int a = readPositive.nextInt();
int b = readPositive.nextInt();
ArrayList<Integer> positivePairs = new ArrayList<Integer>();
positivePairs.add(a);
positivePairs.add(b);
positives.add(positivePairs);
positiveGraph.addEdge(a, b, 1);
}
//Creating random negative pairs that are not included in
//the other two file (positive pairs and big file of friendships)
Random r = new Random();
Graph negativeGraph = new Graph(10000);
ArrayList<ArrayList<Integer>> negatives = new ArrayList<ArrayList<Integer>>();
for(int i=0; i<5592; i++) {
int a = r.nextInt(2000);
int b = r.nextInt(2000);
ArrayList<Integer> negativePairs1 = new ArrayList<Integer>();
negativePairs1.add(a);
negativePairs1.add(b);
if(a==b || positives.contains(negativePairs1) || friendship.contains(negativePairs1)) {
int newa = r.nextInt(2000);
int newb = r.nextInt(2000);
ArrayList<Integer> negativePairs2 = new ArrayList<Integer>();
negativePairs2.add(newa);
negativePairs2.add(newb);
negatives.add(negativePairs2);
negativeGraph.addEdge(newa, newb, 1);
}
else {
negatives.add(negativePairs1);
negativeGraph.addEdge(a, b, 1);
}
}
//Creating arraylist of arraylist in purpose of
//collecting all data (source,destination,common friend numbers)
ArrayList<ArrayList<Integer>> commons = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> sources = new ArrayList<Integer>();
ArrayList<Integer> destinations = new ArrayList<Integer>();
ArrayList<Integer> score = new ArrayList<Integer>();
//commons.get(0) will give "sources" in order
//commons.get(1) will give "destinations" in order
//commons.get(2) will give "scores" in order
commons.add(sources);
commons.add(destinations);
commons.add(score);
fw.write("-----------------------------------------NOT-SORTED-----------------------------------------\n");
//Finding common friend scores of negative pairs
Graph negativesWithScore = new Graph(10000);
for(int i=0; i<negativeGraph.size; i++) {
LinkedList<Edge> source = negativeGraph.list[i];
for(int j=0; j<source.size(); j++) {
int common = wholeGraph.findCommon(i, source.get(j).destination);
fw.write(i + " and "+ source.get(j).destination + " have " + common +" common (Negative Pairs)\n");
commons.get(0).add(i);
commons.get(1).add(source.get(j).destination);
commons.get(2).add(common);
negativesWithScore.addEdge(i, source.get(j).destination, common);
}
}
//Finding common friend scores of positive pairs
Graph positivesWithScore = new Graph(10000);
for(int i=0; i<positiveGraph.size; i++) {
LinkedList<Edge> source = positiveGraph.list[i];
for(int j=0; j<source.size(); j++) {
int common = wholeGraph.findCommon(i, source.get(j).destination);
fw.write(i + " and "+ source.get(j).destination + " have " + common +" common (Positive Pairs)\n");
commons.get(0).add(i);
commons.get(1).add(source.get(j).destination);
commons.get(2).add(common);
positivesWithScore.addEdge(i, source.get(j).destination, common);
}
}
//Copying common friend score arraylist to use all sorting algorithms properly
ArrayList<Integer> newCommons1 = new ArrayList<Integer>(score);
ArrayList<Integer> newCommons2 = new ArrayList<Integer>(score);
ArrayList<Integer> newCommons3 = new ArrayList<Integer>(score);
//Performing quick sort and calculating its execution time
long startQuick = System.currentTimeMillis();
quickSort(score, 0, score.size()-1);
long endQuick = System.currentTimeMillis();
System.out.println("Quick Sort Execution Time: " + (endQuick - startQuick) + " ms");
//Performing heap sort and calculating its execution time
long startHeap = System.currentTimeMillis();
heapSort(newCommons1, commons);
long endHeap = System.currentTimeMillis();
System.out.println("Heap Sort Execution Time: " + (endHeap - startHeap) + " ms");
//Performing merge sort and calculating its execution time
long startMerge = System.currentTimeMillis();
mergeSort(newCommons2);
long endMerge = System.currentTimeMillis();
System.out.println("Merge Sort Execution Time: " + (endMerge - startMerge) + " ms");
//Performing insertion sort and calculating its execution time
long startInsertion = System.currentTimeMillis();
insertionSort(newCommons3);
long endInsertion = System.currentTimeMillis();
System.out.println("Insertion Sort Execution Time: " + (endInsertion - startInsertion) + " ms");
//Printing out the sorted list of negative/positive pairs to the "control.txt" file
fw.write("-----------------------------------------SORTED-----------------------------------------\n");
for(int i=0; i<commons.get(0).size(); i++) {
ArrayList<Integer> pair = new ArrayList<Integer>();
pair.add(commons.get(0).get(i));
pair.add(commons.get(1).get(i));
if(negatives.contains(pair)) {
fw.write(commons.get(0).get(i) + " and "+ commons.get(1).get(i) + " have " + commons.get(2).get(i)+" common (Negative Pairs)\n");
}
if(positives.contains(pair)) {
fw.write(commons.get(0).get(i) + " and "+ commons.get(1).get(i) + " have " + commons.get(2).get(i)+" common (Positive Pairs)\n");
}
}
//Calculating accuracy and printing out it to console
//(Checking how many negative pair is included in the first 5592 item of mixed array)
int count=0;
for(int i=0; i<commons.get(0).size()/2; i++) {
ArrayList<Integer> neg = new ArrayList<Integer>();
neg.add(commons.get(0).get(i));
neg.add(commons.get(1).get(i));
if(negatives.contains(neg)) {
count++;
}
}
float accuracy = (float) count/11184;
System.out.printf("Accuracy: %d / 11184 (%.4f) (Assumed that 0-5592 is negative, 5592-11184 is positive)\n", 2*count, accuracy);
//Closing all scanners and file writer
readPositive.close();
userInput.close();
bigFile.close();
fw.close();
}
}