|
| 1 | +#ifndef WEIGHTED_GRAPHS_CPP_ADJACENCYLISTFLOWNETWORK_H |
| 2 | +#define WEIGHTED_GRAPHS_CPP_ADJACENCYLISTFLOWNETWORK_H |
| 3 | + |
| 4 | +#include <istream> |
| 5 | +#include "FlowNetwork.h" |
| 6 | + |
| 7 | +class AdjacencyListFlowNetwork : public FlowNetwork { |
| 8 | +public: |
| 9 | + explicit AdjacencyListFlowNetwork(const int numVertices) |
| 10 | + : numV(numVertices), numE(0), edgesByVertex(numVertices, std::vector<std::shared_ptr<FlowEdge>>{}) {} |
| 11 | + |
| 12 | + explicit AdjacencyListFlowNetwork(std::istream& is) { |
| 13 | + if (!(is >> numV) || numV < 0) throw std::invalid_argument("Invalid format (V)"); |
| 14 | + if (!(is >> numE || numE < 0)) throw std::invalid_argument("Invalid format (E)"); |
| 15 | + edgesByVertex.insert(edgesByVertex.end(), numV, {}); |
| 16 | + |
| 17 | + for (int i = 0; i < numE; ++i) { |
| 18 | + int fromVertex, toVertex; |
| 19 | + double capacity; |
| 20 | + if (!(is >> fromVertex >> toVertex >> capacity)) throw std::invalid_argument("Invalid format (edges)"); |
| 21 | + if (fromVertex < 0 || fromVertex >= numV || toVertex < 0 || toVertex >= numV || capacity <= 0 || !std::isfinite(capacity)) { |
| 22 | + throw std::invalid_argument("Cannot create flow edge v->w"); |
| 23 | + } |
| 24 | + edgesByVertex[fromVertex].emplace_back(std::make_shared<FlowEdge>(fromVertex, toVertex, capacity)); |
| 25 | + edgesByVertex[toVertex].emplace_back(edgesByVertex[fromVertex].back()); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + void addEdge(const FlowEdge& e) override { |
| 30 | + const int fromVertex = e.from(); |
| 31 | + const int toVertex = e.to(); |
| 32 | + if(!validVertex(fromVertex) || !validVertex(toVertex)) { |
| 33 | + throw std::invalid_argument("Vertex IDs invalid"); |
| 34 | + } |
| 35 | + if (e.capacity() < 0 || !std::isfinite(e.capacity())) { |
| 36 | + throw std::invalid_argument("Invalid capacity"); |
| 37 | + } |
| 38 | + |
| 39 | + edgesByVertex[fromVertex].emplace_back(std::make_shared<FlowEdge>(e)); |
| 40 | + edgesByVertex[toVertex].emplace_back(edgesByVertex[fromVertex].back()); |
| 41 | + ++numE; |
| 42 | + } |
| 43 | + |
| 44 | + [[nodiscard]] |
| 45 | + const std::vector<std::shared_ptr<FlowEdge>>& adj(int v) const override { |
| 46 | + if(!validVertex(v)) throw std::invalid_argument("Invalid vertex ID"); |
| 47 | + return edgesByVertex[v]; |
| 48 | + } |
| 49 | + |
| 50 | + [[nodiscard]] |
| 51 | + std::vector<std::shared_ptr<FlowEdge>> edges() const override { |
| 52 | + std::vector<std::shared_ptr<FlowEdge>> result; |
| 53 | + result.reserve(numE); |
| 54 | + for (size_t i=0; i<edgesByVertex.size(); ++i) { |
| 55 | + const auto& edgesVec = edgesByVertex[i]; |
| 56 | + |
| 57 | + for(const auto& edge : edgesVec) { |
| 58 | + if (static_cast<int>(i) == edge->from()) { |
| 59 | + // only add edges once |
| 60 | + result.emplace_back(edge); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + [[nodiscard]] |
| 69 | + int V() const override { |
| 70 | + return numV; |
| 71 | + } |
| 72 | + |
| 73 | + [[nodiscard]] |
| 74 | + int E() const override { |
| 75 | + return numE; |
| 76 | + } |
| 77 | + |
| 78 | +private: |
| 79 | + int numV {}; |
| 80 | + int numE {}; |
| 81 | + std::vector<std::vector<std::shared_ptr<FlowEdge>>> edgesByVertex {}; |
| 82 | + |
| 83 | + [[nodiscard]] |
| 84 | + bool validVertex(int v) const { |
| 85 | + return v >= 0 && static_cast<size_t>(v) < edgesByVertex.size(); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +#endif //WEIGHTED_GRAPHS_CPP_ADJACENCYLISTFLOWNETWORK_H |
0 commit comments