Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

common_algorithms.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/Array.h>
36 #include <ogdf/basic/Graph.h>
37 #include <ogdf/basic/List.h>
38 #include <ogdf/basic/basic.h>
39 #include <ogdf/basic/comparer.h>
43 
44 #include <limits>
45 
46 namespace ogdf::steiner_tree {
47 template<typename T>
48 class Triple;
49 } // namespace ogdf::steiner_tree
50 
51 //#define OGDF_COMMON_ALG_FIND_BEST_TAKAHASHI_ROOT
52 
53 
54 namespace ogdf {
55 template<typename T>
56 class EdgeWeightedGraph;
57 
58 namespace steiner_tree {
59 
68 template<typename T>
70  NodeArray<node>& externalNodes,
71  NodeArray<edge>& treeEdge,
72  Graph& outputTree)
73 {
74  // sort edges by weight
75  Array<Prioritized<edge, T>> sortEdges(inputTree.numberOfEdges());
76  int i = 0;
77  for (edge e : inputTree.edges) {
78  sortEdges[i] = Prioritized<edge, T>(e, inputTree.weight(e));
79  ++i;
80  }
81  sortEdges.quicksort();
82 
83  // insert edges into forest (which in the end makes up a tree)
84  NodeArray<node*> root(outputTree);
85  List<node*> garbage;
86  node edgeNode = nullptr;
87  for (i = 0; i < inputTree.numberOfEdges(); ++i) {
88  edgeNode = outputTree.newNode();
89  edge e = sortEdges[i].item();
90  treeEdge[edgeNode] = e;
91 
92  node u = e->source();
93  node v = e->target();
94  if (externalNodes[u]) {
95  node* uRoot = root[externalNodes[u]];
96  OGDF_ASSERT(uRoot);
97  while (root[*uRoot] != uRoot) {
98  *uRoot = *root[*uRoot];
99  uRoot = root[*uRoot];
100  OGDF_ASSERT(uRoot);
101  }
102  outputTree.newEdge(edgeNode, *uRoot);
103  root[edgeNode] = uRoot;
104  if (externalNodes[v]) {
105  node* vRoot = root[externalNodes[v]];
106  OGDF_ASSERT(vRoot);
107  while (root[*vRoot] != vRoot) {
108  *vRoot = *root[*vRoot];
109  vRoot = root[*vRoot];
110  OGDF_ASSERT(vRoot);
111  }
112  outputTree.newEdge(edgeNode, *vRoot);
113  *vRoot = edgeNode;
114  } else {
115  externalNodes[v] = edgeNode;
116  }
117  } else {
118  externalNodes[u] = edgeNode;
119  if (externalNodes[v]) {
120  node* vRoot = root[externalNodes[v]];
121  OGDF_ASSERT(vRoot);
122  while (root[*vRoot] != vRoot) {
123  *vRoot = *root[*vRoot];
124  vRoot = root[*vRoot];
125  OGDF_ASSERT(vRoot);
126  }
127  outputTree.newEdge(edgeNode, *vRoot);
128  root[edgeNode] = vRoot;
129  } else {
130  root[edgeNode] = new node;
131  garbage.pushBack(root[edgeNode]);
132  externalNodes[v] = edgeNode;
133  }
134  }
135  *root[edgeNode] = edgeNode;
136  }
137  OGDF_ASSERT(edgeNode);
138 
139  // garbage collect
140  for (node* p : garbage) {
141  delete p;
142  }
143 
144  return edgeNode;
145 }
146 
158 template<typename T>
160  edge save1, edge save2, edge& ne0, edge& ne1) {
161  if (save0 == save1) {
162  st.delEdge(save1);
163  st.delEdge(save2);
164  } else {
165  st.delEdge(save0);
166  st.delEdge(save1);
167  }
168  ne0 = st.newEdge(st.copy(t.s0()), st.copy(t.s1()), 0);
169  ne1 = st.newEdge(st.copy(t.s0()), st.copy(t.s2()), 0);
170 }
171 
172 template<typename T>
174  edge e1, edge e2) {
175  edge ne0, ne1;
176  contractTripleInSteinerTree(t, st, e0, e1, e2, ne0, ne1);
177 }
178 
179 template<typename T>
181  const NodeArray<bool>& isOriginalTerminal, EdgeWeightedGraphCopy<T>*& finalSteinerTree) {
182  List<node> terminals;
183  MinSteinerTreeModule<T>::getTerminals(terminals, G, isTerminal);
184 
185  finalSteinerTree = nullptr;
187 #ifdef OGDF_COMMON_ALG_FIND_BEST_TAKAHASHI_ROOT
188  // find minimum Steiner tree of G among Takahashi approximations for each start node
189  T bestMstWeight = std::numeric_limits<T>::max();
190  for (node v : terminals) {
191  EdgeWeightedGraphCopy<T>* tmpSteinerTree;
192  T tmpMstWeight = mstt.call(G, terminals, isTerminal, isOriginalTerminal, tmpSteinerTree, v);
193  if (tmpMstWeight < bestMstWeight) {
194  bestMstWeight = tmpMstWeight;
195  delete finalSteinerTree;
196  finalSteinerTree = tmpSteinerTree;
197  } else {
198  delete tmpSteinerTree;
199  }
200  }
201  return bestMstWeight;
202 #else
203  return mstt.call(G, terminals, isTerminal, isOriginalTerminal, finalSteinerTree);
204 #endif
205 }
206 
207 }
208 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
Graph.h
Includes declaration of graph class.
MinSteinerTreeTakahashi.h
Implementation of the 2(1-1/l)-approximation algorithm for the minimum Steiner tree problem by Matsuy...
ogdf::steiner_tree::contractTripleInSteinerTree
void contractTripleInSteinerTree(const Triple< T > &t, EdgeWeightedGraphCopy< T > &st, edge save0, edge save1, edge save2, edge &ne0, edge &ne1)
Updates the Steiner tree by deleting save edges, removing all direct connections between the terminal...
Definition: common_algorithms.h:159
ogdf::steiner_tree::Triple
This class represents a triple used by various contraction-based minimum Steiner tree approximations.
Definition: common_algorithms.h:48
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::MinSteinerTreeTakahashi::call
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree, const node startNode)
An extended call method with specific start node.
Definition: MinSteinerTreeTakahashi.h:79
ogdf::steiner_tree::Triple::s2
node s2() const
Definition: Triple.h:54
ogdf::steiner_tree::obtainFinalSteinerTree
T obtainFinalSteinerTree(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const NodeArray< bool > &isOriginalTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
Definition: common_algorithms.h:180
ogdf::steiner_tree::Triple::s1
node s1() const
Definition: Triple.h:52
ogdf::EdgeWeightedGraphCopy::newEdge
edge newEdge(node u, node v, T weight)
Definition: EdgeWeightedGraphCopy.h:169
EdgeWeightedGraphCopy.h
Extends the GraphCopy concept to weighted graphs.
ogdf::steiner_tree
Definition: MinSteinerTreeMehlhorn.h:297
ogdf::steiner_tree::buildHeaviestEdgeInComponentTree
node buildHeaviestEdgeInComponentTree(const EdgeWeightedGraphCopy< T > &inputTree, NodeArray< node > &externalNodes, NodeArray< edge > &treeEdge, Graph &outputTree)
Given an edge-weighted tree, builds an auxiliary arborescence where each arc of the input tree is a n...
Definition: common_algorithms.h:69
MinSteinerTreeModule.h
Declaration of ogdf::MinSteinerTreeModule.
ogdf::Graph::numberOfEdges
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:985
ogdf::EdgeWeightedGraph
Definition: GraphIO.h:56
ogdf::Array
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:219
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:398
ogdf::node
NodeElement * node
The type of nodes.
Definition: Graph_d.h:70
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: DfsMakeBiconnected.h:40
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::steiner_tree::Triple::s0
node s0() const
Definition: Triple.h:50
ogdf::GraphCopy::copy
edge copy(edge e) const override
Returns the first edge in the list of edges corresponding to edge e.
Definition: GraphCopy.h:464
ogdf::GraphCopy::delEdge
void delEdge(edge e) override
Removes edge e and clears the list of edges corresponding to e's original edge.
ogdf::Graph::edges
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:935
ogdf::MinSteinerTreeTakahashi
This class implements the minimum Steiner tree 2-approximation algorithm by Takahashi and Matsuyama w...
Definition: MinSteinerTreeTakahashi.h:66
basic.h
Basic declarations, included by all source files.
ogdf::Graph::newNode
node newNode(int index=-1)
Creates a new node and returns it.
Definition: Graph_d.h:1064
ogdf::EdgeWeightedGraphCopy::weight
T weight(const edge e) const
Definition: EdgeWeightedGraphCopy.h:61
ogdf::EdgeWeightedGraphCopy
Definition: EdgeWeightedGraphCopy.h:44
Array.h
Declaration and implementation of Array class and Array algorithms.
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
List.h
Declaration of doubly linked lists and iterators.
ogdf::Array::quicksort
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:639
ogdf::MinSteinerTreeModule::getTerminals
static void getTerminals(List< node > &terminals, const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Generates a list (as List<node>) of all terminals.
Definition: MinSteinerTreeModule.h:320
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::List::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1547