Ejemplo n.º 1
0
///////////////////////////////////////////////////////////////////////////////
///  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();
}
Ejemplo n.º 2
0
///////////////////////////////////////////////////////////////////////////////
///  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();	
}
Ejemplo n.º 3
0
void Graph::get_precedent_vertices( BaseVertex* vertex, set<BaseVertex*>& vertex_set )
{
	if (m_stRemovedVertexIds.find(vertex->getID()) == m_stRemovedVertexIds.end())
	{
		int ending_vt_id = vertex->getID();
		set<BaseVertex*>* pre_vertex_set = get_vertex_set_pt(vertex, m_mpFaninVertices);
		for(set<BaseVertex*>::const_iterator pos=(*pre_vertex_set).begin(); 
			pos != (*pre_vertex_set).end(); ++pos)
		{
			int starting_vt_id = (*pos)->getID();
			if (m_stRemovedVertexIds.find(starting_vt_id) != m_stRemovedVertexIds.end()
				|| m_stRemovedEdge.find(make_pair(starting_vt_id, ending_vt_id)) != m_stRemovedEdge.end())
			{
				continue;
			}
			//
			vertex_set.insert(*pos);
		}
	}
}