From 4a332be0e7c32ebaafe9e37b43659c3a56462a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sat, 4 Nov 2023 17:26:17 +0100 Subject: [PATCH] =?UTF-8?q?Store=20=CE=BC,=CF=86,=CF=81=20in=20nodes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also, add simple property checkers whether nodes are inner/outer/out-of-forest. --- graph.cpp | 18 ++++++++++++++++++ graph.hpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/graph.cpp b/graph.cpp index 7926f0d..e1aa1f2 100644 --- a/graph.cpp +++ b/graph.cpp @@ -178,4 +178,22 @@ std::ostream & operator<<(std::ostream & output, Graph const & graph) return output; } +bool Graph::is_outer(NodeId const id) const { + return matched_neighbor(id) == id or \ + ear_or_root_neighbor(matched_neighbor(id)) != matched_neighbor(id); +} + +bool Graph::is_inner(NodeId const id) const +{ + return ear_or_root_neighbor(id) != id and \ + ear_or_root_neighbor(matched_neighbor(id)) == matched_neighbor(id); +} + +bool Graph::is_out_of_forest(const ED::NodeId id) const +{ + return matched_neighbor(id) != id and \ + ear_or_root_neighbor(id) == id and \ + ear_or_root_neighbor(matched_neighbor(id)) == matched_neighbor(id); +} + } // namespace ED diff --git a/graph.hpp b/graph.hpp index 129acce..cca0ea8 100644 --- a/graph.hpp +++ b/graph.hpp @@ -69,6 +69,7 @@ using size_type = uint32_t; * Note the same Id may be used by different graphs though! */ using NodeId = size_type; +constexpr NodeId invalid_node_id = std::numeric_limits::max(); /** @class Node @@ -103,6 +104,9 @@ private: void add_neighbor(NodeId const id); std::vector _neighbors; + NodeId _matched_neighbor {invalid_node_id}; + NodeId _ear_or_root_neighbor {invalid_node_id}; + NodeId _root_of_ear_component {invalid_node_id}; }; // class Node /** @@ -159,6 +163,18 @@ public: @brief Prints the graph to the given ostream in DIMACS format. **/ friend std::ostream & operator<<(std::ostream & str, Graph const & graph); + + NodeId matched_neighbor(NodeId const id) const; + + NodeId ear_or_root_neighbor(NodeId const id) const; + + NodeId root_of_ear_component(NodeId const id) const; + + bool is_outer(NodeId const id) const; + + bool is_inner(NodeId const id) const; + + bool is_out_of_forest(NodeId const id) const; private: std::vector _nodes; size_type _num_edges; @@ -206,6 +222,25 @@ Node const & Graph::node(NodeId const id) const } //END: Inline section + +inline +NodeId Graph::matched_neighbor(NodeId const id) const +{ + return _nodes[id]._matched_neighbor; +} + +inline +NodeId Graph::ear_or_root_neighbor(const ED::NodeId id) const +{ + return _nodes[id]._ear_or_root_neighbor; +} + +inline +NodeId Graph::root_of_ear_component(const ED::NodeId id) const +{ + return _nodes[id]._root_of_ear_component; +} + } // namespace ED #endif /* GRAPH_HPP */