簡體   English   中英

Kruskal的算法(排序)

[英]Kruskal's algorithm (sort)

我正在從文件中讀取表示並將其存儲在鄰接列表中。 然后我以“graphviz格式”輸出圖形並在圖形上執行MST算法。 最后,我以“graphviz格式”輸出MST。 我在C ++中這樣做。

我的主要問題是算法。 我正在實現Kruskals算法,並且sort函數不起作用。

當我編譯它時,我收到此錯誤:

從'void std :: sort(_RandomAccessIterator,_RandomAccessIterator,_Compare)[與_RandomAccessIterator = __gnu_cxx :: __ normal_iterator *,std :: vector,std :: allocator >>>,_Compare = bool(*)(Edges,Edges)]實例化“

這是我的代碼:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <map>
#include <vector>
#include <algorithm>
#include <cstdlib>  
#include <utility>   


using namespace std;


#define egdes pair<int ,int >
#define MAX 9

struct Edges
{
    int weight;
    int first,second;
    char begin,end;
};

    bool EdgeLess(Edges oneE,Edges twoE)
     {
        return oneE.weight < twoE.weight;
     }  

vector<pair<int ,Edges > > graph,MST;
int parent[MAX],total ;


bool openInputFile(ifstream &inFile,char* argv);
bool openOutputFile(ofstream &outFile,char* argv);
void readInputFile(ifstream &inFile,vector<Edges> &graph);
int findSet(int x,int *parent);
void kruskals();
void makeSet();
bool compareEdgW(Edges oneE,Edges twoE);

int main (int argc,char **argv)
{

    ifstream inFile;
    ofstream outFile;
    int u,v,w;
     int nodeCount;
    int edgeCount;
    char nodeName;
    Edges edge;

    vector<Edges> graph;

        cout<<"hey"<<endl;
 if(openInputFile(inFile,argv[1]) && openOutputFile(outFile,argv[2]))
    {
        readInputFile(inFile,graph);

        outFile.close();
    }

    inFile >> nodeCount;
    inFile >> edgeCount;

    for( int i = 0;i < edgeCount; i++)
    {
        cin >> u >> v >> w ; 
//        graph.push_back(pair<int ,Edges >(w,edges(u,v)));
        graph.push_back(edge);
    }

    kruskals();

    makeSet();  
    return 0;
}


bool openInputFile(ifstream &inFile,char* argv)
{
    inFile.open("input.txt");
    if(!inFile)
    {
        cout<<"Oops!Input file did not open.\n";
        cout<<"Terminating the program.\n";
        return false;
    }
    return true;
}
bool openOutputFile(ofstream &outFile,char* argv)
{
    outFile.open("output.gv");
    if(!outFile)
    {
        cout<<"Hey!Oops!Input file did not open.\n";
        cout<<"Terminating the program.\n";
        return false;
    }
    return true;
}
void readInputFile(ifstream &inFile,vector<Edges> &graph)
{
    int nodeCount;
    Edges edge;

    inFile >>nodeCount;

    char nodeName;

    for (int i = 0;i < nodeCount;i++)
    {
        inFile >> nodeName;
//        graph.insert(make_pair(nodeName,vector<Edges>()));

    }
    int edgeCount;
    inFile >> edgeCount;

    for (int i = 0;i < edgeCount;i++)
    {
  //      inFile >>nodeName;
        Edges edge;
        inFile >> edge.begin;
        inFile >> edge.weight;
        inFile >> edge.end;
        graph.push_back(edge);

    }

}
int findSet(int x,int *parent)
{
    if( x != parent[x])
        parent [x] = findSet(parent[x],parent);
    return parent[x];

}

void kruskals()
{
    int pu;
    int pv;
    int edgeCount;

    sort(graph.begin(),graph.end (),EdgeLess);
    for (int i = 0;i < edgeCount; i++) 
    {
        pu = findSet(graph[i].second.first,parent);
        pv = findSet(graph[i].second.second,parent);
        if(pu != pv)
        {   
            MST.push_back(graph[i]);
            total += graph[i].first;
            parent[pu] = parent[pv];
        }
    }
}
void makeSet()
{
    unsigned long sizeNum;
    sizeNum = MST.size();
    for(int i = 0;i < sizeNum;i++)
    {
        cout<< MST[i].second.first <<endl;
        cout<< MST[i].second.second <<endl;
        cout<< MST[i].first <<endl;
    }
        cout << total <<endl;
}

的問題是,該graph那是在范圍在調用kruskals是全局graph ,聲明為vector<pair<int,Edges> > 因此,您無法使用EdgeLess進行排序,因為EdgeLess比較Edges es,而不是pair<int,Edges> es。

我可能會建議有一個名為graph的全局變量,它具有類型vector<pair<int,Edges> >以及名為graph各種局部變量,它們具有類型vector<pair<Edges> > ,這是不必要的混淆。 如果您確實需要所有這些不同的變量,以及它們當前的范圍和當前類型,那么您應該將全局變量重命名為指示它是全局變量的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM