|
| 1 | +/* LeftRightPlanarityCheck.hpp |
| 2 | + * |
| 3 | + * Created on: 03.01.2025 |
| 4 | + * Authors: Andreas Scharf ([email protected]) |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef NETWORKIT_PLANARITY_LEFT_RIGHT_PLANARITY_CHECK_HPP_ |
| 9 | +#define NETWORKIT_PLANARITY_LEFT_RIGHT_PLANARITY_CHECK_HPP_ |
| 10 | +#include <networkit/base/Algorithm.hpp> |
| 11 | +#include <networkit/graph/Graph.hpp> |
| 12 | + |
| 13 | +namespace NetworKit { |
| 14 | + |
| 15 | +class LeftRightPlanarityCheck final : public Algorithm { |
| 16 | + |
| 17 | +public: |
| 18 | + /** |
| 19 | + * Implements the left-right planarity test as described in |
| 20 | + * [citation](https://citeseerx.ist.psu.edu/document?repid=rep1&type=pdf&doi=7963e9feffe1c9362eb1a69010a5139d1da3661e). |
| 21 | + * This algorithm determines whether a graph is planar, i.e., whether it can be drawn on a plane |
| 22 | + * without any edges crossing. For an overview of planar graphs, refer to: |
| 23 | + * https://en.wikipedia.org/wiki/Planar_graph |
| 24 | + * |
| 25 | + * The algorithm achieves (almost) linear runtime complexity. The only non-linear component |
| 26 | + * arises from sorting the nodes of the depth-first search tree. |
| 27 | + * |
| 28 | + * @param G The input graph to test for planarity. The graph should be undirected. |
| 29 | + * @throws std::runtime_error if graph is not an undirected graph |
| 30 | + */ |
| 31 | + LeftRightPlanarityCheck(const Graph &G) : graph(&G) { |
| 32 | + if (G.isDirected()) { |
| 33 | + throw std::runtime_error("The graph is not an undirected graph."); |
| 34 | + } |
| 35 | + dfsGraph = Graph(graph->numberOfNodes(), false, true, false); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Executes the left-right planarity test on the input graph. |
| 40 | + * This method performs all necessary computations to determine |
| 41 | + * whether the graph is planar and prepares the result for retrieval |
| 42 | + * via the `isPlanar()` method. |
| 43 | + */ |
| 44 | + void run() override; |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns whether the input graph is planar. |
| 48 | + * The result is only valid after the `run()` method has been called. |
| 49 | + * |
| 50 | + * @return True if the graph is planar, false otherwise. |
| 51 | + * @throws std::runtime_error if called before `run()` has been executed. |
| 52 | + */ |
| 53 | + bool isPlanar() const { |
| 54 | + assureFinished(); |
| 55 | + return isGraphPlanar; |
| 56 | + } |
| 57 | + |
| 58 | +private: |
| 59 | + static const Edge noneEdge; |
| 60 | + static constexpr count noneHeight{std::numeric_limits<count>::max()}; |
| 61 | + |
| 62 | + struct Interval { |
| 63 | + Edge low{noneEdge}; |
| 64 | + Edge high{noneEdge}; |
| 65 | + |
| 66 | + Interval() : low{noneEdge}, high{noneEdge} {}; |
| 67 | + Interval(const Edge &low, const Edge &high) : low(low), high(high) {} |
| 68 | + bool isEmpty() const { return low == noneEdge && high == noneEdge; } |
| 69 | + |
| 70 | + friend bool operator==(const Interval &lhs, const Interval &rhs) { |
| 71 | + return lhs.low == rhs.low && lhs.high == rhs.high; |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + struct ConflictPair { |
| 76 | + Interval left{}; |
| 77 | + Interval right{}; |
| 78 | + |
| 79 | + ConflictPair() = default; |
| 80 | + ConflictPair(const Interval &left, const Interval &right) : left(left), right(right) {} |
| 81 | + |
| 82 | + void swap() { std::swap(left, right); } |
| 83 | + |
| 84 | + friend bool operator==(const ConflictPair &lhs, const ConflictPair &rhs) { |
| 85 | + return lhs.left == rhs.left && lhs.right == rhs.right; |
| 86 | + } |
| 87 | + }; |
| 88 | + const ConflictPair NoneConflictPair{Interval(), Interval()}; |
| 89 | + |
| 90 | + const Graph *graph; |
| 91 | + bool isGraphPlanar{}; |
| 92 | + void dfsOrientation(node startNode); |
| 93 | + bool dfsTesting(node startNode); |
| 94 | + bool applyConstraints(const Edge &edge, const Edge &parentEdge); |
| 95 | + void removeBackEdges(const Edge &edge); |
| 96 | + void sortAdjacencyListByNestingDepth(); |
| 97 | + bool conflicting(const Interval &interval, const Edge &edge); |
| 98 | + count getLowestLowPoint(const ConflictPair &conflictPair); |
| 99 | + std::vector<count> heights; |
| 100 | + std::unordered_map<Edge, count> lowestPoint; |
| 101 | + std::unordered_map<Edge, count> secondLowestPoint; |
| 102 | + std::unordered_map<Edge, Edge> ref; |
| 103 | + std::vector<node> roots; |
| 104 | + std::unordered_map<Edge, Edge> lowestPointEdge; |
| 105 | + std::unordered_map<Edge, count> nestingDepth; |
| 106 | + std::unordered_map<index, Edge> parentEdges; |
| 107 | + std::stack<ConflictPair> stack; |
| 108 | + std::unordered_map<Edge, ConflictPair> stackBottom; |
| 109 | + Graph dfsGraph; |
| 110 | +}; |
| 111 | +} // namespace NetworKit |
| 112 | +#endif // NETWORKIT_PLANARITY_LEFT_RIGHT_PLANARITY_CHECK_HPP_ |
0 commit comments