Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

extended_graph_alg.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Array.h>
36 #include <ogdf/basic/Graph.h>
37 #include <ogdf/basic/GraphCopy.h>
38 #include <ogdf/basic/GraphList.h>
40 #include <ogdf/basic/basic.h>
41 #include <ogdf/basic/comparer.h>
45 
46 namespace ogdf {
47 class ClusterGraph;
48 template<class E>
49 class List;
50 
52 
60 template<class NODELISTITERATOR, class EDGELIST>
61 void inducedSubgraph(Graph& G, NODELISTITERATOR& it, EDGELIST& E) {
62  NODELISTITERATOR itBegin = it;
63  NodeArray<bool> mark(G, false);
64 
65  for (; it.valid(); it++) {
66  mark[*it] = true;
67  }
68  it = itBegin;
69  for (; it.valid(); it++) {
70  node v = (*it);
71  for (adjEntry adj : v->adjEntries) {
72  edge e = adj->theEdge();
73  if (mark[e->source()] && mark[e->target()]) {
74  E.pushBack(e);
75  }
76  }
77  }
78 }
79 
82 
84 
87 OGDF_EXPORT bool isCConnected(const ClusterGraph& C);
88 
90 
99 OGDF_EXPORT void makeCConnected(ClusterGraph& C, Graph& G, List<edge>& addedEdges,
100  bool simple = true);
101 
105 
107 
116 template<typename T>
117 inline T computeMinST(const Graph& G, const EdgeArray<T>& weight, EdgeArray<bool>& isInTree) {
118  NodeArray<edge> pred(G, nullptr);
119  return computeMinST(G.firstNode(), G, weight, pred, isInTree);
120 }
121 
123 
133 template<typename T>
134 inline T computeMinST(const Graph& G, const EdgeArray<T>& weight, NodeArray<edge>& pred,
135  EdgeArray<bool>& isInTree) {
136  return computeMinST(G.firstNode(), G, weight, pred, isInTree);
137 }
138 
140 
148 template<typename T>
149 inline void computeMinST(const Graph& G, const EdgeArray<T>& weight, NodeArray<edge>& pred) {
150  computeMinST(G.firstNode(), G, weight, pred);
151 }
152 
154 
163 template<typename T>
164 void computeMinST(node s, const Graph& G, const EdgeArray<T>& weight, NodeArray<edge>& pred) {
165  PrioritizedMapQueue<node, T> pq(G); // priority queue of front vertices
166 
167  // insert start node
168  T tmp(0);
169  pq.push(s, tmp);
170 
171  // extract the nodes again along a minimum ST
172  NodeArray<bool> processed(G, false);
173  pred.init(G, nullptr);
174 
175  while (!pq.empty()) {
176  const node v = pq.topElement();
177  pq.pop();
178  processed[v] = true;
179  for (adjEntry adj = v->firstAdj(); adj; adj = adj->succ()) {
180  const node w = adj->twinNode();
181  const edge e = adj->theEdge();
182  if (pred[w] == nullptr && w != s) {
183  tmp = weight[e];
184  pq.push(w, tmp);
185  pred[w] = e;
186  } else if (!processed[w] && weight[e] < pq.priority(w)) {
187  pq.decrease(w, weight[e]);
188  pred[w] = e;
189  }
190  }
191  }
192 }
193 
195 
206 template<typename T>
207 T computeMinST(node s, const Graph& G, const EdgeArray<T>& weight, NodeArray<edge>& pred,
208  EdgeArray<bool>& isInTree) {
209  computeMinST(s, G, weight, pred);
210 
211  // now just compute isInTree and total weight
212 #ifdef OGDF_DEBUG
213  int rootcount = 0;
214 #endif
215  T treeWeight = 0;
216  isInTree.init(G, false);
217  for (node v = G.firstNode(); v; v = v->succ()) {
218  if (pred[v]) {
219  isInTree[pred[v]] = true;
220  treeWeight += weight[pred[v]];
221  }
222 #ifdef OGDF_DEBUG
223  else {
224  ++rootcount;
225  }
226 #endif
227  }
228  OGDF_ASSERT(rootcount == 1); // is connected
229 
230  return treeWeight;
231 }
232 
234 
242 template<typename T>
244  T total(0);
245  Array<Prioritized<edge, T>> sortEdges(G.numberOfEdges());
246  int i = 0;
247  for (edge e : G.edges) {
248  sortEdges[i++] = Prioritized<edge, T>(e, weight[e]);
249  }
250  sortEdges.quicksort();
251 
252  // now let's do Kruskal's algorithm
253  NodeArray<int> setID(G);
254  DisjointSets<> uf(G.numberOfNodes());
255  for (node v : G.nodes) {
256  setID[v] = uf.makeSet();
257  }
258 
259  for (auto prioEdge : sortEdges) {
260  const edge e = prioEdge.item();
261  const int v = setID[e->source()];
262  const int w = setID[e->target()];
263  if (uf.find(v) != uf.find(w)) {
264  uf.link(uf.find(v), uf.find(w));
265  total += weight[e];
266  } else {
267  G.delEdge(e);
268  }
269  }
270  return total;
271 }
272 
274 
276 
284 inline bool isPlanar(const Graph& G) { return BoyerMyrvold().isPlanar(G); }
285 
297 inline bool isSTPlanar(const Graph& graph, const node s, const node t) {
298  OGDF_ASSERT(s != nullptr);
299  OGDF_ASSERT(t != nullptr);
300  OGDF_ASSERT(s->graphOf() == &graph);
301  OGDF_ASSERT(t->graphOf() == &graph);
302 
303  GraphCopy copy(graph);
304  copy.newEdge(copy.copy(s), copy.copy(t));
305 
306  return isPlanar(copy);
307 }
308 
310 
318 inline bool planarEmbed(Graph& G) { return BoyerMyrvold().planarEmbed(G); }
319 
331 inline bool planarSTEmbed(Graph& graph, node s, node t) {
332  edge e = graph.newEdge(s, t);
333  bool result = planarEmbed(graph);
334  graph.delEdge(e);
335 
336  return result;
337 }
338 
340 
354 
355 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
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:178
Graph.h
Includes declaration of graph class.
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
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:341
ogdf::inducedSubgraph
void inducedSubgraph(Graph &G, NODELISTITERATOR &it, EDGELIST &E)
Computes the edges in a node-induced subgraph.
Definition: extended_graph_alg.h:61
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:333
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:192
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:117
ogdf::GraphCopy
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:391
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:324
ogdf::isPlanar
bool isPlanar(const Graph &G)
Returns true, if G is planar, false otherwise.
Definition: extended_graph_alg.h:284
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:331
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
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:160
ogdf::PriorityQueue::empty
bool empty() const
Checks whether the queue is empty.
Definition: PriorityQueue.h:211
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:219
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::AdjElement::succ
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:218
ogdf::PrioritizedMapQueue
Prioritized queue interface wrapper for heaps.
Definition: PriorityQueue.h:411
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:271
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:318
config_autogen.h
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:398
GraphCopy.h
Declaration of graph copy classes.
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
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:286
graph_iterators.h
Decralation of graph iterators.
ogdf::BoyerMyrvold
Wrapper class used for preprocessing and valid invocation of the planarity test.
Definition: BoyerMyrvold.h:139
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:351
basic.h
Basic declarations, included by all source files.
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:286
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:243
DisjointSets.h
Implementation of disjoint sets data structures (union-find functionality).
Array.h
Declaration and implementation of Array class and Array algorithms.
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:295
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
ogdf::NodeElement::graphOf
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition: Graph_d.h:344
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:858
ogdf::Array::quicksort
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:639
ogdf::planarEmbedPlanarGraph
bool planarEmbedPlanarGraph(Graph &G)
Constructs a planar embedding of G. It assumes that G is planar!
Definition: extended_graph_alg.h:353
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:401
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:1083
comparer.h
Declarations for Comparer objects.
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
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:297
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716