Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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>
40#include <ogdf/basic/basic.h>
41#include <ogdf/basic/comparer.h>
42#include <ogdf/basic/internal/config_autogen.h>
45
46namespace ogdf {
47class ClusterGraph;
48template<class E>
49class List;
50
52
60template<class NODELISTITERATOR, class EDGELIST>
61void 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
88
90
100 bool simple = true);
101
105
107
116template<typename T>
117inline 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
133template<typename T>
134inline 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
148template<typename T>
149inline void computeMinST(const Graph& G, const EdgeArray<T>& weight, NodeArray<edge>& pred) {
150 computeMinST(G.firstNode(), G, weight, pred);
151}
152
154
163template<typename T>
164void 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
206template<typename T>
207T 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
242template<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
284inline bool isPlanar(const Graph& G) { return BoyerMyrvold().isPlanar(G); }
285
297inline 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
318inline bool planarEmbed(Graph& G) { return BoyerMyrvold().planarEmbed(G); }
319
331inline 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}
Declaration and implementation of Array class and Array algorithms.
Declaration of the wrapper class of the Boyer-Myrvold planarity test.
Implementation of disjoint sets data structures (union-find functionality).
Includes declaration of graph class.
Declaration of graph copy classes.
Decralation of GraphElement and GraphList classes.
Priority queue interface wrapping various heaps.
Basic declarations, included by all source files.
Class for adjacency list elements.
Definition Graph_d.h:143
The parameterized class Array implements dynamic arrays of type E.
Definition Array.h:219
void quicksort()
Sorts array using Quicksort.
Definition Array.h:639
Wrapper class used for preprocessing and valid invocation of the planarity test.
virtual bool planarEmbed(Graph &G) override
Returns true, if G is planar, false otherwise. If true, G contains a planar embedding.
virtual bool isPlanar(const Graph &g) override
Returns true, iff a copy of the constant graph g is planar.
virtual bool planarEmbedPlanarGraph(Graph &G) override
Constructs a planar embedding of G. G has to be planar!
Representation of clustered graphs.
A Union/Find data structure for maintaining disjoint sets.
int makeSet()
Initializes a singleton set.
int find(disjoint_sets::CompressionOption< CompressionOptions::PathCompression >, int set)
int link(disjoint_sets::LinkOption< LinkOptions::Naive >, int set1, int set2)
Class for the representation of edges.
Definition Graph_d.h:364
node target() const
Returns the target node of the edge.
Definition Graph_d.h:402
node source() const
Returns the source node of the edge.
Definition Graph_d.h:399
Copies of graphs supporting edge splitting.
Definition GraphCopy.h:390
edge copy(edge e) const override
Returns the first edge in the list of edges corresponding to edge e.
Definition GraphCopy.h:463
edge newEdge(edge eOrig)
Creates a new edge (v,w) with original edge eOrig.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
virtual void delEdge(edge e)
Removes edge e from the graph.
edge newEdge(node v, node w, int index=-1)
Creates a new edge (v,w) and returns it.
Definition Graph_d.h:1080
Doubly linked lists (maintaining the length of the list).
Definition List.h:1451
Class for the representation of nodes.
Definition Graph_d.h:241
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition Graph_d.h:272
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition Graph_d.h:287
const Graph * graphOf() const
Returns the graph containing this node (debug only).
Definition Graph_d.h:345
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer.
Definition comparer.h:295
Prioritized queue interface wrapper for heaps.
bool empty() const
Checks whether the queue is empty.
void init(const Base *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
void pop()
Removes the topmost element from the queue.
void push(const E &element, const P &priority)
Adds a new element to the queue.
const P & priority(const E &element) const
const E & topElement() const
Returns the topmost element in the queue.
Declarations for Comparer objects.
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
Decralation of graph iterators.
void makeCConnected(ClusterGraph &C, Graph &G, List< edge > &addedEdges, bool simple=true)
Makes a cluster graph c-connected by adding edges.
bool isCConnected(const ClusterGraph &C)
Returns true iff cluster graph C is c-connected.
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal's algorithm.
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim's algorithm.
bool planarEmbedPlanarGraph(Graph &G)
Constructs a planar embedding of G. It assumes that G is planar!
bool planarEmbed(Graph &G)
Returns true, if G is planar, false otherwise. If true is returned, G will be planarly embedded.
bool planarSTEmbed(Graph &graph, node s, node t)
s-t-planarly embeds a graph.
bool isSTPlanar(const Graph &graph, const node s, const node t)
Returns whether G is s-t-planar (i.e.
bool isPlanar(const Graph &G)
Returns true, if G is planar, false otherwise.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.
void inducedSubgraph(Graph &G, NODELISTITERATOR &it, EDGELIST &E)
Computes the edges in a node-induced subgraph.