-
Notifications
You must be signed in to change notification settings - Fork 1
/
WEEK 3
205 lines (186 loc) · 8.11 KB
/
WEEK 3
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
import java.io.*;
import java.util.*;
//packages
/**
* KargerMinCut.java
* <p>
* Randomized contraction algorithm for the minimize cut problem.
*/
// class for node in graph
class Node {
public int id;
public LinkedList<Integer> nodeEdge;
}
// AdjacencyLists for graph
class AdjacencyLists {
public int len = 200; // number of node
public Hashtable<Integer, Node> nodeHash = new Hashtable();
// add vertex(node),id from 1
public void addVertex(int id) {
Node node = new Node();
node.id = id; // from index of 0
node.nodeEdge = new LinkedList();
nodeHash.put(id, node);
}
public void addAdjvex(int start, int end) {
nodeHash.get(start).nodeEdge.addLast(end);
}
public int getAdjvex(int node, int index) {
return nodeHash.get(node).nodeEdge.get(index);
}
public void contractVertex(int nodeA, int nodeB) {
deleteEdge(nodeA, nodeB); // delete the edge nodeB in nodeA
findEnd(nodeA, nodeB); // change nodeB to a in node change
addList(nodeA, nodeB); // add nodeB in the last of nodeA
nodeHash.remove(nodeB);// remove nodeB
}
// delete edge,number of edge need be changed is >=1, even >2...
public void deleteEdge(int nodeA, int nodeB) {
while (nodeHash.get(nodeA).nodeEdge.contains(nodeB)) {
int index = nodeHash.get(nodeA).nodeEdge.indexOf(nodeB);
nodeHash.get(nodeA).nodeEdge.remove(index);
// print(nodeA);
}
while (nodeHash.get(nodeB).nodeEdge.contains(nodeA)) {
int index = nodeHash.get(nodeB).nodeEdge.indexOf(nodeA);
nodeHash.get(nodeB).nodeEdge.remove(index);
// print(nodeB);
}
}
public void findEnd(int nodeA, int nodeB) {
boolean record[] = new boolean[len];// check the node which has been changed yet
for (int index = 0; index < nodeHash.get(nodeB).nodeEdge.size(); index++) {
int change = nodeHash.get(nodeB).nodeEdge.get(index);
if (record[change-1] == false) {
changeToEnd(nodeA, nodeB, change);
record[change-1] = true;
}
}
}
public void changeToEnd(int nodeA, int nodeB, int change) {
for (int index = 0; index < nodeHash.get(change).nodeEdge.size(); index++) {
if (nodeHash.get(change).nodeEdge.get(index) == nodeB) {
nodeHash.get(change).nodeEdge.set(index, nodeA);
}
}
}
public void addList(int nodeA, int nodeB) {
while (!nodeHash.get(nodeB).nodeEdge.isEmpty()) {
nodeHash.get(nodeA).nodeEdge.addLast(nodeHash.get(nodeB).nodeEdge
.pop());
}
// print(nodeA);
}
// computer the edge number of nodeHash.get(0)
public int computerEdge(int node) {
int n = nodeHash.get(node).nodeEdge.size();
return n;
}
// print edges of nodeA, for test
public void print(int nodeA) {
if (!nodeHash.get(nodeA).nodeEdge.isEmpty()) {
System.out.print("[" + nodeA + "]:");
for (int index = 0; index < nodeHash.get(nodeA).nodeEdge.size(); index++) {
System.out.print(nodeHash.get(nodeA).nodeEdge.get(index) + "-");
}
System.out.println();
}
}
}
// Randomized contraction algorithm for the minimize cut problem
public class MinCut {
private AdjacencyLists graph = new AdjacencyLists();
private int nodeLen = graph.len;
private int cutNum;
private int nodeA, nodeB;
private int min = nodeLen - 1;
private ArrayList<Integer> arraySet = new ArrayList();
private int count = 0;
public MinCut() {
for (count = 0; count < 50; count++) {
input();
contract();
}
System.out.println(min);
}
// input the graph file
public void input() {
cutNum = nodeLen - 1;
graph = new AdjacencyLists();
arraySet = new ArrayList();
try {
String file = "C:\\Users\\Acer\\Downloads\\kargerMinCut.txt";
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
String s;
int x;
while ((s = br.readLine()) != null) {
Scanner sca = new Scanner(s);
x = sca.nextInt();
graph.addVertex(x);
// System.out.print(x); //test
while (sca.hasNext()) {
int y = sca.nextInt();
graph.addAdjvex(x, y);
}
}
} catch (Exception e) {
e.printStackTrace();
}
initSet();
}
// initialize the arraySet for the random set
public void initSet() {
for (int j = 0; j < nodeLen; j++) {
arraySet.add(j, j+1);
// System.out.println(arraySet.get(j)); //test
}
}
// contract two random node until there are only two node left
public void contract() {
int num;
while (arraySet.size() > 2) {
// System.out.println(arraySet.size()); //test
randEdge();
graph.contractVertex(nodeA, nodeB);
// System.out.println(graph.getLen()); //test
tempNum(nodeA);
}
// when the contract is end, print the current and in all minNum
if (arraySet.size() == 2) {
if (cutNum < min) {
min = cutNum;
}
// if(graph.UFO < min){
// min = graph.UFO;
// } //test
System.out.println("///" + cutNum + " - " + min + "///" + count); // test
}
}
// computer the edge number of node chosen
public void tempNum(int a) {
int num = graph.computerEdge(a); // computer edge of a after contract
if (num < cutNum) {
cutNum = num;
}
// graph.print(a); //test
// System.out.println("///////"+num + "///////"); //test
}
// randomize a edge
public void randEdge() {
Random rand = new Random();
int indexOfNodeA;
int indexOfNodeB;
indexOfNodeA = rand.nextInt(arraySet.size());// from 0 to getLen()-1
nodeA = arraySet.get(indexOfNodeA); // from 1 to size
int randomSeed = graph.nodeHash.get(nodeA).nodeEdge.size();//nodeEdge index start from 0
indexOfNodeB = rand.nextInt(randomSeed); // from 0 to
// nodeA.nodeEdge.size()
nodeB = graph.getAdjvex(nodeA, indexOfNodeB);
arraySet.remove(arraySet.indexOf(nodeB));
// System.out.println(nodeA + "+++" + nodeB); // test
}
public static void main(String[] args) {
MinCut test = new MinCut(); // test
}
}