Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.2 kB
2
Indexable
Never
#include <iostream>
#include "graph.hpp"

using namespace cs251;

void graph::read_edge_weights(const std::string& filePath) {
	// TODO: Implement read edge weights	
	//throw std::logic_error("graph::" + std::string(__FUNCTION__) + "() not implemented");
      std::ifstream file(filePath);
        if (!file.is_open()) {
            throw std::runtime_error("Error opening file " + filePath);
        }

        int vertices, edges;
        file >> vertices >> edges;
		graph_vertices = vertices;
		graph_edges = edges;
        m_vertices.resize(vertices);

        for (int i = 0; i < edges; ++i) {
            int source, destination, weight;
            file >> source >> destination >> weight;
			std :: vector<int> edge_input = {weight, source, destination};
			m_graph_edges.push_back(edge_input);
            graph_edge edge;
            edge.m_weight = weight;
            edge.m_destinationHandle = destination;
            m_vertices[source].m_edges.push_back(edge);

        }

        file.close();
} 


void graph::read_edge_colors(const std::string& filePath) {
	// TODO: Implement read edge colors	
	//throw std::logic_error("graph::" + std::string(__FUNCTION__) + "() not implemented");
    std::ifstream file(filePath);
        if (!file.is_open()) {
            throw std::runtime_error("Error opening file " + filePath);
        }

        int vertices, edges;
        file >> vertices >> edges;
        m_vertices.resize(vertices);

        for (int i = 0; i < edges; ++i) {
            int source, destination, weight;
            char colorChar;
            file >> source >> destination >> weight >> colorChar;

            graph_edge edge;
            edge.m_weight = weight;
            edge.m_destinationHandle = destination;
            edge.col = static_cast<color>(colorChar - '0'); // Assuming colorChar is '0' for RED, '1' for GREEN, '2' for BLUE
            m_vertices[source].m_edges.push_back(edge);

           
        } 

        file.close(); 
        
    }
void graph::read_graph(std::vector<graph_vertex> &adj_list){
    adj_list = m_vertices;
}
Leave a Comment