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.
每次随机选取跨越不同等价类的边,合并后修改相应顶点的等价类属性。最后所有的的点只属于两个等价类。 Initialization: 把所有点存在一个hash表中,点的记号作为key, 点所属的等价类作为value. 此时共有40个等价类。 把所有的边存在一个数组中。去掉重复的。 Iteration: 建一个临时数组存放edges,擦去合并后的self-loop; 修改被合并点的等价类。 每次循环都减少了一个等价类。所以这个iteration 运行38次。 Termination: 扫描原来的边数组,如果头尾两点属于两个等价类,# of cuts + 1 PS : C++和Java都不太会,用perl写的 PS2: 作业+考试总分过了70分了,应该会收到他的certificate of accomplishment |
//定义Edge类
public class Edge {
public int vertex1;
public int vertex2;
public Edge(int v1, int v2) {
this.vertex1 = v1 <= v2 ? v1 : v2;
this.vertex2 = v2 > v1 ? v2 : v1;
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
public class RC_Graph {
public static ArrayList<Edge> edges;
public static HashSet<Integer> vertices;
public static int randomSeed;
public static String PATH = “kargerMinCut.txt”;
public static void main(String args[]) throws FileNotFoundException, IOException {
createGraphFromFile();
int mincut = 100;
for (int i = 0; i < 2; i++) {
//randomSeed = i + 1;
int num_cut = randomContraction(vertices, edges);
if (num_cut < mincut)
mincut = num_cut;
System.out.println(num_cut);
}
System.out.println("The minimum cut in the graph is: " + mincut);
}
public static int randomContraction(HashSet<Integer> vertices, ArrayList<Edge> edges) {
while (vertices.size() > 2) {
Random randIdx = new Random();
//randIdx.setSeed(randomSeed);
int v1 = randIdx.nextInt(200) + 1;
int v2 = randIdx.nextInt(200) + 1;
deleteConnectedEdge(v1, v2, vertices, edges);
contractVertices(v1, v2, vertices, edges);
}
return edges.size();
}
public static void deleteConnectedEdge(int v1, int v2, HashSet<Integer> vertices, ArrayList<Edge> edges) {
Iterator<Edge> it = edges.iterator();
while (it.hasNext()) {
Edge e = it.next();
if (e.vertex1 == v1 && e.vertex2 == v2 ||
e.vertex1 == v2 && e.vertex2 == v1) {
it.remove();
edges.remove(e);
}
}
}
public static void contractVertices(int v1, int v2, HashSet<Integer> vertices, ArrayList<Edge> edges) {
Iterator<Edge> it = edges.iterator();
while (it.hasNext()) {
Edge e = it.next();
if (e.vertex1 == v2)
e.vertex1 = v1;
if (e.vertex2 == v2)
e.vertex2 = v1;
}
vertices.remove(Integer.valueOf(v2));
}
public static void createGraphFromFile() throws FileNotFoundException, IOException {
edges = new ArrayList<Edge>();
vertices = new HashSet<Integer>();
BufferedReader br = new BufferedReader(new FileReader(PATH));
while (true) {
boolean isDuplicate = false;
String data = br.readLine();
if (data == null) break;
data = data.replace("\t", " ");
String[] nums = data.split(" ");
Integer v1 = Integer.parseInt(nums[0]);
vertices.add(v1);
for (int i = 1; i < nums.length; i++) {
Integer v2 = Integer.parseInt(nums[i]);
Edge e = new Edge(v1.intValue(), v2.intValue());
for (Edge edge : edges) {
if (edge.vertex1 == e.vertex1 && edge.vertex2 == e.vertex2
|| edge.vertex1 == e.vertex2 && edge.vertex2 == e.vertex1) {
isDuplicate = true;
}
}
if (!isDuplicate)
edges.add(e);
isDuplicate = false;
}
}
}
}