我一直在工作&试着解决这个问题一段时间了。实际上已经有好几个星期但是我用尽了可能的解决方案或修改。因此该算法是随机化Karger最小割算法&我的实现如下:
算法:
用于无向图。每个顶点都存储为hashmap&的关键字。它的相邻顶点作为arraylist存储在hashmap的值中,例如
如果测试用例是:
1 2 6
2 1 3 4 5
3 2 4
4 2 3 5
5 2 4 6
6 1 5
其中第一列是键&与它们相邻的数字是它的值(arraylist)。
我的算法是
删除“U”(键),例如,如果“U”为6,则更新的顶点将如下所示:
1 2 6 2
2 1 3 4 5 1 5
3 2 4
4 2 3 5
5 2 4 6 2
因此,当“i”将具有唯一编号时,算法将终止(通过将“i”列表添加到Set来完成)。例如:
2 6 6
6 2 2
这是给定图表的2个最小切割。
代码:
我在java中对上述算法的代码如下:
package practice;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class MinCut {
static Map <Integer,ArrayList <Integer>> vertices=new HashMap <Integer,ArrayList <Integer>>() ;
static Random random= new Random ();
static int SIZE=0;
static TreeSet <Integer> minSize= new TreeSet <Integer> ();
static int reps=0;
static ArrayList <Integer> uTracker= new ArrayList <Integer> ();
public static void main(String[] args) {
int reps2=0;
for (int i=0;i<=10;i++){
minCutCalc();
TreeSet <Integer> trackSet= new TreeSet <Integer> ();
if (!uTracker.isEmpty()){
Collections.sort(uTracker);
for (int o=0;o<uTracker.size();o++){
System.out.println("uTracker: "+uTracker.get(o));
if (!trackSet.add(uTracker.get(o))){
reps2++;
}
}
//to check "U"s in every run
uTracker.clear();
}
}
//prints the lowest number in the set hence the minimum cut
System.out.println("The attempted minimum cut is: "+minSize.first());
System.out.println("FINAL REPS: "+reps2);
}
private static void minCutCalc() {
readMap();
int i=0;
i=selectVertex(1);
//for testing purposes
System.out.println(" \"i\" is: "+i);
if (vertices.get(i) != null){
Set <Integer> tempSet= new LinkedHashSet <Integer> ();
while (vertices.get(i).size()>2){
/*to remove any instances of "i" copied into list from other vertices
* to avoid getting "i" selected as "U" in random numbers as then "U"
* will be deleted hence showing result as "null"
*/
for (int l=0;l<vertices.get(i).size();l++){
for (int c=0;c<vertices.get(i).size();c++){
if (vertices.get(i).get(c)==i){
int index=vertices.get(i).indexOf(i);
System.out.println("Yes it contains "+i+": "+vertices.get(i).get(index));
vertices.get(i).remove(index);
}
}
}
//for testing purposes
System.out.println("\"i\" is: "+i);
for (int j=0;j<vertices.get(i).size();j++){
System.out.println("\n"+"LIST DISPLAY: "+vertices.get(i).get(j));
}
if (!tempSet.isEmpty()){
tempSet.clear();
}
tempSet.addAll(vertices.get(i));
//if there is only one occurrence of a number in the list
if (tempSet.size()==1){
break;
}
int U=selectRandom(i,vertices);
//for testing purposes
System.out.println("PRINT u: "+U);
//to check if unique "U"s are selected each time
uTracker.add(U);
for (int n=0;n<vertices.get(U).size();n++){
System.out.println("\"U\" List: "+vertices.get(U).get(n));
}
//merging "U"'s vertices to "i"
if (vertices.containsKey(U)){
vertices.get(i).addAll(vertices.get(U));
for (int y=0;y<vertices.get(U).size();y++){
System.out.println(vertices.get(U).get(y));
//cross referencing "i" to rest of the vertices in "U"'s list
if (vertices.get(U).get(y)!=i){
vertices.get(vertices.get(U).get(y)).add(i);
}
}
}
vertices.remove(U);
//if any of the vertices consists of "U" as its edge it will be deleted
for (int t=1;t<=SIZE;t++){
if (vertices.containsKey(t)){
for (int y=0;y<vertices.get(t).size();y++){
for (int z=0;z<vertices.get(t).size();z++){
if (vertices.get(t).get(z)==U){
int index=vertices.get(t).indexOf(U);
vertices.get(t).remove(index);
}
}
}
}
}
//for testing purposes
System.out.println("\"i\" is: "+i);
for (int j=0;j<vertices.get(i).size();j++){
System.out.println("LIST \"AFTER\" DISPLAY: "+vertices.get(i).get(j));
}
//null check
if (vertices.get(i)==null){
System.out.println("This is null: "+i);
break;
}
}
}
//to check the final result
for (int o=1;o<=SIZE;o++){
System.out.println(" SHOW at "+o+" index: "+vertices.get(o));
if (vertices.get(o)!=null){
//minimum cuts (size of current list) are added to a set
minSize.add(vertices.get(o).size());
}
}
System.out.println("Total Number of Repititions: "+reps);
}
private static void readMap() {
try {
FileReader file= new FileReader("C:\\xyz\\Desktop\\testMinCut.txt");
BufferedReader reader= new BufferedReader(file);
String line="";
while ((line=reader.readLine())!=null){
String [] lineArr;
lineArr=line.split("\t");
int vert1=Integer.parseInt(lineArr[0]);
vertices.put(vert1,new ArrayList <Integer> ());
for (int p=1;p<lineArr.length;p++){
int vert2=Integer.parseInt(lineArr[p]);
vertices.get(vert1).add(vert2);
}
}
SIZE=vertices.size();
} catch (FileNotFoundException e) {
System.err.println(e.toString());
} catch (IOException e) {
System.err.println(e.toString());
}
}
private static int selectVertex(int i) {
LinkedHashSet <Integer> storer= new LinkedHashSet <Integer> ();
for (int s=1;s<=SIZE;s++){
if (vertices.get(s)!=null){
if (!storer.isEmpty()){
storer.clear();
}
storer.addAll(vertices.get(s));
if (storer.size()>2){
i=s;
break;
}
else {
i=0;
}
}
}
return i;
}
private static int selectRandom(int i, Map<Integer, ArrayList<Integer>> vertices) {
int u;
int U = 0;
LinkedHashSet <Integer> tempSet2= new LinkedHashSet <Integer> ();
tempSet2.addAll(vertices.get(i));
u=random.nextInt(tempSet2.size());
Set <Integer> uSet=new HashSet <Integer> ();
Set <Integer> uSet2=new HashSet <Integer> ();
//to reassure unique "u" is selected each time
if (!uSet.isEmpty() && uSet.contains(u)){
u=random.nextInt(tempSet2.size());
reps++;
}
uSet.add(u);
U=vertices.get(i).get(u);
//ensuring unique "U" is selected
if (uSet2.contains(U)){
u=random.nextInt(tempSet2.size());
U=vertices.get(i).get(u);
reps++;
}
uSet2.add(U);
tempSet2.clear();
return U;
}
}
问题:
我面临的问题是,这个算法对于我遇到的几乎所有测试用例都非常好,除了一个由200个顶点组成的非常大的测试用例。正确的答案应该是17但是我继续把答案称为“20”。我跟踪了所有被选中的“U”。显然它们都是独一无二的,没有任何重复和我一直得到答案“20”。有什么建议吗?再次感谢。测试用例的链接是:
http://spark-public.s3.amazonaws.com/algo1/programming_prob/kargerMinCut.txt
N.B:
这不是一个家庭作业,而是我正在研究的一个练习题,我在课程中的在线课程(算法设计和分析)中看到了这个问题。课程结束了。非常感谢你提前。我再次问这个问题,因为我第一次得不到答案。我很感激任何提供的帮助,因为我说这个问题对我造成了影响,因为我已经工作了很长一段时间。
答案 0 :(得分:2)
所以我设法让它工作&amp;得到正确的答案。问题在于随机边缘选择。因为我第一次选择一个顶点(有两个以上的边)和&amp;然后是来自该顶点的边缘,这是错误的方法,因为并非所有边缘均匀地分布在顶点之间。所以这是我改进的算法:
保留集合
中所有边的列表希望这会对某人有所帮助。