/////////////////////////////////////////////////////////////////////////////// /// public _import_from_stdin /// Construct the graph by importing the edges from the standard input. /// /// /// @remarks The format of the data is as follows: /// 1. The first line has an integer as the number of vertices of the graph /// 2. Each line afterwards contains a directed edge in the graph: /// starting point, ending point and the weight of the edge. /// These values are separated by 'white space'. /// /// /// @author Lukas Limacher @date 5/24/2015 /////////////////////////////////////////////////////////////////////////////// void Graph::_import_from_stdin() { // Read from stdin // http://bytes.com/topic/c/answers/667481-copy-std-cin-ifstream istream* input; input = &cin; // read from *input //2. Reset the members of the class clear(); //3. Start to read information from the input file. /// Note the format of the data in the graph file. //3.1 The first line has an integer as the number of vertices of the graph *input >> m_nVertexNum; //3.2 In the following lines, each line contains a directed edge in the graph: /// the id of starting point, the id of ending point, the weight of the edge. /// These values are separated by 'white space'. int start_vertex, end_vertex; double edge_weight; int vertex_id = 0; //TODO check if an EOF or NULL needs to be writte from perl at the end // or if a delimiter character is needed! while(*input >> start_vertex) { if (start_vertex == -1) { break; } *input >> end_vertex; *input >> edge_weight; ///3.2.1 construct the vertices BaseVertex* start_vertex_pt = get_vertex(start_vertex); BaseVertex* end_vertex_pt = get_vertex(end_vertex); ///3.2.2 add the edge weight //// note that the duplicate edge would overwrite the one occurring before. m_mpEdgeCodeWeight[get_edge_code(start_vertex_pt, end_vertex_pt)] = edge_weight; ///3.2.3 update the fan-in or fan-out variables //// Fan-in get_vertex_set_pt(end_vertex_pt, m_mpFaninVertices)->insert(start_vertex_pt); //// Fan-out get_vertex_set_pt(start_vertex_pt, m_mpFanoutVertices)->insert(end_vertex_pt); } if(m_nVertexNum != m_vtVertices.size()) { cerr << "The number of nodes in the graph is "<< m_vtVertices.size() << " instead of " << m_nVertexNum << endl; exit(1); } m_nVertexNum = m_vtVertices.size(); m_nEdgeNum = m_mpEdgeCodeWeight.size(); }
/////////////////////////////////////////////////////////////////////////////// /// public _import_from_file /// Construct the graph by importing the edges from the input file. /// /// @param [in] file_name const std::string & The input graph file /// /// This function doesn't return a value /// /// @remarks The format of the file is as follows: /// 1. The first line has an integer as the number of vertices of the graph /// 2. Each line afterwards contains a directed edge in the graph: /// starting point, ending point and the weight of the edge. /// These values are separated by 'white space'. /// /// @see <TODO: insert text here> /// /// @author Yan Qi @date 5/29/2010 /////////////////////////////////////////////////////////////////////////////// void Graph::_import_from_file( const string& input_file_name ) { const char* file_name = input_file_name.c_str(); //1. Check the validity of the file ifstream ifs(file_name); if (!ifs) { cerr << "The file " << file_name << " can not be opened!" << endl; exit(1); } //2. Reset the members of the class clear(); //3. Start to read information from the input file. /// Note the format of the data in the graph file. //3.1 The first line has an integer as the number of vertices of the graph ifs >> m_nVertexNum; //3.2 In the following lines, each line contains a directed edge in the graph: /// the id of starting point, the id of ending point, the weight of the edge. /// These values are separated by 'white space'. int start_vertex, end_vertex; double edge_weight; int vertex_id = 0; while(ifs >> start_vertex) { if (start_vertex == -1) { break; } ifs >> end_vertex; ifs >> edge_weight; ///3.2.1 construct the vertices BaseVertex* start_vertex_pt = get_vertex(start_vertex); BaseVertex* end_vertex_pt = get_vertex(end_vertex); ///3.2.2 add the edge weight //// note that the duplicate edge would overwrite the one occurring before. m_mpEdgeCodeWeight[get_edge_code(start_vertex_pt, end_vertex_pt)] = edge_weight; ///3.2.3 update the fan-in or fan-out variables //// Fan-in get_vertex_set_pt(end_vertex_pt, m_mpFaninVertices)->insert(start_vertex_pt); //// Fan-out get_vertex_set_pt(start_vertex_pt, m_mpFanoutVertices)->insert(end_vertex_pt); } m_nVertexNum = m_vtVertices.size(); m_nEdgeNum = m_mpEdgeCodeWeight.size(); ifs.close(); }
double Graph::get_original_edge_weight( const BaseVertex* source, const BaseVertex* sink ) { map<int, double>::const_iterator pos = m_mpEdgeCodeWeight.find(get_edge_code(source, sink)); if (pos != m_mpEdgeCodeWeight.end()) { return pos->second; }else { return DISCONNECT; } }