Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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
46namespace ogdf::steiner_tree {
47template<typename T>
48class Triple;
49} // namespace ogdf::steiner_tree
50
51//#define OGDF_COMMON_ALG_FIND_BEST_TAKAHASHI_ROOT
52
53
54namespace ogdf {
55template<typename T>
56class EdgeWeightedGraph;
57
58namespace steiner_tree {
59
68template<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
158template<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
172template<typename T>
174 edge e1, edge e2) {
175 edge ne0, ne1;
176 contractTripleInSteinerTree(t, st, e0, e1, e2, ne0, ne1);
177}
178
179template<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}
Declaration and implementation of Array class and Array algorithms.
Extends the GraphCopy concept to weighted graphs.
Includes declaration of graph class.
Declaration of doubly linked lists and iterators.
Declaration of ogdf::MinSteinerTreeModule.
Implementation of the 2(1-1/l)-approximation algorithm for the minimum Steiner tree problem by Matsuy...
Basic declarations, included by all source files.
The parameterized class Array implements dynamic arrays of type E.
Definition Array.h:219
void quicksort()
Sorts array using Quicksort.
Definition Array.h:639
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
edge newEdge(node u, node v, T weight)
edge copy(edge e) const override
Returns the first edge in the list of edges corresponding to edge e.
Definition GraphCopy.h:463
void delEdge(edge e) override
Removes edge e and clears the list of edges corresponding to e's original edge.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
int numberOfEdges() const
Returns the number of edges in the graph.
Definition Graph_d.h:982
node newNode(int index=-1)
Creates a new node and returns it.
Definition Graph_d.h:1061
edge newEdge(node v, node w, int index=-1)
Creates a new edge (v,w) and returns it.
Definition Graph_d.h:1080
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition Graph_d.h:932
Doubly linked lists (maintaining the length of the list).
Definition List.h:1451
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition List.h:1547
static void getTerminals(List< node > &terminals, const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Generates a list (as List<node>) of all terminals.
This class implements the minimum Steiner tree 2-approximation algorithm by Takahashi and Matsuyama w...
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.
Class for the representation of nodes.
Definition Graph_d.h:241
Augments any data elements of type X with keys of type Priority. This class is also its own Comparer.
Definition comparer.h:295
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
This class represents a triple used by various contraction-based minimum Steiner tree approximations.
Definition Triple.h:44
Declarations for Comparer objects.
NodeElement * node
The type of nodes.
Definition Graph_d.h:71
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
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...
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...
T obtainFinalSteinerTree(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const NodeArray< bool > &isOriginalTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
The namespace for all OGDF objects.