-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphOperation.java
More file actions
253 lines (225 loc) · 8.28 KB
/
Copy pathGraphOperation.java
File metadata and controls
253 lines (225 loc) · 8.28 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
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
package basicAlgorithm;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
public class GraphOperation {
HashMap<Integer, HashMap<Integer, Integer>> originalGraph; // original Graph
HashMap<Integer, HashMap<Integer, Integer>> graphMatrix; // save the graph
// in rows:
// vertexs:
// <neighborX,
// edge to
// neighborX>..
HashMap<Integer, LinkedList<Integer>> mergedNodes; // save the mapping
// between final node :
// nodes contained in
// this node
public GraphOperation() {
originalGraph = new HashMap<Integer, HashMap<Integer, Integer>>();
graphMatrix = new HashMap<Integer, HashMap<Integer, Integer>>();
mergedNodes = new HashMap<Integer, LinkedList<Integer>>();
}
public int kargerRandomMinCut() {
int retval = 0;
while (graphMatrix.keySet().size() > 2) {
// printMergedNodes();
// Integer[] vertexArray = (Integer[])
// graphMatrix.keySet().toArray();
ArrayList<Integer> vertexArray = new ArrayList<Integer>(
graphMatrix.keySet());
int randomVertexIndex = (int) (Math.random() * (vertexArray.size()));
int randomVertex = vertexArray.get(randomVertexIndex);
HashMap<Integer, Integer> neighborTable = graphMatrix
.get(randomVertex);
ArrayList<Integer> neighborArray = new ArrayList<Integer>(
neighborTable.keySet());
int randomNeighborIndex = (int) (Math.random() * (neighborArray
.size()));
int randomNeighbor = neighborArray.get(randomNeighborIndex);
// System.out.println("VerticeArraylen=" + vertexArray.size()
// + " neighborArrayLen=" + neighborArray.size());
// System.out.println("vertex=" + randomVertex + " neighbor="
// + randomNeighbor);
shrinkByEdge(randomVertex, randomNeighbor);
// System.out.println("------------------------------------");
}
// when only two elements left in array, they're essentially A,B, print
// A and B, and the number of crossing edges between them
Iterator<Integer> it = graphMatrix.keySet().iterator();
while (it.hasNext()) {
int vertex = it.next();
HashMap<Integer, Integer> neighbor = graphMatrix.get(vertex);
// System.out.print(vertex + ":");
Iterator<Integer> innerIt = neighbor.keySet().iterator();
while (innerIt.hasNext()) {
int neighborVertex = innerIt.next();
int edgeCount = neighbor.get(neighborVertex);
retval = edgeCount;
// System.out.print("(" + neighborVertex + "," + edgeCount +
// ")");
}
// LinkedList<Integer> nodes = mergedNodes.get(vertex);
// if (nodes != null) {
// System.out.print("[");
// for (int i : nodes) {
// System.out.print(i + ",");
// }
// System.out.println("]");
// } else {
// System.out.println("null!!");
// }
}
return retval;
}
public void printMergedNodes() {
Iterator<Integer> it = mergedNodes.keySet().iterator();
while (it.hasNext()) {
int vertex = it.next();
System.out.print(vertex + ":");
LinkedList<Integer> nodes = mergedNodes.get(vertex);
if (nodes != null) {
System.out.print("[");
for (int i : nodes) {
System.out.print(i + ",");
}
System.out.println("]");
} else {
System.out.println("null!!");
}
}
}
/**
* @param args
* @throws IOException
*/
public void shrinkByEdge(int vertex1, int vertex2) {
int lowVertex = vertex1 > vertex2 ? vertex2 : vertex1;
int highVertex = vertex1 > vertex2 ? vertex1 : vertex2;
// System.out.println("low:high =" + lowVertex + ":" + highVertex);
HashMap<Integer, Integer> neighborHigh = graphMatrix.get(highVertex);
HashMap<Integer, Integer> neighborLow = graphMatrix.get(lowVertex);
// 1.go through neighbors of highVertex, if it's lowVertex, don't do
// anything, otherwise replace it with lowVertex in other neighbor's row
Iterator<Integer> it = neighborHigh.keySet().iterator();
while (it.hasNext()) {
int neighborVertex = it.next();
int edgeToNeighbor = neighborHigh.get(neighborVertex);
/*
* if it's edge to other vertices: 1. add these edges to low
* vertex's neighbor table; 2. modify the neighbor list of
* corresponding vertices, replacing high vertex by low vertex
*/
if (neighborVertex != lowVertex) {
// 1. if lowVertex has edge(s) to this vertex already, then just
// add up the No.edges, otherwise add this entry
if (neighborLow.containsKey(neighborVertex)) {
int existEdgeNumber = neighborLow.get(neighborVertex);
int newEdgeNumber = existEdgeNumber + edgeToNeighbor;
neighborLow.put(neighborVertex, newEdgeNumber);
} else {
neighborLow.put(neighborVertex, edgeToNeighbor);
}
/*
* 2. update this neighborVertex's neighbor table. If it already
* has edge to lowVertex, increase the No.edge, otherwise, add
* this entry; remove the highVertex entry
*/
HashMap<Integer, Integer> curNeighbor = graphMatrix
.get(neighborVertex);
// this should happen all the time
if (curNeighbor.containsKey(highVertex)) {
int edgeToHigh = curNeighbor.get(highVertex);
if (curNeighbor.containsKey(lowVertex)) {
int edgeToLow = curNeighbor.get(lowVertex);
int newEdgeNum = edgeToLow + edgeToHigh;
curNeighbor.put(lowVertex, newEdgeNum);
} else {
curNeighbor.put(lowVertex, edgeToHigh);
}
curNeighbor.remove(highVertex);
}
}
}
// 3.remove self-loop for low vertex's neighbor table; (if neighbor is
// highVertex, remove this entry from neighbor table);
neighborLow.remove(highVertex);
// 4.remove the entry of high Vertex from graphMatrix
graphMatrix.remove(highVertex);
/*
* Update the mergedNodes table 1. If lowVertex doesn't have an entry
* there, add entry with key lowVertex, value highVertex; 2. If
* lowVertex already has an entry there, add highVertex to the end of
* it; 3. If highVertex already has an entry there, add lowVertex entry
* and copy all nodes in highVertex to lowVertex entry;
*/
if (mergedNodes.containsKey(lowVertex)) {
LinkedList<Integer> existNodesMerged = mergedNodes.get(lowVertex);
if (mergedNodes.containsKey(highVertex)) {
LinkedList<Integer> highNodes = mergedNodes.get(highVertex);
existNodesMerged.addAll(highNodes);
}
existNodesMerged.add(highVertex);
} else {
LinkedList<Integer> nodesMerged = new LinkedList<Integer>();
if (mergedNodes.containsKey(highVertex)) {
LinkedList<Integer> highNodes = mergedNodes.get(highVertex);
nodesMerged.addAll(highNodes);
}
nodesMerged.add(highVertex);
mergedNodes.put(lowVertex, nodesMerged);
}
}
public void readFileIntoMemory(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
int curVertex = 0;
int neighborVertex = 0;
int edgeToNeighbor = 1;
while (line != null) {
String[] vertexRow = line.split("\t");
HashMap<Integer, Integer> neighborCount = new HashMap<Integer, Integer>();
curVertex = Integer.parseInt(vertexRow[0]);
for (int i = 1; i < vertexRow.length; i++) {
neighborVertex = Integer.parseInt(vertexRow[i]);
neighborCount.put(neighborVertex, edgeToNeighbor);
}
graphMatrix.put(curVertex, neighborCount);
line = br.readLine();
}
br.close();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int lowest=Integer.MAX_VALUE;
for (int i = 0; i < 100000; i++) {
GraphOperation obj = new GraphOperation();
obj.readFileIntoMemory("kargerMinCut.txt");
int retval =obj.kargerRandomMinCut();
if(retval<lowest){
lowest=retval;
}
}
System.out.print(lowest);
// Iterator<Integer> it = obj.graphMatrix.keySet().iterator();
// while (it.hasNext()) {
// int key = it.next();
// System.out.print(key + ":");
// HashMap<Integer, Integer> value = obj.graphMatrix.get(key);
// Iterator<Integer> innerIt = value.keySet().iterator();
// while (innerIt.hasNext()) {
// int innerKey = innerIt.next();
// int innerValue = value.get(innerKey);
// System.out.print("(" + innerKey + "," + innerValue + ")" + " ");
// }
// System.out.println();
// }
//
// System.out.println(obj.graphMatrix.keySet().size());
}
}