Refactor graph classes
- Extract base graph logic from WeightedEdgeGraph into new Graph class - Add LabelGraph to replace old Graph class used by LanParty solver - Rename WeightedEdgeGraph to WeightGraph and change it to use new Graph class - Update affected solvers to use new graph classes
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 Stefan Müller
|
||||
// Copyright (C) 2025 Stefan Müller
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it under
|
||||
// the terms of the GNU General Public License as published by the Free Software
|
||||
@@ -15,18 +15,95 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
|
||||
#include <aoc/common/Vertex.hpp>
|
||||
|
||||
class Graph
|
||||
{
|
||||
public:
|
||||
const std::map<std::string, std::shared_ptr<Vertex>>& getVertices() const;
|
||||
void addEdge(const std::string& vertexId1, const std::string& vertexId2);
|
||||
int addVertex();
|
||||
void addEdge(const int vertex1, const int vertex2);
|
||||
size_t getNVertices() const;
|
||||
size_t getNEdges() const;
|
||||
bool areAdjacent(const int vertex1, const int vertex2) const;
|
||||
|
||||
struct Incidence
|
||||
{
|
||||
public:
|
||||
Incidence(const int vertex, const int edge)
|
||||
: vertex{ vertex }, edge{ edge }
|
||||
{
|
||||
}
|
||||
int vertex;
|
||||
int edge;
|
||||
};
|
||||
|
||||
struct NeighborIterator
|
||||
{
|
||||
public:
|
||||
// Iterator traits.
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using value_type = Incidence;
|
||||
using pointer = const value_type*;
|
||||
using reference = const value_type&;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
|
||||
NeighborIterator(const Graph& graph, const int vertex)
|
||||
: graph_{ graph }, incidence_{ -1 }, value_{ -1, -1 }
|
||||
{
|
||||
if (vertex >= 0 && vertex < graph_.firstVertexIncidences_.size())
|
||||
{
|
||||
setIncidence(graph_.firstVertexIncidences_[vertex]);
|
||||
}
|
||||
};
|
||||
NeighborIterator& operator++()
|
||||
{
|
||||
if (incidence_ >= 0)
|
||||
{
|
||||
setIncidence(graph_.nextIncidences_[incidence_]);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
NeighborIterator operator++(int)
|
||||
{
|
||||
NeighborIterator it = *this;
|
||||
++(*this);
|
||||
return it;
|
||||
}
|
||||
bool operator==(NeighborIterator other) const
|
||||
{
|
||||
return incidence_ == other.incidence_;
|
||||
}
|
||||
bool operator!=(NeighborIterator other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
const reference operator*() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
const pointer operator->() const
|
||||
{
|
||||
return &value_;
|
||||
}
|
||||
private:
|
||||
const Graph& graph_;
|
||||
int incidence_;
|
||||
value_type value_;
|
||||
void setIncidence(const int incidence)
|
||||
{
|
||||
incidence_ = incidence;
|
||||
if (incidence_ >= 0)
|
||||
{
|
||||
value_ = { graph_.incidenceVertices_[incidence_], incidence_ >> 1 };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NeighborIterator begin(const int vertex) const;
|
||||
NeighborIterator end() const;
|
||||
private:
|
||||
std::map<std::string, std::shared_ptr<Vertex>> vertices_;
|
||||
std::shared_ptr<Vertex> findOrAddVertex(const std::string& vertexId);
|
||||
std::vector<int> firstVertexIncidences_{};
|
||||
std::vector<int> nextIncidences_{};
|
||||
std::vector<int> incidenceVertices_{};
|
||||
};
|
||||
|
||||
@@ -15,14 +15,19 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
class VertexEdgeIncidence
|
||||
#include <vector>
|
||||
|
||||
class GraphPathsResult
|
||||
{
|
||||
public:
|
||||
VertexEdgeIncidence(const int vertex, const int next)
|
||||
GraphPathsResult()
|
||||
: distances{}, predecessors{}
|
||||
{
|
||||
this->vertex = vertex;
|
||||
this->next = next;
|
||||
}
|
||||
int vertex;
|
||||
int next;
|
||||
GraphPathsResult(const size_t size, const int initialDistance, const int initialPredecessor)
|
||||
: distances(size, initialDistance), predecessors(size, initialPredecessor)
|
||||
{
|
||||
}
|
||||
std::vector<int> distances;
|
||||
std::vector<int> predecessors;
|
||||
};
|
||||
38
include/aoc/common/LabelGraph.hpp
Normal file
38
include/aoc/common/LabelGraph.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2025 Stefan Müller
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it under
|
||||
// the terms of the GNU General Public License as published by the Free Software
|
||||
// Foundation, either version 3 of the License, or (at your option) any later
|
||||
// version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include <aoc/common/Graph.hpp>
|
||||
|
||||
class LabelGraph
|
||||
{
|
||||
public:
|
||||
int addVertex(const std::string& label);
|
||||
void addEdge(const int vertex1, const int vertex2);
|
||||
bool areAdjacent(const int vertex1, const int vertex2) const;
|
||||
const std::string& getVertexLabel(const int vertex) const;
|
||||
const std::map<std::string, int>& getLabelVertices() const;
|
||||
Graph::NeighborIterator begin(const int vertex) const;
|
||||
Graph::NeighborIterator end() const;
|
||||
private:
|
||||
Graph graph_{};
|
||||
std::vector<std::string> vertexLabels_{};
|
||||
std::map<std::string, int> labelVertices_{};
|
||||
};
|
||||
38
include/aoc/common/WeightGraph.hpp
Normal file
38
include/aoc/common/WeightGraph.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2025 Stefan Müller
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it under
|
||||
// the terms of the GNU General Public License as published by the Free Software
|
||||
// Foundation, either version 3 of the License, or (at your option) any later
|
||||
// version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <aoc/common/Graph.hpp>
|
||||
#include <aoc/common/GraphPathsResult.hpp>
|
||||
|
||||
class WeightGraph
|
||||
{
|
||||
public:
|
||||
static constexpr int getInfiniteDistance();
|
||||
int addVertex(const int weight);
|
||||
void addEdge(const int vertex1, const int vertex2, const int weight);
|
||||
int getVertexWeight(const int vertex) const;
|
||||
int getEdgeWeight(const int edge) const;
|
||||
GraphPathsResult dijkstra(const int source) const;
|
||||
Graph::NeighborIterator begin(const int vertex) const;
|
||||
Graph::NeighborIterator end() const;
|
||||
private:
|
||||
Graph graph_{};
|
||||
std::vector<int> edgeWeights_{};
|
||||
std::vector<int> vertexWeights_{};
|
||||
};
|
||||
@@ -1,129 +0,0 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2025 Stefan Müller
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it under
|
||||
// the terms of the GNU General Public License as published by the Free Software
|
||||
// Foundation, either version 3 of the License, or (at your option) any later
|
||||
// version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
|
||||
#include <aoc/common/VertexEdgeIncidence.hpp>
|
||||
|
||||
class WeightedEdgeGraph
|
||||
{
|
||||
public:
|
||||
WeightedEdgeGraph();
|
||||
int addVertex(const int weight = 0);
|
||||
void addEdge(const int vertex1, const int vertex2, const int weight);
|
||||
int getVertexWeight(const int vertex) const;
|
||||
int getEdgeWeight(const int edge) const;
|
||||
|
||||
struct PathsResult
|
||||
{
|
||||
PathsResult()
|
||||
: distances{}, predecessors{}
|
||||
{
|
||||
}
|
||||
PathsResult(const size_t size, const int initialDistance, const int initialPredecessor)
|
||||
: distances(size, initialDistance), predecessors(size, initialPredecessor)
|
||||
{
|
||||
}
|
||||
std::vector<int> distances;
|
||||
std::vector<int> predecessors;
|
||||
};
|
||||
|
||||
PathsResult dijkstra(const int source) const;
|
||||
static constexpr int getInfiniteDistance();
|
||||
|
||||
struct Incidence
|
||||
{
|
||||
public:
|
||||
Incidence(const int vertex, const int edge)
|
||||
: vertex{ vertex }, edge{ edge }
|
||||
{
|
||||
}
|
||||
int vertex;
|
||||
int edge;
|
||||
};
|
||||
|
||||
struct NeighborIterator
|
||||
{
|
||||
public:
|
||||
// Iterator traits.
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using value_type = Incidence;
|
||||
using pointer = const value_type*;
|
||||
using reference = const value_type&;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
|
||||
NeighborIterator(const WeightedEdgeGraph& graph, const int vertex)
|
||||
: graph_{ graph }, incidence_{ -1 }, value_{ -1, -1 }
|
||||
{
|
||||
if (vertex >= 0 && vertex < graph_.firstVertexIncidences_.size())
|
||||
{
|
||||
setIncidence(graph_.firstVertexIncidences_[vertex]);
|
||||
}
|
||||
};
|
||||
NeighborIterator& operator++()
|
||||
{
|
||||
if (incidence_ >= 0)
|
||||
{
|
||||
setIncidence(graph_.vertexEdgeIncidences_[incidence_].next);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
NeighborIterator operator++(int)
|
||||
{
|
||||
NeighborIterator it = *this;
|
||||
++(*this);
|
||||
return it;
|
||||
}
|
||||
bool operator==(NeighborIterator other) const
|
||||
{
|
||||
return incidence_ == other.incidence_;
|
||||
}
|
||||
bool operator!=(NeighborIterator other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
const reference operator*() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
const pointer operator->() const
|
||||
{
|
||||
return &value_;
|
||||
}
|
||||
private:
|
||||
const WeightedEdgeGraph& graph_;
|
||||
int incidence_;
|
||||
value_type value_;
|
||||
void setIncidence(const int incidence)
|
||||
{
|
||||
incidence_ = incidence;
|
||||
if (incidence_ >= 0)
|
||||
{
|
||||
value_ = { graph_.vertexEdgeIncidences_[incidence_].vertex, incidence_ >> 1 };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NeighborIterator begin(const int vertex) const;
|
||||
NeighborIterator end() const;
|
||||
private:
|
||||
std::vector<int> firstVertexIncidences_;
|
||||
std::vector<VertexEdgeIncidence> vertexEdgeIncidences_;
|
||||
std::vector<int> edgeWeights_;
|
||||
std::vector<int> vertexWeights_;
|
||||
};
|
||||
Reference in New Issue
Block a user