2011-11-11 1 views
3

파일에서 표현을 읽고 인접 목록에 저장하고 있습니다. 그런 다음 "graphviz 형식"으로 그래프를 출력하고 그래프에서 MST 알고리즘을 수행합니다. 마지막으로 MST를 "graphviz 형식"으로 출력합니다. 나는 C++에서 이것을하고있다.Kruskal의 알고리즘 (정렬)

내 주요 질문은 알고리즘입니다. Kruskals 알고리즘을 구현 중이며 정렬 함수가 작동하지 않습니다. 여기

instantiated from ‘void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _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; 
} 
+1

어떻게 작동하지 않습니까? 대부분의 SO 사용자는 한 줄 문제 일 수있는 디버깅을 위해 모든 코드를 읽지 않을 것입니다. – Alex

+0

전체 오류 메시지를 게시하십시오. – Erbureth

답변

2

문제는 kruskals에 호출 범위 년대 글로벌 때문이다 vector<pair<int,Edges> >으로 선언되었습니다. EdgeLesspair<int,Edges>이 아닌 Edges입니다. 따라서 EdgeLess을 사용하여 정렬 할 수 없습니다.

나는 불필요 유형 vector<pair<Edges> >이 라는 이름의 다양한 지역 변수와 함께 vector<pair<int,Edges> >을 입력있다 라는 이름의 전역 변수를 가지고 혼란 제안 할 수 있음? 실제로에 이러한 모든 고유 변수와 현재 범위 및 현재 유형이 필요한 경우 전역 변수의 이름을 전역 변수임을 나타내는 값으로 변경해야합니다.

+0

그래서 전역 변수로 명명 된 그래프의 이름을 바꿀 것을 제안합니다. – kay