Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

MinSteinerTreeTakahashi.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/Graph.h>
36 #include <ogdf/basic/GraphList.h>
37 #include <ogdf/basic/List.h>
39 #include <ogdf/basic/basic.h>
44 
45 #include <limits>
46 
47 namespace ogdf {
48 template<typename T>
49 class EdgeWeightedGraph;
50 
65 template<typename T>
67 public:
69 
71 
79  virtual T call(const EdgeWeightedGraph<T>& G, const List<node>& terminals,
80  const NodeArray<bool>& isTerminal, EdgeWeightedGraphCopy<T>*& finalSteinerTree,
81  const node startNode) {
82  return call(G, terminals, isTerminal, isTerminal, finalSteinerTree, startNode);
83  }
84 
92  virtual T call(const EdgeWeightedGraph<T>& G, const List<node>& terminals,
93  const NodeArray<bool>& isTerminal, const NodeArray<bool>& isOriginalTerminal,
94  EdgeWeightedGraphCopy<T>*& finalSteinerTree) {
95  return call(G, terminals, isTerminal, isOriginalTerminal, finalSteinerTree,
96  terminals.front());
97  }
98 
100 
109  virtual T call(const EdgeWeightedGraph<T>& G, const List<node>& terminals,
110  const NodeArray<bool>& isTerminal, const NodeArray<bool>& isOriginalTerminal,
111  EdgeWeightedGraphCopy<T>*& finalSteinerTree, const node startNode);
112 
113 protected:
114  virtual T computeSteinerTree(const EdgeWeightedGraph<T>& G, const List<node>& terminals,
115  const NodeArray<bool>& isTerminal, EdgeWeightedGraphCopy<T>*& finalSteinerTree) override {
116  return call(G, terminals, isTerminal, isTerminal, finalSteinerTree, terminals.front());
117  }
118 
129  EdgeWeightedGraphCopy<T>& intermediateTerminalSpanningTree, const node s,
130  int numberOfTerminals, const NodeArray<bool>& isTerminal);
131 };
132 
133 template<typename T>
135  const NodeArray<bool>& isTerminal, const NodeArray<bool>& isOriginalTerminal,
136  EdgeWeightedGraphCopy<T>*& finalSteinerTree, const node startNode) {
138 
139  EdgeWeightedGraphCopy<T> terminalSpanningTree;
140  terminalSpanningTree.setOriginalGraph(G);
141  terminalDijkstra(G, terminalSpanningTree, startNode, terminals.size(), isTerminal);
142 
143  finalSteinerTree = new EdgeWeightedGraphCopy<T>(G);
144  for (node u : G.nodes) {
145  if (!terminalSpanningTree.copy(u)) {
146  finalSteinerTree->delNode(finalSteinerTree->copy(u));
147  }
148  }
149 
150  T mstWeight = makeMinimumSpanningTree(*finalSteinerTree, finalSteinerTree->edgeWeights());
151  mstWeight -= MinSteinerTreeModule<T>::pruneAllDanglingSteinerPaths(*finalSteinerTree,
152  isOriginalTerminal);
153 
154  return mstWeight;
155 }
156 
157 template<typename T>
159  EdgeWeightedGraphCopy<T>& intermediateTerminalSpanningTree, const node s,
160  int numberOfTerminals, const NodeArray<bool>& isTerminal) {
161  NodeArray<edge> predecessor(wG, nullptr);
162  NodeArray<T> distance(wG, std::numeric_limits<T>::max());
163  distance[s] = 0;
164  NodeArray<T> bestDistance(wG, std::numeric_limits<T>::max());
165  bestDistance[s] = 0;
166  NodeArray<bool> isInQueue(wG, true);
167 
168  PrioritizedMapQueue<node, T> queue(wG); //priority queue
169  for (node v : wG.nodes) {
170  queue.push(v, distance[v]);
171  }
172 
173  T mstWeight = 0;
174  int terminalsFound = 1;
175  while (!queue.empty() && terminalsFound < numberOfTerminals) {
176  node v = queue.topElement();
177  queue.pop();
178  isInQueue[v] = false;
179  bestDistance[v] = distance[v];
180  if (isTerminal[v] && distance[v] > 0) {
181  ++terminalsFound;
182  // insert path from new node to old tree
183  node tmpT = intermediateTerminalSpanningTree.newNode(v);
184  while (distance[v] > 0) {
185  distance[v] = 0;
186  queue.push(v, distance[v]);
187  isInQueue[v] = true;
188  const edge e = predecessor[v];
189  OGDF_ASSERT(e);
190  const node w = e->opposite(v);
191  node tmpS = intermediateTerminalSpanningTree.copy(w);
192  if (!tmpS) {
193  tmpS = intermediateTerminalSpanningTree.newNode(w);
194  }
195  edge tmpE;
196  if (e->target() == v) {
197  tmpE = intermediateTerminalSpanningTree.newEdge(tmpS, tmpT, wG.weight(e));
198  } else {
199  tmpE = intermediateTerminalSpanningTree.newEdge(tmpT, tmpS, wG.weight(e));
200  }
201  mstWeight += wG.weight(e);
202  intermediateTerminalSpanningTree.setEdge(e, tmpE);
203  tmpT = tmpS;
204  v = w;
205  }
206  } else { // !isTerminal[v] || distance[v] == 0
207  for (adjEntry adj : v->adjEntries) {
208  const node w = adj->twinNode();
209  const edge e = adj->theEdge();
210  if (distance[w] > distance[v] + wG.weight(e) && bestDistance[w] >= distance[w]) {
211  distance[w] = distance[v] + wG.weight(e);
212  if (!isInQueue[w]) {
213  queue.push(w, distance[w]);
214  isInQueue[w] = true;
215  } else {
216  queue.decrease(w, distance[w]);
217  }
218  predecessor[w] = e;
219  }
220  }
221  }
222  }
223  return mstWeight;
224 }
225 
226 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
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::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::EdgeWeightedGraph::weight
T weight(const edge e) const
Definition: EdgeWeightedGraph.h:59
PriorityQueue.h
Priority queue interface wrapping various heaps.
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::EdgeWeightedGraphCopy::setOriginalGraph
void setOriginalGraph(const Graph *wG) override
Associates the graph copy with G, but does not create any nodes or edges.
Definition: EdgeWeightedGraphCopy.h:162
extended_graph_alg.h
Declaration of extended graph algorithms.
ogdf::EdgeWeightedGraphCopy::newEdge
edge newEdge(node u, node v, T weight)
Definition: EdgeWeightedGraphCopy.h:169
ogdf::MinSteinerTreeTakahashi::call
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, const NodeArray< bool > &isOriginalTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
An extended call method with intermediate and final (original) terminals.
Definition: MinSteinerTreeTakahashi.h:92
ogdf::isConnected
bool isConnected(const Graph &G)
Returns true iff G is connected.
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
EdgeWeightedGraphCopy.h
Extends the GraphCopy concept to weighted graphs.
ogdf::MinSteinerTreeModule
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
Definition: MinSteinerTreeModule.h:72
MinSteinerTreeModule.h
Declaration of ogdf::MinSteinerTreeModule.
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::EdgeWeightedGraph
Definition: GraphIO.h:56
GraphList.h
Decralation of GraphElement and GraphList classes.
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::MinSteinerTreeModule::pruneAllDanglingSteinerPaths
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prunes nonterminal leaves and their paths to terminal or branching nodes.
Definition: MinSteinerTreeModule.h:501
ogdf::GraphCopyBase::newNode
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:192
ogdf::MinSteinerTreeTakahashi::MinSteinerTreeTakahashi
MinSteinerTreeTakahashi()
Definition: MinSteinerTreeTakahashi.h:68
ogdf::MinSteinerTreeTakahashi::terminalDijkstra
T terminalDijkstra(const EdgeWeightedGraph< T > &wG, EdgeWeightedGraphCopy< T > &intermediateTerminalSpanningTree, const node s, int numberOfTerminals, const NodeArray< bool > &isTerminal)
Modified Dijkstra algorithm to solve the Minimum Steiner Tree problem.
Definition: MinSteinerTreeTakahashi.h:158
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::GraphCopyBase::delNode
void delNode(node v) override
Removes node v.
Definition: GraphCopy.h:207
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
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:175
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::setEdge
void setEdge(edge eOrig, edge eCopy)
sets eOrig to be the corresponding original edge of eCopy and vice versa
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
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::EdgeWeightedGraphCopy::edgeWeights
const EdgeArray< T > & edgeWeights() const
Definition: EdgeWeightedGraphCopy.h:65
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
ogdf::EdgeWeightedGraphCopy
Definition: EdgeWeightedGraphCopy.h:44
ogdf::EdgeElement::opposite
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:413
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
ogdf::MinSteinerTreeTakahashi::computeSteinerTree
virtual T computeSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree) override
Computes the actual Steiner tree.
Definition: MinSteinerTreeTakahashi.h:114
List.h
Declaration of doubly linked lists and iterators.
ogdf::List::size
int size() const
Returns the number of elements in the list.
Definition: List.h:1488
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:401
ogdf::MinSteinerTreeTakahashi::~MinSteinerTreeTakahashi
virtual ~MinSteinerTreeTakahashi()
Definition: MinSteinerTreeTakahashi.h:70
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
simple_graph_alg.h
Declaration of simple graph algorithms.