Union Find解决旅行推销员

时间:2013-10-13 17:03:19

标签: algorithm traveling-salesman union-find

我必须使用union find算法来解决旅行商问题。除了我刚刚发现的一个问题外,我做得很好。当它处理每个边时,它将检查一个循环,这是用整个父数组的东西完成的。问题是,当它到达最后一个边缘时我需要添加以完成问题,因为它在技术上是一个循环,它不会添加边缘,因此路径无法完成。如何区分无用的循环和指示我们完成的循环?

这是我到目前为止的代码

private int[] parent; //parent of each vertex
private int[] connection; //number of edges coming from a given vertex
private int[] subsize; //size of each subtree
boolean donepath;

public void GreedyAlgo(){
    ArrayList<Edge> newedges = new ArrayList<Edge>();
    for(int i = 0; i<graph.realedge.size();i++){
        if(donepath) break;
        Edge e= graph.realedge.get(i);
        int x = e.in1;
        int y = e.in2;


        if(unionFind(x,y) && !thirdEdge(x,y)){

            newedges.add(e);



        }

        else{

        }

    }
}


 public int findParent(int i){
    if (parent[i] != i)
       return findParent(parent[i]);
    return parent[i];

}

public boolean unionFind(int x, int y){
     int xx = findParent(x);
     int yy = findParent(y);

    if(xx == yy){

            if(subsize[xx]== n){
                donepath = true;
                return true;
            }
            return false;
    }
     else{

        if( subsize[xx] < subsize[yy]){
             parent[xx] = yy;
                              subsize[yy]+=subsize[xx];
         }
        else if( subsize[xx] >  subsize[yy]){
             parent[yy] = xx;  subsize[xx]+=subsize[yy];
         }
         else{
             parent[yy] = xx;
             subsize[xx]+=subsize[yy];
         } 
        connection[x]++;
        connection[y]++;

     }


    return true;


}

public boolean makesCycle(int x, int y){
        int xx = findParent(x);
        int yy = findParent(y);

        if(xx == yy){

            return true;
        }

    return false;
}

以下是按顺序经过的边缘

0-0,
1-1,
2-2,
3-3,
4-4,
0-4 should get added,
2-3 should get added,
3-2,
4-0,
0-1 should get added,
0-2 ,
0-3,
1-0,
1-4,
2-0,
3-0,
4-1,
1-3 should get added,
3-1,
2-4 should get added......but doesnt,
3-4,
4-2,
4-3,
1-2,
2-1,

1 个答案:

答案 0 :(得分:1)

如何跟踪每组的大小?

然后,当进行联合时,如果两者的根相同(即一个循环)并且根的大小等于问题中所有点的总和,则包括该边并停止,否则按照惯例继续。

警告:

请注意,简单的Union-Find实施可能会以minimum spanning tree而不是hamiltonian cycle结束。你需要确保选择合适的边缘 - 我会假设你已经想到了这一点,或者,如果没有,我会把它留给你。

<强>精化:

问题的Union应如下所示:(源自Wikipedia
(返回falsetrue以指示是否应添加边缘

boolean Union(x, y)
   xRoot = Find(x)
   yRoot = Find(y)
   if xRoot == yRoot
      return false
   // merge xRoot and yRoot
   xRoot.parent = yRoot
   return true

(正确的合并(效率)稍微复杂一点 - 你应该比较深度并选择最深的一个作为父级,详见维基百科)

现在,我的建议:

为每个节点创建一个size变量,初始化为1,然后是Union函数:

boolean Union(x, y)
   xRoot = Find(x)
   yRoot = Find(y)

   if xRoot == yRoot
      // check if it's the last node
      if xRoot.size == pointCount
         done = true
         return true
      else
         return false

   // merge xRoot and yRoot
   xRoot.parent = yRoot
   yRoot.size += xRoot.size
   return true

示例:

点数:

1---2
|\  |
| \ |
|  \|
4---3

有4个点,因此pointCount = 4

开始:( size出现在节点下)

1  2  3  4
1  1  1  1

联盟12

1  3  4
2  1  1
|
2
1

联盟32

3  4
3  1
|
1
2
|
2
1

联盟31

公共根是3(因此xRoot == yRoot为真)和xRoot.size(3)!= pointCount(4),因此我们返回false(不添加边)。< / p>

联盟34

4
4
|
3
3
|
1
2
|
2
1

联盟41

公共根是4(因此xRoot == yRoot是真的)和xRoot.size(4) == pointCount(4),因此我们返回true(添加边)并设置标志表明我们已经完成了。