我正在从文件中读取表示并将其存储在邻接列表中。然后我以“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;
}
答案 0 :(得分:2)
问题在于graph
调用范围内的kruskals
是全局graph
,声明为vector<pair<int,Edges> >
。因此,您无法使用EdgeLess
对其进行排序,因为EdgeLess
会比较Edges
es,而非pair<int,Edges>
es。
我可能会建议让名为graph
的全局变量具有类型vector<pair<int,Edges> >
以及名为graph
且类型为vector<pair<Edges> >
的各种局部变量,这是不必要的混淆吗?如果确实需要所有这些不同的变量,以及它们当前的范围和当前类型,那么您应该将全局变量重命名为指示它是全局的。