Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 克鲁斯卡尔';s算法(排序)_C++_Algorithm_Sorting_Graph_Kruskals Algorithm - Fatal编程技术网

C++ 克鲁斯卡尔';s算法(排序)

C++ 克鲁斯卡尔';s算法(排序),c++,algorithm,sorting,graph,kruskals-algorithm,C++,Algorithm,Sorting,Graph,Kruskals Algorithm,我正在从文件中读取表示并将其存储在邻接列表中。然后我以“graphviz格式”输出图形,并在图形上执行MST算法。 最后,我以“graphviz格式”输出MST。我在C++中做这个。< /P> 我的主要问题是算法。我正在实现Kruskals算法,但排序函数不起作用 当我编译它时,会出现以下错误: 从“void std::sort(_RandomAccessIterator,_RandomAccessIterator,_Compare)[with _RandomAccessIterator=_gn

我正在从文件中读取表示并将其存储在邻接列表中。然后我以“graphviz格式”输出图形,并在图形上执行MST算法。 最后,我以“graphviz格式”输出MST。我在C++中做这个。< /P> 我的主要问题是算法。我正在实现Kruskals算法,但排序函数不起作用

当我编译它时,会出现以下错误:

从“void std::sort(_RandomAccessIterator,_RandomAccessIterator,_Compare)[with _RandomAccessIterator=_gnu_cxx::_normal_iterator*,std::vector,std::allocator>>,_Compare=bool(*)(边,边)]实例化

这是我的密码:

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#定义egdes对
#定义最大值9
结构边
{
整数权重;
int第一,第二;
开始,结束;
};
bool无边(边一E,边二E)
{
返回一个E.weight<两个E.weight;
}  
矢量图;
整数父项[MAX],总计;
bool openInputFile(ifstream&infle,char*argv);
bool openOutputFile(流和输出文件,字符*argv);
void readInputFile(ifstream和infle、vector和graph);
int findSet(int x,int*parent);
void kruskals();
void makeSet();
bool compareEdgW(边one,边twone);
int main(int argc,字符**argv)
{
河流充填;
出流孔的直径;
国际u,v,w;
int nodeCount;
int edgeCount;
焦节点胺;
边缘;
矢量图;
cout edgeCount;
对于(int i=0;i>u>>v>>w;
//图。推回(对(w,边(u,v));
图。推回(边缘);
}
kruskals();
makeSet();
返回0;
}
bool openInputFile(ifstream&infle,char*argv)
{
infle.open(“input.txt”);
如果(!infle)
{
coutnodeName;
边缘;
infle>>edge.begin;
infle>>edge.weight;
infle>>edge.end;
图。推回(边缘);
}
}
int findSet(int x,int*parent)
{
如果(x!=父项[x])
父[x]=findSet(父[x],父);
返回父项[x];
}
void kruskals()
{
int pu;
int pv;
int edgeCount;
排序(graph.begin()、graph.end()、edgess);
对于(int i=0;i问题是调用
kruskals
的范围内的
graph
是全局
graph
,声明为
vector
。因此不能使用
EdgeLess
对其排序,因为
EdgeLess
比较
es,而不是
pair
es


我是否可以建议,将一个名为
graph
的全局变量(类型为
vector
)与名为
graph
的各种局部变量(类型为
vector
)放在一起是不必要的混淆?如果您确实需要所有这些不同的变量,以及它们的当前范围和当前类型,那么您应该请将全局变量重新命名为表明它是全局变量。

如何工作?大多数用户不会通读所有代码来调试可能出现的单行问题。请发布整个错误消息。