std::pair<std::vector<NodeID>, EdgeWeight> TSP(Graph* G){ NodeID* n = new NodeID[G->size()]; NodeID* mi = new NodeID[G->size()]; EdgeWeight total = 0; EdgeWeight min = -1; for(int i = 0; i < G->size(); i++){ n[i] = i; }; /*NodeID** nn = new NodeID*[G->size()]; for(int i = 0; i < G->size(); i++){ nn[i] = new NodeID[G->size()]; };*/ EList l; NWPair p; bool found; for(int i = 0; i < G->size(); i++){ total = 0; for(int i = 0; i < G->size(); i++){ n[i] = -1; }; n[0] = i; l = G->getAdj(i); l.sort(sortWeight); p = l.front(); total = total + p.second; n[1] = p.first; for(int j = 0; j < G->size() - 2; j++){ l = G->getAdj(p.first); l.sort(sortWeight); p = l.front(); found = true; while(found){ found = false; for(int q = 0; q < G->size(); q++){ if(p.first == n[q]){ l.pop_front(); p = l.front(); found = true; }; }; }; p = l.front(); total = total + p.second; n[j+2] = p.first; }; total = total + G->weight(n[0],n[G->size()-1]); if(min == -1 || min > total){ min = total; for(int i = 0; i < G->size(); i++){ mi[i] = n[i]; }; }; }; /*do { for(int i = 0; i < (G->size() - 1); i++){ total = total + G->weight(n[i],n[i+1]); }; total = total + G->weight(n[G->size()-1], n[0]); if(min == -1 || min > total){ min = total; for(int i = 0; i < G->size(); i++){ mi[i] = n[i]; }; }; total = 0; } while ( next_permutation (n,n+G->size()) );*/ //vector<NodeID> wut (*mi, *mi + sizeof(*mi) / sizeof(int) ); //NodeID in[] = {0,3,4,1,2}; //vector<NodeID> wut2 (in, in + sizeof(in) / sizeof(int) ); vector<NodeID> wut3; for(int i = 0; i < G->size(); i++){ wut3.push_back(mi[i]); }; std::pair<std::vector<NodeID>, EdgeWeight> omg (wut3,min); /*NodeID in[] = {0,1,2,3,4}; vector<NodeID> wut (in, in + sizeof(in) / sizeof(int) ); //vector<NodeID> wut = NULL; //vector<NodeID> hu; cout << "what" << G->size() << endl; cout << "no where " << endl; EdgeWeight wtf = 6.7; std::pair<std::vector<NodeID>, EdgeWeight> omg (wut,wtf);*/ return omg; };
/** * Sort the edits in the provided list. */ void Edit::sort(EList<Edit>& edits) { edits.sort(); // simple! }