3

ファイルから表現を読み取り、隣接リストに保存しています。次に、グラフを「graphviz 形式」で出力し、グラフに対して MST アルゴリズムを実行します。最後に、MST を「graphviz 形式」で出力します。私はC++でこれをやっています。

私の主な質問は、アルゴリズムに関するものです。Kruskals アルゴリズムを実装していますが、並べ替え機能が機能していません。

コンパイルすると、次のエラーが発生します。

'void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [ with _RandomAccessIterator = __gnu_cxx::__normal_iterator*, std::vector, std::allocator > > >, _Compare = bool (*)(Edges, Edges)] からインスタンス化'</p>

これが私のコードです:

#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;
}
4

1 に答える 1

2

問題は、graphへの呼び出しのスコープ内にあるkruskalsが globalgraphであり、 として宣言されていることvector<pair<int,Edges> >です。es ではなくes を比較EdgeLessするため、並べ替えには使用できません。EdgeLessEdgespair<int,Edges>

typeという名前のさまざまなローカル変数と一緒graphに、 typeという名前のグローバル変数を使用することは、不必要に混乱を招くと思いますか? 現在のスコープと現在の型を持つこれらの個別の変数がすべて本当に必要な場合は、おそらく、グローバル変数の名前をグローバルであることを示す名前に変更する必要があります。vector<pair<int,Edges> >graphvector<pair<Edges> >

于 2011-11-11T21:34:09.323 に答える