Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

PlanarSubgraphTriangles.h
Go to the documentation of this file.
1 
32 #pragma once
33 
37 
38 namespace ogdf {
39 
41 
53 template<typename TCost>
55 {
56 public:
57 
59 
63  PlanarSubgraphTriangles(bool onlyTriangles = false) : m_onlyTriangles(onlyTriangles) { }
64 
66  virtual PlanarSubgraphTriangles *clone() const override {
68  }
69 
70 protected:
71  virtual Module::ReturnType doCall(const Graph &graph,
72  const List<edge> &,
73  List<edge> &delEdges,
74  const EdgeArray<TCost> *pCost,
75  bool preferredImplyPlanar = false) override
76  {
77  OGDF_ASSERT(isConnected(graph));
79 
80  delEdges.clear();
81  GraphCopy copy(graph);
82  List<edge> edges;
83  copy.allEdges(edges);
84  EdgeArray<bool> includeEdges(copy, false);
85 
86  // sort weighted edges
87  if(pCost != nullptr) {
88  GenericComparer<edge, TCost, false> edgeCmp([&](edge e) { return (*pCost)[copy.original(e)]; });
89  GenericComparer<adjEntry, TCost, false> adjCmp([&](adjEntry a) { return (*pCost)[copy.original(a->theEdge())]; });
90  edges.quicksort(edgeCmp);
91 
92  for(node v : copy.nodes) {
93  List<adjEntry> newOrder;
94  v->allAdjEntries(newOrder);
95  newOrder.quicksort(adjCmp);
96  copy.sort(v, newOrder);
97  }
98  }
99 
100  DisjointSets<> components(copy.numberOfNodes());
101  NodeArray<int> set(copy);
102  for (node v : copy.nodes) {
103  set[v] = components.makeSet();
104  }
105 
106  if (!m_onlyTriangles) {
107  // First step: Find as many diamonds as we can.
108  for (edge currentEdge : edges) {
109 
110  // Skip if we already include this edge
111  if (includeEdges[currentEdge]) continue;
112 
113  // We assume this edge to be the cord of our diamond. This means we have to find two
114  // distinct triangles to make a diamond, and we will try to prefer ones with higher
115  // weights given by our pCost parameter
116 
117  node source = currentEdge->source();
118  node target = currentEdge->target();
119 
120  edge triangleEdge1 = nullptr, triangleEdge2 = nullptr;
121  node triangleNode = nullptr;
122  int triangleSet = -1;
123 
124  findTriangle(copy, currentEdge, pCost, components, set, [&](node v, edge e1, edge e2) {
125  // Only use the found triangle if none of the nodes are in the same component.
126  // We know that each individually found triangle does not have two nodes in the
127  // same component, so we only have to check the opposite ones.
128  int potentialSet = components.find(set[v]);
129  if (potentialSet == triangleSet) return false; // This is not a triangle we can use, keep looking!
130 
131  if (triangleNode == nullptr) {
132  // We don't have a triangle yet, mark this one down as the best we can find from this edge.
133  triangleNode = v;
134  triangleEdge1 = e1;
135  triangleEdge2 = e2;
136  triangleSet = potentialSet;
137  return false; // continue searching for a second triangle to make our diamond
138  }
139  else {
140  // We already found a triangle before, so this is the second-best triangle we can find,
141  // making a diamond.
142  includeEdges[currentEdge] = includeEdges[triangleEdge1] = includeEdges[triangleEdge2]
143  = includeEdges[e1] = includeEdges[e2] = true;
144 
145  // Link up diamond nodes' components. These cannot be on the same connected subgraph yet.
146  int sourceSet = components.find(set[source]);
147  int targetSet = components.find(set[target]);
148  components.link(components.link(components.link(sourceSet, targetSet), triangleSet), potentialSet);
149  return true; // stop looking
150  }
151  });
152  }
153  }
154 
155  // Second step: Find as many triangles as we can.
156  for (edge currentEdge : edges) {
157 
158  if (includeEdges[currentEdge]) continue;
159 
160  node source = currentEdge->source();
161  node target = currentEdge->target();
162 
163  findTriangle(copy, currentEdge, pCost, components, set,[&](node v, edge e1, edge e2) {
164  includeEdges[currentEdge] = includeEdges[e1] = includeEdges[e2] = true;
165  int potentialSet = components.find(set[v]);
166  int sourceSet = components.find(set[source]);
167  int targetSet = components.find(set[target]);
168  components.link(components.link(sourceSet, targetSet), potentialSet);
169  return true;
170  });
171 
172  }
173 
174  // Third step: Link unconnected sub graphs
175  for (edge currentEdge : edges) {
176  node source = currentEdge->source();
177  node target = currentEdge->target();
178  int sourceSet = components.find(set[source]);
179  int targetSet = components.find(set[target]);
180  if (sourceSet != targetSet) {
181  includeEdges[currentEdge] = true;
182  components.link(sourceSet, targetSet);
183  }
184 
185  if (!includeEdges[currentEdge]) delEdges.pushBack(copy.original(currentEdge));
186  }
187 
189  }
190 
191 
192 private:
194 
196 
204  edge searchEdge(node target, node source, internal::GraphIterator<adjEntry> connectionIterator) {
205  while (connectionIterator != source->adjEntries.end()) {
206  if ((*connectionIterator)->twinNode() == target) {
207  return (*connectionIterator)->theEdge();
208  }
209  connectionIterator++;
210  }
211  return nullptr;
212  }
213 
215 
231  void findTriangle(GraphCopy& copy, edge currentEdge, const EdgeArray<TCost> *pCost,
232  DisjointSets<>& components, NodeArray<int>& set,
233  std::function<bool(node, edge, edge)> callback) {
234  node source = currentEdge->source();
235  node target = currentEdge->target();
236  auto sourceIt = source->adjEntries.begin();
237  auto targetIt = target->adjEntries.begin();
238  int sourceSet = components.find(set[source]);
239  int targetSet = components.find(set[target]);
240  // Our nodes cannot be in the same set.
241  if (sourceSet == targetSet) return;
242 
243  while (sourceIt != source->adjEntries.end() && targetIt != target->adjEntries.end()) {
244  if ((*sourceIt)->theEdge() == currentEdge) {
245  sourceIt++;
246  continue; // re-check loop condition
247  }
248  if ((*targetIt)->theEdge() == currentEdge) {
249  targetIt++;
250  continue; // re-check loop condition
251  }
252 
253  // Look for a triangle, starting with the edge with next highest weight
254  // If no weights are given, it does not matter which edge we start with
255  adjEntry potentialConnector;
256  node potentialConnection;
257  internal::GraphIterator<adjEntry> potentialConnectionIterator;
258  if (pCost == nullptr || (*pCost)[copy.original((*sourceIt)->theEdge())] > (*pCost)[copy.original((*targetIt)->theEdge())]) {
259  potentialConnector = *sourceIt;
260  potentialConnection = target;
261  potentialConnectionIterator = targetIt;
262  sourceIt++;
263  }
264  else {
265  potentialConnector = *targetIt;
266  potentialConnection = source;
267  potentialConnectionIterator = sourceIt;
268  targetIt++;
269  }
270  // Note: sourceIt and targetIt may be invalid until next loop
271 
272  node potentialNode = potentialConnector->twinNode();
273  int potentialSet = components.find(set[potentialNode]);
274 
275  // Only use this edge if it doesn't connect back to one of the components
276  if (potentialSet != sourceSet && potentialSet != targetSet) {
277  edge potentialEdge = searchEdge(potentialNode, potentialConnection, potentialConnectionIterator);
278  if (potentialEdge != nullptr) {
279  // We found a triangle.
280  // If our callback returns true, it's signalling that we're done and don't
281  // want to look for another triangle.
282  // If it returns false, we continue looking.
283  if (callback(potentialNode, potentialEdge, potentialConnector->theEdge())) {
284  return;
285  }
286  }
287  }
288  }
289  }
290 };
291 
292 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
void clear()
Removes all elements from the list.
Definition: List.h:1451
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
void sort(node v, const ADJ_ENTRY_LIST &newOrder)
Sorts the adjacency list of node v according to newOrder.
Definition: Graph_d.h:1051
Maximum planar subgraph approximation algorithms by Chalermsook/Schmid and Calinescu et al...
ReturnType
The return type of a module.
Definition: Module.h:51
void findTriangle(GraphCopy &copy, edge currentEdge, const EdgeArray< TCost > *pCost, DisjointSets<> &components, NodeArray< int > &set, std::function< bool(node, edge, edge)> callback)
Finds all triangles from a given edge and calls a callback function on them.
void quicksort()
Sorts the list using Quicksort.
Definition: List.h:1152
Interface for planar subgraph algorithms.
static void copy(const T &from, T &to)
Definition: Alg.h:61
The solution is feasible.
PlanarSubgraphTriangles(bool onlyTriangles=false)
Creates a planarization module based on triangle or diamond matching.
int find(disjoint_sets::CompressionOption< CompressionOptions::PathCompression >, int set)
Definition: DisjointSets.h:273
edge searchEdge(node target, node source, internal::GraphIterator< adjEntry > connectionIterator)
Finds an edge, starting at a given iterator.
A Union/Find data structure for maintaining disjoint sets.
Definition: DisjointSets.h:81
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:108
Class for the representation of edges.
Definition: Graph_d.h:292
void allAdjEntries(ADJLIST &adjList) const
Returns a list with all adjacency entries of this node.
Definition: Graph_d.h:228
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:249
Class for the representation of nodes.
Definition: Graph_d.h:169
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:286
virtual Module::ReturnType doCall(const Graph &graph, const List< edge > &, List< edge > &delEdges, const EdgeArray< TCost > *pCost, bool preferredImplyPlanar=false) override
Computes the set of edges delEdges which have to be deleted to obtain the planar subgraph.
bool isSimpleUndirected(const Graph &G)
Returns true iff G contains neither self-loops nor undirected parallel edges.
Declaration of simple graph algorithms.
Implementation of disjoint sets data structures (union-find functionality).
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
bool m_onlyTriangles
Whether we want to only check for triangles.
The namespace for all OGDF objects.
void allEdges(CONTAINER &edgeContainer) const
Returns a container with all edges of the graph.
Definition: Graph_d.h:661
Declaration of interface for planar subgraph algorithms.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:598
Compare elements based on a single comparable attribute.
Definition: comparer.h:377
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
virtual PlanarSubgraphTriangles * clone() const override
Returns a new instance of the planarization module with the same settings.
bool isConnected(const Graph &G)
Returns true iff G is connected.
Class for adjacency list elements.
Definition: Graph_d.h:79
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200