41 namespace steiner_tree {
74 : m_original(orig), m_copyOfNode(m_original),
75 m_origOfNode(m_copy, nullptr), m_origOfEdge(m_copy, nullptr),
76 m_isTerminal(m_copy, false) {
80 m_copyOfNode[v] = vCopy;
81 m_origOfNode[vCopy] = v;
85 m_origOfEdge[eCopy] = e;
89 for (
node v : terminals) {
90 m_isTerminal[
copy(v)] =
true;
96 m_copy.
newEdge(m_source,
copy(w), std::numeric_limits<T>::max());
103 return m_copyOfNode[v];
109 return m_origOfNode[v];
115 return m_origOfEdge[e];
155 explicit DWMData(T _cost = std::numeric_limits<T>::max()) : cost(_cost) {}
165 return cost == 0 || !(edges.
empty() && subgraphs.
empty());
171 if (other->
valid()) {
172 subgraphs.
push(other);
197 T cost = std::numeric_limits<T>::max();
205 cost = s1->
cost + s2->cost;
219 return &m_map.
lookup(key)->info();
230 #ifdef OGDF_FULL_COMPONENT_GENERATION_ALWAYS_SAFE 231 return summand1 + summand2 < compareValue;
233 return summand1 < std::numeric_limits<T>::max()
234 && summand2 < std::numeric_limits<T>::max()
235 && summand1 + summand2 < compareValue;
251 if (!inserted && w->
index() > newNode->
index()) {
260 bool inserted =
false;
269 bool insertedIntoSubset =
false;
270 bool insertedIntoComplement =
false;
275 if (!insertedIntoSubset) {
278 if (!insertedIntoComplement) {
296 makeKey(newSubset, newComplement, subset, v);
305 template<
typename CONTAINER>
310 m_terminalSubset.
list(terminals);
328 for (
node t : m_terminals) {
337 if (v->
index() < t->index()) {
345 T dist = distance[v];
347 for (
node curr = v; pred[curr] !=
nullptr; curr = pred[curr]->opposite(curr)) {
348 edges.
push(pred[curr]);
363 cost = split[w].cost;
373 while (curr != m_auxG.
source()) {
378 if (prev != m_auxG.
source()) {
396 DWMData best(distance[v]);
411 best.add(
dataOf(terminals));
413 best.add(split[tO].subgraph1);
414 best.add(split[tO].subgraph2);
421 template<
typename CONTAINER>
423 for (
node v : targets) {
428 if (!m_map.
member(newSubset)) {
430 if (pred[m_auxG.
copy(v)] ==
nullptr) {
465 for (
const DWMData* subgraph : data.
subgraphs) {
482 m_terminals(terminals),
483 m_isTerminal(isTerminal),
484 m_auxG(m_G, m_terminals),
485 m_terminalSubset(m_terminals),
493 for (m_terminalSubset.
begin(2, restricted - 2); m_terminalSubset.
valid(); m_terminalSubset.
next()) {
497 for (m_terminalSubset.
begin(restricted - 1); m_terminalSubset.
valid(); m_terminalSubset.
next()) {
529 static const unsigned int c_prime = 0x7fffffff;
539 unsigned int hash = 0;
541 hash = (hash * m_random + v->index()) % c_prime;
node target() const
Returns the target node of the edge.
HashElement< K, I > * lookup(const K &key) const
Returns the hash element with key key in the hash table; returns nullptr if no such element exists...
bool member(const K &key) const
Returns true iff the hash table contains an element with key key.
ArrayBuffer< edge > edges
node m_source
The source node.
void makeKey(List< node > &newSubset, node v) const
Makes a list from the current terminal subset including an correctly inserted node v...
void clear()
Clears the buffer.
static void sortedInserter(node w, List< node > &list, bool &inserted, node newNode)
Is being used as a callback to ogdf::SubsetEnumerator's forEach* methods to get the subset plus a cor...
node copy(node v) const
Returns the node in the graph copy corresponding to v.
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
EdgeWeightedGraph< T > m_copy
The auxiliary copy.
iterator pushBack(const E &x)
Adds element x at the end of the list.
const NodeArray< bool > & terminalArray() const
Returns a const reference to m_isTerminal.
SubsetEnumerator< node > m_terminalSubset
Handling subsets of terminals.
int randomNumber(int low, int high)
Returns random integer between low and high (including).
int degree() const
Returns the degree of the node (indegree + outdegree).
edge newEdge(node u, node v, T weight)
Declaration of ogdf::MinSteinerTreeModule.
Declaration of classes used for hashing.
void setWeight(const edge e, T weight)
void push(E e)
Puts a new element in the buffer.
ArrayBuffer< const DWMData * > subgraphs
Extends the GraphCopy concept to weighted graphs.
void setWeight(edge e, T value)
Sets the weight of a copied edge.
static void singleSourceShortestPaths(const EdgeWeightedGraph< T > &G, node source, const NodeArray< bool > &isTerminal, NodeArray< T > &distance, NodeArray< edge > &pred)
The default single-source-shortest-paths algorithm.
bool valid() const
Checks if the current subset is valid. If not, the subset is either not initialized or all subsets ha...
Dynamic arrays indexed with edges.
NodeArray< node > m_copyOfNode
A mapping from original nodes to copied nodes.
bool isValidComponent(const EdgeWeightedGraphCopy< T > &tree) const
Checks if a given tree is a valid full component.
AuxiliaryGraph m_auxG
An auxiliary graph to compute partial solutions.
void set(const DWMData *s1, const DWMData *s2)
Sets subgraph1 and subgraph2 and computes cost.
T weight(const edge e) const
void add(edge e, T c)
Adds an edge e of cost c.
edge newEdge(node v, node w, T weight)
T getSteinerTreeFor(const List< node > &terminals, EdgeWeightedGraphCopy< T > &tree) const
Constructs a Steiner tree for the given set of terminals if it is valid, otherwise an empty tree is r...
void createEmpty(const Graph &wG)
EdgeArray< edge > m_origOfEdge
A mapping from copied edges to original edges.
void updateMin(T &min, const T &newValue)
Stores the minimum of min and newValue in min.
void computePartialSolutions(const CONTAINER &targets)
Computes all partial solutions for given m_terminalSubset.
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
edge original(edge e) const
Returns the original edge for the copied edge e.
const DWMData * dataOf(const List< node > &key) const
Returns a pointer to the relevant data of the partial solution given by key.
bool isAcyclicUndirected(const Graph &G, List< edge > &backedges)
Returns true iff the undirected graph G is acyclic.
iterator pushFront(const E &x)
Adds element x at the beginning of the list.
const EdgeWeightedGraph< T > & m_original
A reference to the original graph.
void begin(int low, int high)
Initializes the SubsetEnumerator to enumerate subsets of cardinalities from low to high...
bool empty() const
Returns true if the buffer is empty, false otherwise.
A collection of two subgraphs and their total cost.
Enumerator for k-subsets of a given type.
Doubly linked lists (maintaining the length of the list).
DWMData(T _cost=std::numeric_limits< T >::max())
node copy(node v) const
Returns the copied node of the original node v.
A generator for restricted full components (for Steiner tree approximations) based on the Dreyfus-Wag...
Class for the representation of edges.
const EdgeWeightedGraph< T > & graph() const
Returns a const reference to the graph.
static uint32_t hash(uint32_t x)
T costOf(const List< node > &key) const
Returns the cost of the partial solution given by key.
T getSteinerTreeFor(const DWMData &data, EdgeWeightedGraphCopy< T > &tree) const
Adds edges to construct a Steiner tree from the given data (recursively) if the data is valid...
void updateAuxGraph(NodeArray< DWMSplit > &split, SubsetEnumerator< node > &subset, T oldCost)
Updates the auxiliary graph.
Class for the representation of nodes.
FullComponentGeneratorDreyfusWagnerWithoutMatrix(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal)
The constructor.
Subgraphs (given by other subgraphs and additional edges) and their cost for a partial solution...
node opposite(node v) const
Returns the adjacent node different from v.
const Graph & original() const
Returns a reference to the original graph.
void call(int restricted)
NodeArray< node > m_origOfNode
A mapping from copied nodes to original nodes.
const NodeArray< bool > & m_isTerminal
A reference to the terminal incidence vector.
bool hasMember(const T &element) const
Checks in O(subset cardinality) whether element is a member of the subset.
void forEachMemberAndNonmember(std::function< void(const T &)> funcIn, std::function< void(const T &)> funcNotIn) const
Calls funcIn for each subset member and funcNotIn for each other element of the set.
T weight(edge e) const
Returns the weight of a copied edge.
bool safeIfSumSmaller(const T summand1, const T summand2, const T compareValue) const
Checks overflow-safe if summand1 plus summand2 is less than compareValue.
int index() const
Returns the (unique) node index.
HashElement< K, I > * fastInsert(const K &key, const I &info)
Inserts a new element with key key and information info into the hash table.
void list(List< T > &subset) const
Obtains (appends) a list of the subset members.
node original(node v) const
Returns the original node for the copied node v.
void next()
Obtains the next subset if possible. The result should be checked using the valid() method...
int size() const
Returns the number of elements in the list.
edge addNewPath(DWMData &result, node curr, const NodeArray< edge > &pred) const
Adds the shortest path from the source to curr into result.
bool valid() const
Returns true iff the data is valid.
void forEachMember(std::function< void(const T &)> func) const
Calls func for each member in the subset.
void computeSplit(NodeArray< DWMSplit > &split, node v, SubsetEnumerator< node > &subset) const
Populates split that contains a partial solution for an included nonterminal v in m_G...
bool isTree(const Graph &G)
Returns true iff G is a tree, i.e. contains no undirected cycle and is connected. ...
node source() const
Returns the source node of the edge.
void add(const DWMData *other)
Adds other subgraph to ours.
const List< node > & m_terminals
A reference to the index-sorted list of terminals.
Necessary because ogdf::EdgeWeightedGraphCopy<T> is rubbish.
Hashing with chaining and table doubling.
int size() const
Returns the cardinality of the subset.
The namespace for all OGDF objects.
void initializeMap()
Initializes the hash array with all node-terminal-pairs.
DWMData(T _cost, ArrayBuffer< edge > _edges)
SortedNodeListHashFunc()
Initializes the random number.
void insertBestSubtrees(const CONTAINER &targets, const NodeArray< DWMSplit > &split, const NodeArray< edge > &pred, const NodeArray< T > &distance, const List< node > &terminals)
Inserts the best subtrees into the hash map.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
const EdgeWeightedGraph< T > & m_G
A reference to the graph instance.
int numberOfMembersAndNonmembers() const
Returns the cardinality of the (super-)set. This is the maximum size that can be used for a subset...
unsigned int hash(const List< node > &key) const
The actual hash function.
node source() const
Returns the source node.
void clear()
Remove all subgraphs and edges and set cost to zero.
void invalidate()
Invalidates the data.
NodeArray< bool > m_isTerminal
True for terminals in the auxiliary graph.
A class that allows to enumerate k-subsets.
AuxiliaryGraph(const EdgeWeightedGraph< T > &orig, const List< node > &terminals)
Constructs a copy of the original graph with an added source node having edges to all other nodes...
bool empty() const
Returns true iff the graph is empty, i.e., contains no nodes.
void insertInvalidBestSubtree(node v, const NodeArray< T > &distance, const List< node > &newSubset)
Inserts the invalid best subtree (based on the SSSP computation) into the hash map.
Class for adjacency list elements.
void makeKey(List< node > &newSubset, List< node > &newComplement, const SubsetEnumerator< node > &subset, node v) const
Makes a list from subset and its complement, each including an correctly inserted node v...
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
void insertValidBestSubtree(node v, const NodeArray< DWMSplit > &split, const NodeArray< edge > &pred, const List< node > &newSubset, const List< node > &terminals)
Inserts the valid best subtree (based on the SSSP computation) into the hash map. ...
Hashing< List< node >, DWMData, SortedNodeListHashFunc > m_map
A hash array for keys of size > 2.
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.