Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
236 lines (194 loc) · 8.24 KB
/
Copy pathMain.java
File metadata and controls
236 lines (194 loc) · 8.24 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
/**
* Programming Question - Week 3, July 2013.
*
* The file contains the adjacency list representation of a simple undirected
* graph. There are 200 vertices labeled 1 to 200. The first column in the file
* represents the vertex label, and the particular row (other entries except the
* first column) tells all the vertices that the vertex is adjacent to. So for
* example, the 6th row looks like : "6 155 56 52 120......". This just means
* that the vertex with label 6 is adjacent to (i.e., shares an edge with) the
* vertices with labels 155,56,52,120,......,etc
*
* Your task is to code up and run the randomized contraction algorithm for the
* min cut problem and use it on the above graph to compute the min cut.
* (HINT: Note that you'll have to figure out an implementation of edge contractions.
* Initially, you might want to do this naively, creating a new graph from the old
* every time there's an edge contraction. But you should also think about more
* efficient implementations.) (WARNING: As per the video lectures, please make
* sure to run the algorithm many times with different random seeds, and remember
* the smallest cut that you ever find.) Write your numeric answer in the space
* provided. So e.g., if your answer is 5, just type 5 in the space provided.
*/
//package com.stanford.algorithms.weekthree;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Algorithms: Design and Analysis, Part 1
* Programming Question - Week 3
* @author Felix Garcia Lainez
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Read the graph from the text file
HashMap<Integer, ArrayList<Integer>> originalGraph = readGraphFromFile();
int minimumCut = 0;
for(int i = 0; i < 1000; i++)
{
//Copy the original graph in each iteration
HashMap<Integer, ArrayList<Integer>> copyGraph = copyGraph(originalGraph);
int result = processKargerMinimumCutAlgorithm(copyGraph);
if(minimumCut == 0){
minimumCut = result;
}
else{
if(result < minimumCut){
minimumCut = result;
}
}
System.out.println("Partial Result => " + result);
}
System.out.println("*** Minimum Cut => " + minimumCut + " ***");
}
/**
* Process the Karger Minimum Cut Algorithm for a given Graph
* @param graph The graph to be processed
* @return The Minimum Cut
*/
private static int processKargerMinimumCutAlgorithm(HashMap<Integer, ArrayList<Integer>> graph)
{
//Iterate until there are only two nodes
while(graph.size() > 2){
processKargerAlgorithmStep(graph);
}
//Return the Minimum Cut (the number of edges of both nodes is the same)
return graph.get((Integer)graph.keySet().toArray()[0]).size();
}
/**
* Process a single step in Karger Minimum Cut Algorithm
* @param graph The graph to be processed
*/
private static void processKargerAlgorithmStep(HashMap<Integer, ArrayList<Integer>> graph)
{
//Choose randome items
List<Integer> randomItems = chooseRandomItems(graph);
Integer firstItem = randomItems.get(0);
Integer secondItem = randomItems.get(1);
ArrayList<Integer> firstItemList = graph.get(firstItem);
ArrayList<Integer> secondItemList = graph.get(secondItem);
//Add second list items to first list
firstItemList.addAll(secondItemList);
//Remove second list items
graph.remove(randomItems.get(1));
//Replace second item appeareances by first item
Iterator it = graph.keySet().iterator();
while(it.hasNext())
{
Integer currentKey = (Integer)it.next();
ArrayList<Integer> currentItemList = graph.get(currentKey);
for(Integer i : currentItemList)
{
if(i.intValue() == secondItem.intValue()){
currentItemList.set(currentItemList.indexOf(i), firstItem);
}
}
}
//Remove loops
ArrayList<Integer> itemsToRemove = new ArrayList<Integer>();
for(Integer i : firstItemList)
{
if(i.intValue() == firstItem.intValue()){
itemsToRemove.add(i);
}
}
firstItemList.removeAll(itemsToRemove);
}
/**
* Select a randome node and a random edge of the list of this node
* @param graph
* @return
*/
private static List<Integer> chooseRandomItems(HashMap<Integer, ArrayList<Integer>> graph)
{
ArrayList<Integer> randomItems = new ArrayList<Integer>();
int nodeIndex = (int)(Math.random() * graph.keySet().size());
Integer randomNode = (Integer)(graph.keySet().toArray()[nodeIndex]);
int edgeIndex = (int)(Math.random() * graph.get(randomNode).size());
Integer randomEdge = graph.get(randomNode).get(edgeIndex);
randomItems.add(randomNode);
randomItems.add(randomEdge);
return randomItems;
}
/**
* Return a copy of the graph passed as parameter
* @param graph The graph to be copied
* @return The copy of the graph
*/
private static HashMap<Integer, ArrayList<Integer>> copyGraph(HashMap<Integer, ArrayList<Integer>> graph)
{
HashMap<Integer, ArrayList<Integer>> graphCopy = new HashMap<Integer, ArrayList<Integer>>();
Iterator it = graph.keySet().iterator();
while(it.hasNext())
{
Integer currentKey = (Integer)it.next();
ArrayList<Integer> currentItemList = graph.get(currentKey);
graphCopy.put(currentKey, new ArrayList<Integer>(currentItemList));
}
return graphCopy;
}
/**
* Reads the Graph used as input for the assignment
* @return A hash that contains each vertice and the list
* of vertices linked to it
*/
private static HashMap<Integer, ArrayList<Integer>> readGraphFromFile()
{
HashMap<Integer, ArrayList<Integer>> graph = new HashMap<Integer, ArrayList<Integer>>();
FileInputStream fstream = null;
try {
fstream = new FileInputStream("C:/Users/srinath/Desktop/Amazon/Design_analysis/kargerMinCut.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null)
{
// process the line
StringTokenizer tokens = new StringTokenizer(line);
ArrayList<Integer> edges = new ArrayList<Integer>();
// first item is the token
Integer node = new Integer(tokens.nextToken());
while(tokens.hasMoreTokens()){
edges.add(new Integer(tokens.nextToken()));
}
graph.put(node, edges);
}
br.close();
}catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}finally {
try {
fstream.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
return graph;
}
}