From f6b9e7b0341b76bd8f41db3bbf455bde385f12d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Mon, 23 Jun 2025 20:52:16 +0200 Subject: [PATCH] Add GraphBase::getDegree() --- include/aoc/common/GraphBase.hpp | 6 ++++-- src/common/GraphBase.cpp | 21 ++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/include/aoc/common/GraphBase.hpp b/include/aoc/common/GraphBase.hpp index 93a59e6..5df728b 100644 --- a/include/aoc/common/GraphBase.hpp +++ b/include/aoc/common/GraphBase.hpp @@ -23,6 +23,7 @@ class GraphBase public: size_t getNVertices() const; size_t getNEdges() const; + size_t getDegree(const int vertex) const; bool areAdjacent(const int vertex1, const int vertex2) const; struct Incidence @@ -51,7 +52,7 @@ class GraphBase { if (vertex >= 0 && vertex < graph_->getNVertices()) { - setIncidence(graph_->firstVertexIncidences[vertex]); + setIncidence(graph_->vertexIncidenceHeaders[vertex].first); } }; NeighborIterator& operator++() @@ -99,7 +100,8 @@ class GraphBase NeighborIterator end() const; protected: - std::vector firstVertexIncidences{}; + // First is the index of the first incidence, second is the number of incidences (the length of the list). + std::vector> vertexIncidenceHeaders{}; std::vector nextIncidences{}; std::vector incidenceVertices{}; int addVertexInternal(); diff --git a/src/common/GraphBase.cpp b/src/common/GraphBase.cpp index 56b6bef..e241e2d 100644 --- a/src/common/GraphBase.cpp +++ b/src/common/GraphBase.cpp @@ -17,7 +17,7 @@ size_t GraphBase::getNVertices() const { - return firstVertexIncidences.size(); + return vertexIncidenceHeaders.size(); } size_t GraphBase::getNEdges() const @@ -25,6 +25,11 @@ size_t GraphBase::getNEdges() const return nextIncidences.size() >> 1; } +size_t GraphBase::getDegree(const int vertex) const +{ + return vertexIncidenceHeaders[vertex].second; +} + bool GraphBase::areAdjacent(const int vertex1, const int vertex2) const { auto it = begin(vertex1); @@ -51,17 +56,19 @@ GraphBase::NeighborIterator GraphBase::end() const int GraphBase::addVertexInternal() { - firstVertexIncidences.push_back(-1); - return static_cast(firstVertexIncidences.size()) - 1; + vertexIncidenceHeaders.push_back({ -1, 0 }); + return static_cast(vertexIncidenceHeaders.size()) - 1; } void GraphBase::addEdgeInternal(const int vertex1, const int vertex2) { incidenceVertices.push_back(vertex2); - nextIncidences.push_back(firstVertexIncidences[vertex1]); - firstVertexIncidences[vertex1] = static_cast(incidenceVertices.size()) - 1; + nextIncidences.push_back(vertexIncidenceHeaders[vertex1].first); + vertexIncidenceHeaders[vertex1].first = static_cast(incidenceVertices.size()) - 1; + vertexIncidenceHeaders[vertex1].second++; incidenceVertices.push_back(vertex1); - nextIncidences.push_back(firstVertexIncidences[vertex2]); - firstVertexIncidences[vertex2] = static_cast(incidenceVertices.size()) - 1; + nextIncidences.push_back(vertexIncidenceHeaders[vertex2].first); + vertexIncidenceHeaders[vertex2].first = static_cast(incidenceVertices.size()) - 1; + vertexIncidenceHeaders[vertex2].second++; }