Untitled
unknown
plain_text
2 years ago
2.2 kB
10
Indexable
#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;
m_vertices.resize(vertices);
for (int i = 0; i < edges; ++i) {
int source, destination, weight;
file >> source >> destination >> weight;
graph_edge edge;
edge.m_weight = weight;
edge.m_destinationHandle = destination;
m_vertices[source].m_edges.push_back(edge);
// For undirected graphs, add the reverse edge as well
graph_edge reverseEdge = { weight, source, NONE };
m_vertices[destination].m_edges.push_back(reverseEdge);
}
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);
// For undirected graphs, add the reverse edge as well
graph_edge reverseEdge = { weight, source, edge.col };
m_vertices[destination].m_edges.push_back(reverseEdge);
}
file.close();
}
}
Editor is loading...
Leave a Comment