简体   繁体   English

C ++-Kruskal算法STL

[英]C++ - Kruskal algorithm STL

Based on video I wrote Kruskal's algorithm. 根据视频,我编写了Kruskal算法。 But I have "little" problem - instead of finding lowest weights it finds me highest. 但是我有一个“小”问题-而不是找到最低的权重,而是找到最高的权重。 This may sound funny, but I can't find where I made mistake. 这听起来可能很有趣,但是我找不到我犯错的地方。

collection::collection(int vert){ coll = new SNode[vert]; }
collection::~collection(){}
void collection::Create(int vert)
{
    coll[vert].up = vert;
    coll[vert].rank = 0;
}
int collection::Find(int vert)
{
    /*if (coll[vert].up != vert) coll[vert].up = Find(coll[vert].up);
    return coll[vert].up;*/
    if (coll[vert].up == vert) return coll[vert].up;
    else Find(coll[vert].up);
}
void collection::Union(graf::Edges e)
{
    int home, dest;
    home = Find(e.v1);
    dest = Find(e.v2);
    if (home != dest){
        if (coll[home].rank > coll[dest].rank){ coll[dest].up = home; }
        else if (coll[home].rank < coll[dest].rank){ coll[home].up = dest; }
        else{
            coll[home].up = dest;             
            //if (coll[home].rank == coll[dest].rank) 
                coll[dest].rank++;
        }
    }
}

And main algorithm. 和主要算法。 Weights are kept in two dimensional matrix called 'weightmat'. 权重保存在称为“ weightmat”的二维矩阵中。 Vertex is quantity of vertices, Edges is struct with variables v1,v2,weight. 顶点是顶点数量,边是具有变量v1,v2,weight的结构。 Using this struct I create array of edges. 使用此结构,我创建了边缘数组。 :

collection newcollection(vertex);

        struct CompareMat{
            bool operator()(Edges &node1, Edges &node2){
                if (node1.weight < node2.weight) return true;
                else return false;
            }
        };

        priority_queue<Edges, vector<Edges>, CompareMat> EdgesQueue;
        Edges temp;
        Edges* edges = new Edges[edge];
        Edges* MSTTree = new Edges[vertex-1];

        for (int i = 0; i < vertex; i++){
            for (int j = 0; j < vertex; j++)
            {
                if (nbhmat[i][j] != 0){
                    edges[i].v1 = i;
                    edges[i].v2 = j;
                    edges[i].weight = weightmat[i][j];
                }
            }
            EdgesQueue.push(edges[i]);
        }

        for (int i = 0; i < vertex; i++){
            newcollection.Create(i);
        }

        for (int i = 1; i < vertex; i++)          
        {
            do
            {
                temp = EdgesQueue.top();             
                EdgesQueue.pop();                    
            } while (newcollection.Find(temp.v1) == newcollection.Find(temp.v2));
            MSTTree[i - 1] = temp;
            newcollection.Union(temp);
        }

        cout << endl << endl << "Kruskal's algorithm for matrix:" << endl;
        for (int i = 0; i < vertex - 1; i++){
            cout << MSTTree[i].v1 << " " << MSTTree[i].v2 << " weight " << MSTTree[i].weight << endl;
        }

priority_queue has the biggest element on its top (yes, with the comparator class being equivalent to less ), but you need the smallest edges first. priority_queue在其顶部具有最大的元素(是的,比较器类等于less ),但是首先需要最小的边缘。 You need to invert your CompareMat::operator() comparison. 您需要反转CompareMat::operator()比较。

Two more notes. 还有两个音符。

Firstly, in CompareMat::operator() you can return comparison result directly: 首先,在CompareMat::operator()您可以直接返回比较结果:

//return node1.weight < node2.weight; // your version
return node1.weight > node2.weight;  // correct version

Secondly, why do you need a priority queue? 其次,为什么需要优先级队列? A simple sort will suffice, because you do not seem to change your edges. 一个简单的排序就足够了,因为您似乎并没有改变自己的优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM