Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

extended_graph_alg.h
Go to the documentation of this file.
1 
32 #pragma once
33 
38 
39 
40 namespace ogdf {
41 
44 
46 
55 template<class LISTITERATOR>
56 void inducedSubGraph(const Graph &G, LISTITERATOR start, Graph &subGraph)
57 {
58  NodeArray<node> nodeTableOrig2New;
59  inducedSubGraph(G,start,subGraph,nodeTableOrig2New);
60 }
61 
63 
73 template<class LISTITERATOR>
75  const Graph &G,
76  LISTITERATOR start,
77  Graph &subGraph,
78  NodeArray<node> &nodeTableOrig2New)
79 {
80  subGraph.clear();
81  nodeTableOrig2New.init(G,nullptr);
82 
83  EdgeArray<bool> mark(G,false);
84 
85  LISTITERATOR its;
86  for (its = start; its.valid(); its++)
87  {
88  node w = (*its);
89  OGDF_ASSERT(w != nullptr);
90  OGDF_ASSERT(w->graphOf() == &G);
91  nodeTableOrig2New[w] = subGraph.newNode();
92 
93  for(adjEntry adj : w->adjEntries)
94  {
95  edge e = adj->theEdge();
96  if (nodeTableOrig2New[e->source()] && nodeTableOrig2New[e->target()] && !mark[e])
97  {
98  subGraph.newEdge(nodeTableOrig2New[e->source()],nodeTableOrig2New[e->target()]);
99  mark[e] = true;
100  }
101  }
102  }
103 }
104 
105 
107 
118 template<class LISTITERATOR>
120  const Graph &G,
121  LISTITERATOR start,
122  Graph &subGraph,
123  NodeArray<node> &nodeTableOrig2New,
124  EdgeArray<edge> &edgeTableOrig2New)
125 {
126  subGraph.clear();
127  nodeTableOrig2New.init(G,nullptr);
128  edgeTableOrig2New.init(G,nullptr);
129 
130  EdgeArray<bool> mark(G,false);
131 
132  LISTITERATOR its;
133  for (its = start; its.valid(); its++)
134  {
135  node w = (*its);
136  OGDF_ASSERT(w != nullptr);
137  OGDF_ASSERT(w->graphOf() == &G);
138  nodeTableOrig2New[w] = subGraph.newNode();
139 
140  for(adjEntry adj : w->adjEntries)
141  {
142  edge e = adj->theEdge();
143  if (nodeTableOrig2New[e->source()] &&
144  nodeTableOrig2New[e->target()] &&
145  !mark[e])
146  {
147  edgeTableOrig2New[e] =
148  subGraph.newEdge(
149  nodeTableOrig2New[e->source()],
150  nodeTableOrig2New[e->target()]);
151  mark[e] = true;
152  }
153  }
154  }
155 }
156 
157 
159 
169 template<class NODELISTITERATOR, class EDGELIST>
170 void inducedSubgraph(Graph &G, NODELISTITERATOR &it, EDGELIST &E)
171 {
172  NODELISTITERATOR itBegin = it;
173  NodeArray<bool> mark(G,false);
174 
175  for (;it.valid();it++)
176  mark[*it] = true;
177  it = itBegin;
178  for (;it.valid();it++)
179  {
180  node v = (*it);
181  for(adjEntry adj : v->adjEntries)
182  {
183  edge e = adj->theEdge();
184  if (mark[e->source()] && mark[e->target()])
185  E.pushBack(e);
186  }
187  }
188 }
189 
190 
194 
196 
199 OGDF_EXPORT bool isCConnected(const ClusterGraph &C);
200 
202 
212  ClusterGraph& C,
213  Graph& G,
214  List<edge>& addedEdges,
215  bool simple = true);
216 
217 
221 
223 
232 template<typename T>
233 inline T computeMinST(const Graph &G, const EdgeArray<T> &weight, EdgeArray<bool> &isInTree)
234 {
235  NodeArray<edge> pred(G, nullptr);
236  return computeMinST(G.firstNode(), G, weight, pred, isInTree);
237 }
238 
240 
250 template<typename T>
251 inline T computeMinST(const Graph &G, const EdgeArray<T> &weight, NodeArray<edge> &pred, EdgeArray<bool> &isInTree)
252 {
253  return computeMinST(G.firstNode(), G, weight, pred, isInTree);
254 }
255 
257 
263 template<typename T>
264 inline void computeMinST(const Graph &G, const EdgeArray<T> &weight, NodeArray<edge> &pred)
265 {
266  computeMinST(G.firstNode(), G, weight, pred);
267 }
268 
270 
279 template<typename T>
280 void computeMinST(node s, const Graph &G, const EdgeArray<T> &weight, NodeArray<edge> &pred)
281 {
282  PrioritizedMapQueue<node, T> pq(G); // priority queue of front vertices
283 
284  // insert start node
285  T tmp(0);
286  pq.push(s, tmp);
287 
288  // extract the nodes again along a minimum ST
289  NodeArray<bool> processed(G, false);
290  pred.init(G, nullptr);
291 
292  while (!pq.empty()) {
293  const node v = pq.topElement();
294  pq.pop();
295  processed[v] = true;
296  for (adjEntry adj = v->firstAdj(); adj; adj = adj->succ()) {
297  const node w = adj->twinNode();
298  const edge e = adj->theEdge();
299  if (pred[w] == nullptr && w != s) {
300  tmp = weight[e];
301  pq.push(w, tmp);
302  pred[w] = e;
303  } else
304  if (!processed[w]
305  && weight[e] < pq.priority(w)) {
306  pq.decrease(w, weight[e]);
307  pred[w] = e;
308  }
309  }
310  }
311 }
312 
314 
323 template<typename T>
324 T computeMinST(node s, const Graph &G, const EdgeArray<T> &weight, NodeArray<edge> &pred, EdgeArray<bool> &isInTree)
325 {
326  computeMinST(s, G, weight, pred);
327 
328  // now just compute isInTree and total weight
329  int rootcount = 0;
330  T treeWeight = 0;
331  isInTree.init(G, false);
332  for (node v = G.firstNode(); v; v = v->succ()) {
333  if (!pred[v]) {
334  ++rootcount;
335  } else {
336  isInTree[pred[v]] = true;
337  treeWeight += weight[pred[v]];
338  }
339  }
340  OGDF_ASSERT(rootcount == 1); // is connected
341 
342  return treeWeight;
343 }
344 
346 
354 template<typename T>
356 {
357  T total(0);
359  int i = 0;
360  for (edge e : G.edges) {
361  sortEdges[i++] = Prioritized<edge,T>(e, weight[e]);
362  }
363  sortEdges.quicksort();
364 
365  // now let's do Kruskal's algorithm
366  NodeArray<int> setID(G);
368  for (node v : G.nodes) {
369  setID[v] = uf.makeSet();
370  }
371 
372  for (auto prioEdge : sortEdges) {
373  const edge e = prioEdge.item();
374  const int v = setID[e->source()];
375  const int w = setID[e->target()];
376  if (uf.find(v) != uf.find(w)) {
377  uf.link(uf.find(v), uf.find(w));
378  total += weight[e];
379  } else {
380  G.delEdge(e);
381  }
382  }
383  return total;
384 }
385 
387 
389 
397 inline bool isPlanar(const Graph &G) {
398  return BoyerMyrvold().isPlanar(G);
399 }
400 
410 inline bool isSTPlanar(
411  const Graph &graph,
412  const node s,
413  const node t)
414 {
415  OGDF_ASSERT(s != nullptr);
416  OGDF_ASSERT(t != nullptr);
417  OGDF_ASSERT(s->graphOf() == &graph);
418  OGDF_ASSERT(t->graphOf() == &graph);
419 
420  GraphCopy copy(graph);
421  copy.newEdge(copy.copy(s), copy.copy(t));
422 
423  return isPlanar(copy);
424 }
425 
427 
435 inline bool planarEmbed(Graph &G) {
436  return BoyerMyrvold().planarEmbed(G);
437 }
438 
448 inline bool planarSTEmbed(Graph &graph, node s, node t)
449 {
450  edge e = graph.newEdge(s, t);
451  bool result = planarEmbed(graph);
452  graph.delEdge(e);
453 
454  return result;
455 }
456 
457 
459 
472 inline bool planarEmbedPlanarGraph(Graph &G) {
474 }
475 
476 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:200
Derived class of GraphObserver providing additional functionality to handle clustered graphs...
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
Wrapper class used for preprocessing and valid invocation of the planarity test.
Definition: BoyerMyrvold.h:138
virtual void clear()
Removes all nodes and all edges from the graph.
void init()
Reinitializes the array. Associates the array with no graph.
Definition: EdgeArray.h:283
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:255
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim&#39;s algorithm.
void makeCConnected(ClusterGraph &C, Graph &G, List< edge > &addedEdges, bool simple=true)
Makes a cluster graph c-connected by adding edges.
void inducedSubgraph(Graph &G, NODELISTITERATOR &it, EDGELIST &E)
Computes the edges in a node-induced subgraph.
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:618
virtual void delEdge(edge e)
Removes edge e from the graph.
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal&#39;s algorithm.
bool planarSTEmbed(Graph &graph, node s, node t)
s-t-planarly embeds a graph.
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:218
virtual bool planarEmbed(Graph &G) override
Returns true, if G is planar, false otherwise. If true, G contains a planar embedding.
Definition: BoyerMyrvold.h:174
Prioritized queue interface wrapper for heaps.
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer...
Definition: comparer.h:279
Representation of clustered graphs.
Definition: ClusterGraph.h:291
virtual bool planarEmbedPlanarGraph(Graph &G) override
Constructs a planar embedding of G. G has to be planar!
Definition: BoyerMyrvold.h:188
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:568
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:601
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:438
const E & topElement() const
Returns the topmost element in the queue.
bool planarEmbed(Graph &G)
Returns true, if G is planar, false otherwise. If true is returned, G will be planarly embedded...
static void copy(const T &from, T &to)
Definition: Alg.h:61
edge newEdge(node v, node w)
Creates a new edge (v,w) and returns it.
A Union/Find data structure for maintaining disjoint sets.
Definition: DisjointSets.h:81
bool isPlanar(const Graph &G)
Returns true, if G is planar, false otherwise.
Class for the representation of edges.
Definition: Graph_d.h:292
bool planarEmbedPlanarGraph(Graph &G)
Constructs a planar embedding of G. It assumes that G is planar!
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:249
bool isCConnected(const ClusterGraph &C)
Returns true iff cluster graph C is c-connected.
Class for the representation of nodes.
Definition: Graph_d.h:169
Priority queue interface wrapping various heaps.
bool isSTPlanar(const Graph &graph, const node s, const node t)
Returns whether G is s-t-planar (i.e.
Declaration of the wrapper class of the Boyer-Myrvold planarity test.
bool empty() const
Checks whether the queue is empty.
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:148
virtual bool isPlanar(const Graph &g) override
Returns true, iff a copy of the constant graph g is planar.
Implementation of disjoint sets data structures (union-find functionality).
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
The namespace for all OGDF objects.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:598
node newNode()
Creates a new node and returns it.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:213
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
void inducedSubGraph(const Graph &G, LISTITERATOR start, Graph &subGraph)
Computes the subgraph induced by a list of nodes.
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200