TEST_F(GraphCreatorFileTest, testCreateKTree) { Graph::Graph *mg; mg = creator->initialize_ktree(100, 50); EXPECT_EQ(100, mg->get_num_nodes()) ; }
TEST_F(GraphCreatorFileTest, testGraph) { Graph::Graph *g = creator->create_graph(); EXPECT_EQ(128, g->get_num_nodes()); EXPECT_EQ(1471, g->get_num_edges()); vector<Graph::Node> n; n = g->get_nodes(); EXPECT_EQ(128, n.size()); list<int> nbrlist = n[35].get_nbrs(); vector<int> nbrs(nbrlist.begin(), nbrlist.end()); EXPECT_EQ(75, nbrs[20]); nbrlist = n[127].get_nbrs(); nbrs.clear(); nbrs.insert(nbrs.end(), nbrlist.begin(), nbrlist.end()); EXPECT_EQ(111, nbrs[2]); nbrlist = n[0].get_nbrs(); nbrs.clear(); nbrs.insert(nbrs.end(), nbrlist.begin(), nbrlist.end()); EXPECT_EQ(1, nbrs[0]); EXPECT_EQ(64, nbrs[6l]); EXPECT_EQ(8, nbrs[3l]); }
void remove_all_edges(int i){ list<int>::iterator it; Graph::Node *n = mg->get_node(i); list<int> nbrs = n->get_nbrs(); for(it = nbrs.begin(); it != nbrs.end(); ++it){ mg->remove_edge(i, *it); } }
int get_lowest_degree() { int n = mg->get_num_nodes(); int min = INT_MAX; int i = 0; for(i = 0; i < n; i++) { if(min > mg->get_degree(i)) { min = mg->get_degree(i); } } return min; }
int get_highest_degree() { int n = mg->get_num_nodes(); int max = 0; int i = 0; for(i = 0; i < n; i++) { if(max < mg->get_degree(i)) { max = mg->get_degree(i); } } return max; }
/****************************************************************************** * Create Cluster Graph * ******************************************************************************/ static void create_cluster_graph(Graph::Graph& g, std::vector<int> clusters, int nCluster, std::vector<WgtType>& radii, Graph::Graph& cg) { // Steps // 1. get the linkage between cluster // 2. link the edge using namespace std; vector< vector<int> > cls_connection(nCluster, vector<int>(nCluster, 0)); vector<int> cls_nodes; vector<int> nbors; // Step 1 for (int c=0; c<nCluster; ++c) { cls_nodes.clear(); cls_nodes.resize(0); for (int i=0; i<g.get_num_vtxs(); ++i) if (clusters.at(i) == c) cls_nodes.push_back(i); for (int i=0; i<cls_nodes.size(); ++i) { nbors = g.adj(cls_nodes.at(i)); for (int n=0; n<nbors.size(); ++n) { if (clusters.at(nbors.at(n)) != c) cls_connection.at(c).at(clusters.at(nbors.at(n))) += 1; } } } // Step 2 for (int i=0; i<nCluster; ++i) { for (int j=i+1; j<nCluster; ++j) if (cls_connection.at(i).at(j) != 0) cg.add_edge(i, j, radii.at(i) + radii.at(j)); } }
void NearestNeighborGraph(const vector<Vector3>& pc,int k,Graph::Graph<int,int>& G) { vector<Vector> copy(pc.size()); for(size_t i=0;i<copy.size();i++) { copy[i].resize(3); pc[i].get(copy[i]); } G.Resize(pc.size()); for(size_t i=0;i<pc.size();i++) G.nodes[i] = (int)i; KDTree* tree = KDTree::Create(copy,3,pc.size()); vector<Real> dist(k); vector<int> inds(k); for(size_t i=0;i<pc.size();i++) { tree->KClosestPoints(copy[i],k,&dist[0],&inds[0]); for(int j=0;j<k;j++) { if(inds[j] == (int)i) continue; G.AddEdge((int)i,inds[j],0); } } }
int main(int argc, char **argv){ //usage check if((argc == 1) || ((argc == 2) && (strcmp(argv[1],"-h") == 0)) || ((argc == 2) && (strcmp(argv[1], "--help") == 0))){ usage(argv[0]); exit(-1); } Graph::Graph *g; clock_t begin, end; Graph::GraphProperties prop; Graph::GraphReader ngr; //create the graph object g = new Graph::Graph(); //read the graph from the filename, assume it is an edgelist ngr.read_graph(g, argv[1], "Edge", false); //ngr.read_graph(g, argv[1], "ADJLIST", false); printf("Read %d vertices and %d edges\n", g->get_num_nodes(), g->get_num_edges()); printf("Simplifying graph\n"); begin = clock(); prop.make_simple(g); //remove self loops and duplicate edges end = clock(); printf("Time: %f\n", double(end - begin) / CLOCKS_PER_SEC); //compute normalized expansion begin = clock(); vector<double> norm_hops; prop.expansion(g, norm_hops); end = clock(); printf("Alg Time (expansion): %f\n", double(end - begin) / CLOCKS_PER_SEC); } // main
static void force_direct_with_torque(char* outfilePath, Graph::Graph& g, Graph::Graph& cg, std::vector< std::pair<int, int> > c_edges, DenseMat& cluster_dist_mat, std::vector<int>& clusters, std::vector< std::vector<CoordType> >& center_coord, std::vector< WgtType >& radii, std::vector< std::vector<CoordType> >& coord, int iteration, double initStep) { // Iteration Steps // 1. calculate repulsive displacement // 2. calculate attractive displacement // 3. calculate rotate angle // 4. shift and rotate the cluster nodes and corresponding nodes using namespace std; using namespace boost::posix_time; // algorithm declaration double step = initStep; double curr_energy = INFINITY_ENERGY; double pre_energy; // for iteration dumping fstream fo; // [modified!] clusters_nodes vector<VtxType> cluster_nodes; // id: vtx id of clustered graph // val: vtx id in whole graph // force equilibrium declaration vector< pair<WgtType, WgtType> > node_disp(cg.get_num_vtxs()); vector< WgtType > rotate_angles(cg.get_num_vtxs()); pair<WgtType, WgtType> pos_diff; double rep_force; double att_force; double disp_x; double disp_y; double disp_val; int e_u; // u in c-edge (u, v) int e_v; // v in c-edge (u, v) double disp_u_x; double disp_u_y; double disp_v_x; double disp_v_y; CoordType c_x; // center of u's x coord CoordType c_y; CoordType new_x; // relative to center nodes' x coordinates CoordType new_y; // relative to center nodes' x coordinates double angle; // perform rotation for current node // dump before force process fo.open("out/"+string(outfilePath)+"_start.coord", fstream::out); for (int i=0; i<coord.size(); ++i) { fo << coord.at(i).at(0) << " " << coord.at(i).at(1) << endl; } fo.close(); // center coord fo.open("out/"+string(outfilePath)+"_start.center", fstream::out); for (int i=0; i<center_coord.size(); ++i) { fo << center_coord.at(i).at(0) << " " << center_coord.at(i).at(1) << endl; } fo.close(); // angle fo.open("out/"+string(outfilePath)+"_start.angle", fstream::out); for (int i=0; i<rotate_angles.size(); ++i) { fo << rotate_angles.at(i) << endl; } fo.close(); for (int t=0; t<iteration; t++) { pre_energy = curr_energy; curr_energy = 0; // step 1: repulsive force placement for (int i=0; i<cg.get_num_vtxs(); ++i) { node_disp.at(i) = make_pair(0, 0); for (int j=0; j<cg.get_num_vtxs(); ++j) { if (i != j) { pos_diff = make_pair(center_coord.at(i).at(0)-center_coord.at(j).at(0), center_coord.at(i).at(1)-center_coord.at(j).at(1)); rep_force = repulsive_force(pos_diff, radii.at(i), radii.at(j)); disp_x = node_disp.at(i).first + pos_diff.first/pos_diff_dist(pos_diff)*rep_force; disp_y = node_disp.at(i).second + pos_diff.second/pos_diff_dist(pos_diff)*rep_force; node_disp.at(i) = make_pair(disp_x+node_disp.at(i).first, disp_y+node_disp.at(i).second); if (t==0) { cout << "pos diff of cv_" << i << " = " << pos_diff.first << ", " << pos_diff.second << endl; cout << "repulsive_force of cv_" << i << " = " << rep_force << endl; cout << "disp = " << disp_x << ", " << disp_y << endl; cout << "node disp = " << node_disp.at(i).first << ", " << node_disp.at(i).second << endl; } } } } // step 2: attractive force placement for (int e=0; e<c_edges.size(); ++e) { e_u = c_edges.at(e).first; e_v = c_edges.at(e).second; // \delta <- e.u.pos - e.v.pos pos_diff = make_pair(center_coord.at(e_u).at(0)-center_coord.at(e_v).at(0), center_coord.at(e_u).at(1)-center_coord.at(e_v).at(1)); att_force = attractive_force(pos_diff, radii.at(e_u), radii.at(e_v)); // e.u.disp <- e.u.disp - unit-direction * att_force(abs(\delta)) disp_u_x = node_disp.at(e_u).first - pos_diff.first/pos_diff_dist(pos_diff)*att_force; disp_u_y = node_disp.at(e_u).second - pos_diff.second/pos_diff_dist(pos_diff)*att_force; node_disp.at(e_u) = make_pair(disp_u_x+node_disp.at(e_u).first, disp_u_y+node_disp.at(e_u).second); // e.v.disp <- e.v.disp - unit-direction * att_force(abs(\delta)) disp_v_x = node_disp.at(e_v).first + pos_diff.first/pos_diff_dist(pos_diff)*att_force; disp_v_y = node_disp.at(e_v).second + pos_diff.second/pos_diff_dist(pos_diff)*att_force; node_disp.at(e_v) = make_pair(disp_v_x+node_disp.at(e_v).first, disp_v_y+node_disp.at(e_v).second); if (t==0) { cout << "pos diff of cv_" << e << " = " << pos_diff.first << ", " << pos_diff.second << endl; cout << "attractive_force of cv_" << e << " = " << att_force << endl; cout << "disp = " << disp_v_x << ", " << disp_v_y << endl; } } // step 3: torque equilibrium fill(rotate_angles.begin(), rotate_angles.end(), 0); for (int i=0; i<cg.get_num_vtxs(); ++i) { cluster_nodes.resize(0); for (int c=0; c<clusters.size(); ++c) if (clusters.at(c) == i) cluster_nodes.push_back(c); calculate_rotate_angle(g, i, cluster_nodes, clusters, coord, center_coord, radii, rotate_angles); } // step 4: shift and rotate the cluster nodes and corresponding nodes for (int i=0; i<cg.get_num_vtxs(); ++i) { cluster_nodes.resize(0); for (int c=0; c<clusters.size(); ++c) if (clusters.at(c) == i) cluster_nodes.push_back(c); disp_val = sqrt(pow(node_disp.at(i).first, 2) + pow(node_disp.at(i).second, 2)); c_x = center_coord.at(i).at(0); c_y = center_coord.at(i).at(1); c_x += step * node_disp.at(i).first/disp_val; c_y += step * node_disp.at(i).second/disp_val; for (int j=0; j<cluster_nodes.size(); ++j) { angle = rotate_angles.at(i); // shift nodes coord.at(cluster_nodes.at(j)).at(0) -= center_coord.at(i).at(0); coord.at(cluster_nodes.at(j)).at(1) -= center_coord.at(i).at(1); // rotate nodes new_x = coord.at(cluster_nodes.at(j)).at(0); new_y = coord.at(cluster_nodes.at(j)).at(1); coord.at(cluster_nodes.at(j)).at(0) = new_x*cos(angle) + new_y*sin(angle); coord.at(cluster_nodes.at(j)).at(1) = -new_x*sin(angle) + new_y*cos(angle); // match to new center coord.at(cluster_nodes.at(j)).at(0) += c_x; coord.at(cluster_nodes.at(j)).at(1) += c_y; } center_coord.at(i).at(0) = c_x; center_coord.at(i).at(1) = c_y; } // update step calculate_energy(cluster_dist_mat, center_coord, curr_energy); update_step(step, pre_energy, curr_energy); cout << "curr_energy=" << curr_energy << endl; cout << "step=" << step << endl; // check convergence // dump information of each iteration if (t < 5) { // coordinates fo.open("out/"+string(outfilePath)+"_"+to_string(t)+".coord", fstream::out); for (int i=0; i<coord.size(); ++i) { fo << coord.at(i).at(0) << " " << coord.at(i).at(1) << endl; } fo.close(); // center coord fo.open("out/"+string(outfilePath)+"_"+to_string(t)+".center", fstream::out); for (int i=0; i<center_coord.size(); ++i) { fo << center_coord.at(i).at(0) << " " << center_coord.at(i).at(0) << endl; } fo.close(); // angle fo.open("out/"+string(outfilePath)+"_"+to_string(t)+".angle", fstream::out); for (int i=0; i<rotate_angles.size(); ++i) { fo << rotate_angles.at(i) << endl; } fo.close(); } } }
static void calculate_rotate_angle(Graph::Graph& g, int cls, std::vector<int>& cluster_nodes, std::vector<int>& clusters, std::vector< std::vector<CoordType> >& coord, std::vector< std::vector<CoordType> >& center_coord, std::vector<WgtType>& radii, std::vector< WgtType >& rotate_angles) { using namespace std; // torque int r_u; // current node id int r_v; // adjacent node id CoordType n_c_x; // center of neighbor's in different cluster CoordType n_c_y; CoordType r_u_x; // x components of raidus of u CoordType r_u_y; CoordType c_x; // center of u's x coord CoordType c_y; vector<VtxType> nbors; // current node id double sin_coeff = 0.0; double cos_coeff = 0.0; pair<CoordType, CoordType> force; pair<CoordType, CoordType> arm; double force_val; double arm_val; double t_angle; // angle of torque // topo algo for (int j=0; j<cluster_nodes.size(); ++j) { r_u = cluster_nodes.at(j); r_u_x = coord.at(r_u).at(0); r_u_y = coord.at(r_u).at(1); c_x = center_coord.at(cls).at(0); c_y = center_coord.at(cls).at(1); arm = make_pair(r_u_x - c_x, r_u_y - c_y); arm_val = sqrt( pow(arm.first, 2) + pow(arm.second, 2)); nbors = g.adj(r_u); for (int n=0; n<nbors.size(); ++n) { r_v = nbors.at(n); if ( clusters.at(r_u) != clusters.at(r_v) ) { n_c_x = center_coord.at(clusters.at(r_v)).at(0); n_c_y = center_coord.at(clusters.at(r_v)).at(1); // Rotate Step 1: calculate force and angle force = make_pair(n_c_x-r_u_x, n_c_y-r_u_y); force_val = sqrt( pow(force.first, 2) + pow(force.second, 2)); force = make_pair(force.first/force_val, force.second/force_val); t_angle = (arm_val/radii.at(cls))*M_PI/2*sgn(arm.first*force.second-arm.second*force.first)*(arm.first*force.first+arm.second*force.second); rotate_angles.at(cls) += t_angle; } } } // for (int j=0; j<cluster_nodes.size(); ++j) // { // r_u = cluster_nodes.at(j); // r_u_x = coord.at(r_u).at(0); // r_u_y = coord.at(r_u).at(1); // c_x = center_coord.at(cls).at(0); // c_y = center_coord.at(cls).at(1); // arm = make_pair(r_u_x - c_x, r_u_y - c_y); // arm_val = sqrt( pow(arm.first, 2) + pow(arm.second, 2)); // nbors = g.adj(r_u); // for (int n=0; n<nbors.size(); ++n) // { // r_v = nbors.at(n); // if ( clusters.at(r_u) != clusters.at(r_v) ) // { // n_c_x = center_coord.at(clusters.at(r_v)).at(0); // n_c_y = center_coord.at(clusters.at(r_v)).at(1); // // Rotate Step 1: calculate force and angle // force = make_pair(n_c_x-r_u_x, n_c_y-r_u_y); // force_val = sqrt( pow(force.first, 2) + pow(force.second, 2)); // t_angle = acos( (arm.first*force.first+arm.second*force.second) / (arm_val*force_val) ); // // Rotate Step 2 // // force_val = 1; // make force to be unit // force_val = 1/force_val; // make force to be inverse to the distance // sin_coeff += arm_val*force_val*cos(t_angle); // cos_coeff += arm_val*force_val*sin(t_angle); // } // } // } // // Step 3 // cout << sin_coeff << " " << cos_coeff << endl; // rotate_angles.at(cls) = atan(-cos_coeff/sin_coeff) * M_PI / 180; }
int main(int argc, char **argv){ string infile; string outfilename; string outprefix; string apspinputfilename; string lcc_apspinputfilename; ofstream outfile; ofstream timing_file; bool record_timings = false; bool file_append = false; bool run_largest_cc = true; string intype ("edge"); std::map<string, bool> req_methods; std::map<string, bool> val_methods; ORB_t t1, t2; int spectrum_spread = 0; create_map(allowed_methods, val_methods); parse_options(argc, argv, infile, intype, outfilename, outprefix, req_methods, record_timings, file_append, run_largest_cc, &spectrum_spread, apspinputfilename, lcc_apspinputfilename); if(outfilename.length() == 0){ if(outprefix.length() != 0){ outfilename = outprefix + ".stats"; } else { outfilename = "graph-stats.txt"; } } if(outprefix.length() == 0){ outprefix = infile; } // we'd like higher precision when printing values std::cout.precision(10); #ifdef MPI_VERSION MPI_Init(&argc, &argv); int myrank; MPI_Comm_rank(MPI_COMM_WORLD, &myrank); if(myrank == 0){ #endif cout << "done parsing options" << endl; cout << "Input file: " << infile << endl; cout << "Input type: " << intype << endl; cout << "Output file: " << outfilename << endl; cout << "Appending : "; cout << std::boolalpha << file_append << endl; cout << "Methods :"; for(map<string, bool>::iterator it = req_methods.begin(); it != req_methods.end(); ++it){ cout << " " << it->first; if(val_methods[it->first] != true){ cerr << "Error: " << it->first << " is not a valid method! " << endl; } } cout << endl; cout << "Calibrating timers" << endl; #ifdef MPI_VERSION } // main #endif ORB_calibrate(); // let's do some calculations Graph::Graph *g = new(Graph::Graph); Graph::GraphReader gr; Graph::GraphProperties gp; Graph::GraphUtil gu; #ifdef MPI_VERSION //int myrank; //MPI_Comm_rank(MPI_COMM_WORLD, &myrank); if(myrank == 0){ #endif // Set up output streams if(file_append == false){ outfile.open(outfilename.c_str()); } else { outfile.open(outfilename.c_str(), ios_base::out | ios_base::app); } if(!outfile.is_open()){ cerr << "Error opening " << outfilename << " for writing, exiting" << endl; exit(1); } #ifdef MPI_VERSION } #endif // Read in the graph and start recording things to output streams cout << "Reading graph" << endl; ORB_read(t1); if(gr.read_graph(g, infile, intype, false) == -1){ exit(1); } ORB_read(t2); if(outfile.tellp() == 0){ outfile << "filename " << infile << endl; outfile << "input_num_nodes " << g->get_num_nodes() << endl; outfile << "input_num_edges " << g->get_num_edges() << endl; } if(record_timings){ string of = outfilename + ".timings"; #ifdef MPI_VERSION if(0 == myrank){ #endif if(file_append == false){ timing_file.open(of.c_str()); } else { timing_file.open(of.c_str(), ios_base::out | ios_base::app); } if(!timing_file.is_open()){ cerr << "Error opening " << timing_file << " for writing, exiting" << endl; exit(1); } if(false == file_append){ outfile << "timing_file " << of << endl; } #ifdef MPI_VERSION } #endif } print_time(timing_file, "Time(read_graph)", t1, t2); if(apspinputfilename.length() != 0){ cout << "Reading APSP matrix from " << apspinputfilename << endl; vector< vector<int> > *apsp_dists = new vector< vector<int> >; ORB_read(t1); read_apsp_matrix(apspinputfilename, *apsp_dists); ORB_read(t2); print_time(timing_file, "Time(read_apsp_matrix)", t1, t2); g->set_shortest_path_dist(apsp_dists); } outfile.precision(16); vector<int> components; ORB_read(t1); gu.label_all_components(g, &components); ORB_read(t2); print_time(timing_file, "Time(label_all_components)", t1, t2); bool is_connected = gp.is_connected(g); cout << "Connected components: " << g->get_num_connected_components() << endl; //cout << "Graph is connected: " << std::boolalpha << is_connected << endl; run_all_methods(g, outfile, timing_file, outprefix, req_methods, file_append, spectrum_spread); outfile.close(); timing_file.close(); // some algorithms only make sense to run on a connected graph/component if(not is_connected and run_largest_cc){ // run everything against the other algorithms cout << "Graph is not connected, re-running stats on largest connected component" << endl; outfilename = outprefix + ".largest_component.stats"; if(file_append == false){ outfile.open(outfilename.c_str()); } else { outfile.open(outfilename.c_str(), ios_base::out | ios_base::app); } if(!outfile.is_open()){ cerr << "Error opening " << outfilename << " for writing, exiting" << endl; exit(1); } // get the largest component Graph::Graph *largest_component = gu.get_largest_component_graph(g); cerr << "Deleting g" << endl; delete(g); // delete g here to save on memory cerr << "g deleted" << endl; if(outfile.tellp() == 0){ #ifdef MPI_VERSION if(0 == myrank){ #endif outfile << "largest_component_from " << infile << endl; outfile << "input_num_nodes " << largest_component->get_num_nodes() << endl; outfile << "input_num_edges " << largest_component->get_num_edges() << endl; #ifdef MPI_VERSION } #endif } if(record_timings){ string of = outfilename + ".timings"; if(file_append == false){ timing_file.open(of.c_str()); #ifdef MPI_VERSION if(0 == myrank){ #endif outfile << "timing_file " << of << endl; #ifdef MPI_VERSION } #endif } else { timing_file.open(of.c_str(), ios_base::out | ios_base::app); } if(!timing_file.is_open()){ cerr << "Error opening " << timing_file << " for writing, exiting" << endl; exit(1); } } if(lcc_apspinputfilename.length() != 0){ cout << "Reading LCC APSP matrix from " << lcc_apspinputfilename << endl; vector< vector<int> > *apsp_dists = new vector< vector<int> >; ORB_read(t1); read_apsp_matrix(lcc_apspinputfilename, *apsp_dists); ORB_read(t2); print_time(timing_file, "Time(read_apsp_matrix)", t1, t2); largest_component->set_shortest_path_dist(apsp_dists); } outprefix = outprefix + ".largest_component"; outfile.precision(16); cerr << "Running methods on largest component" << endl; run_all_methods(largest_component, outfile, timing_file, outprefix, req_methods, file_append, spectrum_spread); outfile.close(); timing_file.close(); } #ifdef MPI_VERSION MPI_Finalize(); #endif exit(0); } // main
std::string to_string(const graph::Graph &g) { return "graph " + g.state(); }
static void calculate_nodes_radii(Graph::Graph& g, DenseMat& distMat, std::vector<int>& clusters, std::vector<int>& cluster_nodes, std::vector< std::vector<CoordType> >& intra_coord, std::vector<WgtType>& nodes_radii) { // Steps // 1. Calculate cluster radius // 2. get the inter/intra cluster degree of each vtxs // 3. calculate radial constriants using namespace std; // Step 1 // get the corresponding cluster distance // minus central node // [modified!] clusters_nodes DenseMat clsDistMat(intra_coord.size()-1, intra_coord.size()-1); VtxType rr; VtxType cc; for (int c=0; c<clsDistMat.cols(); ++c) { for (int r=0; r<clsDistMat.rows(); ++r) { rr = cluster_nodes.at(r); cc = cluster_nodes.at(c); clsDistMat(c, r) = distMat(cc, rr); } } cout << "cls distance matrix" << endl; cout << clsDistMat << endl; // find the maximum pair distance double cls_radius = clsDistMat.maxCoeff()/2; // Step 2 vector<VtxType> intra_deg(cluster_nodes.size(), 0); vector<VtxType> inter_deg(cluster_nodes.size(), 0); vector<VtxType> nbors; for (int i=0; i<cluster_nodes.size(); ++i) { nbors = g.adj( cluster_nodes.at(i) ); for (int n=0; n<nbors.size(); ++n) { if (clusters.at( cluster_nodes.at(i) ) == clusters.at( nbors.at(n) )) { intra_deg.at(i) += 1; } else { inter_deg.at(i) += 1; } } } cout << "intra_deg=" << intra_deg.size() << endl; cout << "inter_deg=" << inter_deg.size() << endl; // Step 3 const int offset = 1; double min_inter = *min_element(inter_deg.begin(), inter_deg.end()); double max_inter = *max_element(inter_deg.begin(), inter_deg.end()); cout << "radius" << endl; for (int i=0; i<nodes_radii.size(); ++i) { nodes_radii.at(i) = cls_radius*( (inter_deg.at(i)-min_inter+offset) / (max_inter-min_inter+offset) ); } }
int main(int argc, char** argv) { std::fstream f; std::string line; f.open(/*"tsp_test.txt"*/argv[1]); std::vector<std::string> strs; getline(f,line); boost::split(strs,line,boost::is_any_of("\t ")); int n = atoi(strs[0].c_str()); Graph::Graph* g = new Graph::Graph(); /* while(getline(f,line)) //FILL BY EDGES { boost::split(strs,line,boost::is_any_of("\t ")); g->addEdge(atoi(strs[0].c_str())-1,atoi(strs[1].c_str())-1,atoi(strs[2].c_str())); }*/ while(getline(f,line)) { boost::split(strs,line,boost::is_any_of("\t ")); g->addVertexEuclid(atoi(strs[0].c_str()) , atoi(strs[1].c_str())); } /* int min; try{ min = g->SSP(); std::cout<<"ans is "<< min<<std::endl; } catch(Graph::Exception& e) { std::cout<<"exception: "<<e.what()<<std::endl; } */ std::vector<std::pair<std::vector<Graph::Vertex*>,double> >paths = g->TSPEuclid();//g->TSPEuclidRec(g->getVertex(1)); int num = 1; std::pair<std::vector<Graph::Vertex*>, double> minimalpath = paths.front(); for(std::vector<std::pair< std::vector <Graph::Vertex*>, double> >::iterator i = paths.begin(); i!=paths.end(); ++i) { // std::cout<<"path number "<<num<<std::endl; /*for(std::vector<Graph::Vertex*>::iterator j = (std::get<0>(*i)).begin();j!=(std::get<0>(*i)).end();++j) { std::cout<<"->"<<(*j)->getName(); }*/ if((std::get<1>(*i)) < (std::get<1>(minimalpath))) minimalpath = (*i); num++; // std::cout<<std::endl<<"lenght = "<<std::get<1>(*i)<<std::endl; // std::cout<<std::endl<<std::endl; } for(std::vector<Graph::Vertex*>::iterator j = (std::get<0>(minimalpath)).begin();j!=(std::get<0>(minimalpath)).end();++j) { std::cout<<"->"<<(*j)->getName(); } std::cout<<std::endl<<"minimal path cost = "<<std::get<1>(minimalpath)<<std::endl; f.close(); return 0; }
int main(int argc, char **argv){ #if !WIN32 & !CYGWIN ORB_t t1, t2; char *intype, *outtype, *infile, *outfile; // Check for a cry for help if((argc == 1) || ((argc == 2) && (strcmp(argv[1],"-h") == 0)) || ((argc == 2) && (strcmp(argv[1],"--help") == 0)) || ((argc == 2) && (strcmp(argv[1],"--h") == 0) ) ){ usage(argv[0]); exit(0); } if(argc != 5){ usage(argv[0]); exit(1); } intype = argv[1]; outtype = argv[2]; infile = argv[3]; outfile = argv[4]; Graph::Graph *g; int seed = 0; cout << "calibrating timers\n"; ORB_calibrate(); if(!seed){ // Set the seed to a rand int in 0,2^24 seed = Graph::rand_int(0,0xffffff); } // Spin the RNG seed times for(int ss = 0; ss < seed; ss++){ Graph::lcgrand(0); } Graph::GraphCreatorFile *gcf; Graph::GraphProperties prop; Graph::GraphReader ngr; Graph::GraphWriter writer; g = new Graph::Graph(); cout << "Input type : " << intype << endl; cout << "Output type: " << outtype << endl; cout << "Reading graph" << endl; ORB_read(t1); ngr.read_graph(g, infile, intype, false); ORB_read(t2); print_time("Time(read_graph)", t1, t2); // if we don't get rid of duplicate edges, bad things happen // when trying to output the graph //prop.make_simple(g); fprintf(stderr, "edges read in: %d nodes read in: %d\n", g->get_num_edges(), g->get_num_nodes()); cout << "Writing graph\n"; ORB_read(t1); writer.write_graph(g, outfile, outtype); ORB_read(t2); print_time("Time(write_graph)", t1, t2); return 0; #else fprintf(stderr,"Can't build under Cygwin or Windows\n"); return 1; #endif } // main
TEST_F(GraphCreatorFileTest, testGraph) { Graph::Graph *mg = creator->create_mutable_graph(); EXPECT_EQ(128, mg->get_num_nodes()) ; EXPECT_EQ(1471, mg->get_num_edges()) ; vector<Graph::Node> n; n = mg->get_nodes(); EXPECT_EQ(128, n.size()) ; list<int> nbrlist = n[35].get_nbrs(); vector<int> nbrs(nbrlist.begin(), nbrlist.end()); EXPECT_EQ(75, nbrs[20]) ; nbrlist = n[127].get_nbrs(); nbrs.clear(); nbrs.insert(nbrs.end(), nbrlist.begin(), nbrlist.end()); EXPECT_EQ(111, nbrs[2]) ; nbrlist = n[0].get_nbrs(); nbrs.clear(); nbrs.insert(nbrs.end(), nbrlist.begin(), nbrlist.end()); EXPECT_EQ(1, nbrs[0]) ; EXPECT_EQ(64, nbrs[6l]) ; EXPECT_EQ(8, nbrs[3l]) ; EXPECT_EQ(24, mg->get_degree(35)) ; EXPECT_EQ(28, mg->get_degree(75)) ; mg->remove_edge(35, 75); EXPECT_EQ(23, mg->get_degree(35)) ; EXPECT_EQ(27, mg->get_degree(75)) ; mg->remove_vertex(35); EXPECT_EQ(0, mg->get_degree(35)) ; EXPECT_EQ(27, mg->get_degree(75)) ; }