Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

extended_graph_alg.h
Go to the documentation of this file.
1 
32 #pragma once
33 
38 
39 namespace ogdf {
40 
42 
50 template<class NODELISTITERATOR, class EDGELIST>
51 void inducedSubgraph(Graph& G, NODELISTITERATOR& it, EDGELIST& E) {
52  NODELISTITERATOR itBegin = it;
53  NodeArray<bool> mark(G, false);
54 
55  for (; it.valid(); it++) {
56  mark[*it] = true;
57  }
58  it = itBegin;
59  for (; it.valid(); it++) {
60  node v = (*it);
61  for (adjEntry adj : v->adjEntries) {
62  edge e = adj->theEdge();
63  if (mark[e->source()] && mark[e->target()]) {
64  E.pushBack(e);
65  }
66  }
67  }
68 }
69 
72 
74 
77 OGDF_EXPORT bool isCConnected(const ClusterGraph& C);
78 
80 
89 OGDF_EXPORT void makeCConnected(ClusterGraph& C, Graph& G, List<edge>& addedEdges,
90  bool simple = true);
91 
95 
97 
106 template<typename T>
107 inline T computeMinST(const Graph& G, const EdgeArray<T>& weight, EdgeArray<bool>& isInTree) {
108  NodeArray<edge> pred(G, nullptr);
109  return computeMinST(G.firstNode(), G, weight, pred, isInTree);
110 }
111 
113 
123 template<typename T>
124 inline T computeMinST(const Graph& G, const EdgeArray<T>& weight, NodeArray<edge>& pred,
125  EdgeArray<bool>& isInTree) {
126  return computeMinST(G.firstNode(), G, weight, pred, isInTree);
127 }
128 
130 
138 template<typename T>
139 inline void computeMinST(const Graph& G, const EdgeArray<T>& weight, NodeArray<edge>& pred) {
140  computeMinST(G.firstNode(), G, weight, pred);
141 }
142 
144 
153 template<typename T>
154 void computeMinST(node s, const Graph& G, const EdgeArray<T>& weight, NodeArray<edge>& pred) {
155  PrioritizedMapQueue<node, T> pq(G); // priority queue of front vertices
156 
157  // insert start node
158  T tmp(0);
159  pq.push(s, tmp);
160 
161  // extract the nodes again along a minimum ST
162  NodeArray<bool> processed(G, false);
163  pred.init(G, nullptr);
164 
165  while (!pq.empty()) {
166  const node v = pq.topElement();
167  pq.pop();
168  processed[v] = true;
169  for (adjEntry adj = v->firstAdj(); adj; adj = adj->succ()) {
170  const node w = adj->twinNode();
171  const edge e = adj->theEdge();
172  if (pred[w] == nullptr && w != s) {
173  tmp = weight[e];
174  pq.push(w, tmp);
175  pred[w] = e;
176  } else if (!processed[w] && weight[e] < pq.priority(w)) {
177  pq.decrease(w, weight[e]);
178  pred[w] = e;
179  }
180  }
181  }
182 }
183 
185 
196 template<typename T>
197 T computeMinST(node s, const Graph& G, const EdgeArray<T>& weight, NodeArray<edge>& pred,
198  EdgeArray<bool>& isInTree) {
199  computeMinST(s, G, weight, pred);
200 
201  // now just compute isInTree and total weight
202 #ifdef OGDF_DEBUG
203  int rootcount = 0;
204 #endif
205  T treeWeight = 0;
206  isInTree.init(G, false);
207  for (node v = G.firstNode(); v; v = v->succ()) {
208  if (pred[v]) {
209  isInTree[pred[v]] = true;
210  treeWeight += weight[pred[v]];
211  }
212 #ifdef OGDF_DEBUG
213  else {
214  ++rootcount;
215  }
216 #endif
217  }
218  OGDF_ASSERT(rootcount == 1); // is connected
219 
220  return treeWeight;
221 }
222 
224 
232 template<typename T>
234  T total(0);
235  Array<Prioritized<edge, T>> sortEdges(G.numberOfEdges());
236  int i = 0;
237  for (edge e : G.edges) {
238  sortEdges[i++] = Prioritized<edge, T>(e, weight[e]);
239  }
240  sortEdges.quicksort();
241 
242  // now let's do Kruskal's algorithm
243  NodeArray<int> setID(G);
244  DisjointSets<> uf(G.numberOfNodes());
245  for (node v : G.nodes) {
246  setID[v] = uf.makeSet();
247  }
248 
249  for (auto prioEdge : sortEdges) {
250  const edge e = prioEdge.item();
251  const int v = setID[e->source()];
252  const int w = setID[e->target()];
253  if (uf.find(v) != uf.find(w)) {
254  uf.link(uf.find(v), uf.find(w));
255  total += weight[e];
256  } else {
257  G.delEdge(e);
258  }
259  }
260  return total;
261 }
262 
264 
266 
274 inline bool isPlanar(const Graph& G) { return BoyerMyrvold().isPlanar(G); }
275 
287 inline bool isSTPlanar(const Graph& graph, const node s, const node t) {
288  OGDF_ASSERT(s != nullptr);
289  OGDF_ASSERT(t != nullptr);
290  OGDF_ASSERT(s->graphOf() == &graph);
291  OGDF_ASSERT(t->graphOf() == &graph);
292 
293  GraphCopy copy(graph);
294  copy.newEdge(copy.copy(s), copy.copy(t));
295 
296  return isPlanar(copy);
297 }
298 
300 
308 inline bool planarEmbed(Graph& G) { return BoyerMyrvold().planarEmbed(G); }
309 
321 inline bool planarSTEmbed(Graph& graph, node s, node t) {
322  edge e = graph.newEdge(s, t);
323  bool result = planarEmbed(graph);
324  graph.delEdge(e);
325 
326  return result;
327 }
328 
330 
344 
345 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
BoyerMyrvold.h
Declaration of the wrapper class of the Boyer-Myrvold planarity test.
ogdf::BoyerMyrvold::planarEmbed
virtual bool planarEmbed(Graph &G) override
Returns true, if G is planar, false otherwise. If true, G contains a planar embedding.
Definition: BoyerMyrvold.h:177
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::pq_internal::PrioritizedArrayQueueBase< E, P, std::less< P >, PairingHeap, HashArray< E, PrioritizedQueue< E, P, std::less< P >, PairingHeap >::Handle, DefHashFunc< E > > >::pop
void pop()
Removes the topmost element from the queue.
Definition: PriorityQueue.h:339
ogdf::inducedSubgraph
void inducedSubgraph(Graph &G, NODELISTITERATOR &it, EDGELIST &E)
Computes the edges in a node-induced subgraph.
Definition: extended_graph_alg.h:51
PriorityQueue.h
Priority queue interface wrapping various heaps.
ogdf::makeCConnected
void makeCConnected(ClusterGraph &C, Graph &G, List< edge > &addedEdges, bool simple=true)
Makes a cluster graph c-connected by adding edges.
ogdf::pq_internal::PrioritizedArrayQueueBase< E, P, std::less< P >, PairingHeap, HashArray< E, PrioritizedQueue< E, P, std::less< P >, PairingHeap >::Handle, DefHashFunc< E > > >::push
void push(const E &element, const P &priority)
Adds a new element to the queue.
Definition: PriorityQueue.h:331
ogdf::DisjointSets::find
int find(disjoint_sets::CompressionOption< CompressionOptions::PathCompression >, int set)
Definition: DisjointSets.h:332
ogdf::BoyerMyrvold::planarEmbedPlanarGraph
virtual bool planarEmbedPlanarGraph(Graph &G) override
Constructs a planar embedding of G. G has to be planar!
Definition: BoyerMyrvold.h:191
ogdf::computeMinST
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim's algorithm.
Definition: extended_graph_alg.h:107
ogdf::GraphCopy
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:384
ogdf::DisjointSets::makeSet
int makeSet()
Initializes a singleton set.
Definition: DisjointSets.h:235
ogdf::pq_internal::PrioritizedArrayQueueBase< E, P, std::less< P >, PairingHeap, HashArray< E, PrioritizedQueue< E, P, std::less< P >, PairingHeap >::Handle, DefHashFunc< E > > >::priority
const P & priority(const E &element) const
Definition: PriorityQueue.h:322
ogdf::isPlanar
bool isPlanar(const Graph &G)
Returns true, if G is planar, false otherwise.
Definition: extended_graph_alg.h:274
ogdf::Graph::delEdge
virtual void delEdge(edge e)
Removes edge e from the graph.
ogdf::planarSTEmbed
bool planarSTEmbed(Graph &graph, node s, node t)
s-t-planarly embeds a graph.
Definition: extended_graph_alg.h:321
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:135
ogdf::BoyerMyrvold::isPlanar
virtual bool isPlanar(const Graph &g) override
Returns true, iff a copy of the constant graph g is planar.
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:153
ogdf::PriorityQueue::empty
bool empty() const
Checks whether the queue is empty.
Definition: PriorityQueue.h:209
ogdf::DisjointSets::link
int link(disjoint_sets::LinkOption< LinkOptions::Naive >, int set1, int set2)
Definition: DisjointSets.h:624
Minisat::Internal::copy
static void copy(const T &from, T &to)
Definition: Alg.h:61
ogdf::DisjointSets
A Union/Find data structure for maintaining disjoint sets.
Definition: DisjointSets.h:90
ogdf::Array
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:214
ogdf::AdjElement::succ
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:211
ogdf::PrioritizedMapQueue
Prioritized queue interface wrapper for heaps.
Definition: PriorityQueue.h:409
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:264
ogdf::planarEmbed
bool planarEmbed(Graph &G)
Returns true, if G is planar, false otherwise. If true is returned, G will be planarly embedded.
Definition: extended_graph_alg.h:308
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:391
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::pq_internal::PrioritizedQueue< E, P, std::less< P >, PairingHeap >::topElement
const E & topElement() const
Returns the topmost element in the queue.
Definition: PriorityQueue.h:284
ogdf::BoyerMyrvold
Wrapper class used for preprocessing and valid invocation of the planarity test.
Definition: BoyerMyrvold.h:138
ogdf::pq_internal::PrioritizedArrayQueueBase< E, P, std::less< P >, PairingHeap, HashArray< E, PrioritizedQueue< E, P, std::less< P >, PairingHeap >::Handle, DefHashFunc< E > > >::decrease
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
Definition: PriorityQueue.h:349
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
ogdf::NodeElement::firstAdj
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:279
ogdf::makeMinimumSpanningTree
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal's algorithm.
Definition: extended_graph_alg.h:233
DisjointSets.h
Implementation of disjoint sets data structures (union-find functionality).
ogdf::isCConnected
bool isCConnected(const ClusterGraph &C)
Returns true iff cluster graph C is c-connected.
ogdf::Prioritized
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer.
Definition: comparer.h:291
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
ClusterGraph.h
Derived class of GraphObserver providing additional functionality to handle clustered graphs.
ogdf::NodeElement::graphOf
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition: Graph_d.h:337
ogdf::RegisteredArray< GraphRegistry< Key >, Value, WithDefault, Graph >::init
void init(const Graph *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
Definition: RegisteredArray.h:849
ogdf::Array::quicksort
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:634
ogdf::planarEmbedPlanarGraph
bool planarEmbedPlanarGraph(Graph &G)
Constructs a planar embedding of G. It assumes that G is planar!
Definition: extended_graph_alg.h:343
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:394
ogdf::Graph::newEdge
edge newEdge(node v, node w, int index=-1)
Creates a new edge (v,w) and returns it.
Definition: Graph_d.h:1075
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::isSTPlanar
bool isSTPlanar(const Graph &graph, const node s, const node t)
Returns whether G is s-t-planar (i.e.
Definition: extended_graph_alg.h:287
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709