60class EdgeWeightedGraph;
157 sssp.
call(G, G.edgeWeights(), source, pred, distance);
164#ifdef OGDF_MINSTEINERTREEMODULE_SHORTEST_PATHS_STANDARD
244#ifdef OGDF_MINSTEINERTREEMODULE_SHORTEST_PATHS_STANDARD
274 const char* filename) {
277 drawSVG(G, isTerminal, emptySteinerTree, filename);
322 for (
node v : G.nodes) {
343 for (
node v : G.nodes) {
344 if (!isTerminal[v]) {
345 nonterminals.
push(v);
370 for (
node u : G.nodes) {
371 const T duv = distance[u][v];
372 if (duv < std::numeric_limits<T>::max()) {
374 if (distance[v][w] < std::numeric_limits<T>::max()) {
375 func(u, w, duv + distance[v][w]);
383 template<
typename NODELIST>
392 for (
node u : nodes) {
393 ssspFunc(G, u, isTerminal, distance[u], pred[u]);
403 if (terminals.
size() > 2) {
404 return this->computeSteinerTree(G, terminals, isTerminal, finalSteinerTree);
409 if (!terminals.
empty()) {
412 if (terminals.
size() <= 1) {
421 astar.
call(G, G.edgeWeights(), terminals.
front(), terminals.
back(), pred);
423 for (
node t = terminals.
back(); t != terminals.
front(); t = pred[t]->opposite(t)) {
424 const edge e = pred[t];
426 finalSteinerTree->
newEdge(e, G.weight(e));
437 if (!
isTree(steinerTree)) {
442 for (
node v : terminals) {
444 if (!u || (terminals.
size() > 1 && u->
degree() < 1)) {
451 if (!isTerminal[steinerTree.
original(u)] && u->degree() <= 1) {
462 for (
node v = G.firstNode(); v; v = v->
succ()) {
463 if (!isTerminal[v]) {
464 for (
adjEntry adj = v->firstAdj(); adj; adj = adj->
succ()) {
465 if (!isTerminal[adj->twinNode()]) {
480 while (u->
degree() == 1 && !isTerminal[steinerTree.
original(u)]) {
494 for (
node v : start) {
495 delWeights += pruneDanglingSteinerPathFrom(steinerTree, isTerminal, v);
505 if (u->degree() == 1 && !isTerminal[steinerTree.
original(u)]) {
510 return pruneDanglingSteinerPathsFrom(steinerTree, isTerminal, start);
522 for (
edge nextEdge, e = steinerTree.
firstEdge(); e; e = nextEdge) {
523 oldCost += steinerTree.
weight(e);
524 nextEdge = e->
succ();
526 if (e->source()->degree() == 2) {
529 if (e->target()->degree() == 2) {
537 return oldCost - newCost;
545 distance.
init(G, std::numeric_limits<T>::max());
546 distance[source] = 0;
548 for (
node v : G.nodes) {
549 queue.
push(v, distance[v]);
552 pred.
init(G,
nullptr);
560 ssspInit(G, source, queue, distance, pred);
567 edge e = adj->theEdge();
568 node w = adj->twinNode();
569 if (distance[w] > G.weight(
571 queue.
decrease(w, (distance[w] = G.weight(e)));
576 auto setPredecessor = [&](
node w,
edge e) {
577 bool wOnPathWithTerminal = isTerminal[v] || pred[v] ==
nullptr;
578 pred[w] = wOnPathWithTerminal ? nullptr : e;
580 while (!queue.
empty()) {
584 if (distance[v] == std::numeric_limits<T>::max()) {
588 edge e = adj->theEdge();
589 node w = adj->twinNode();
590 T dist = distance[v] + G.weight(e);
591 if (distance[w] > dist) {
592 queue.
decrease(w, (distance[w] = dist));
593 setPredecessor(w, e);
594 }
else if (distance[w] == dist && pred[w] !=
nullptr) {
595 setPredecessor(w, e);
605 apspInit(G, distance, pred);
607 for (
node v : G.nodes) {
609 apspInnerLoop(v, G, distance, [&distance, &pred](
node u,
node w, T duvw) {
610 if (duvw <= distance[u][w]) {
611 distance[w][u] = distance[u][w] = duvw;
612 pred[w][u] = pred[u][w] =
nullptr;
616 apspInnerLoop(v, G, distance, [&v, &distance, &pred](
node u,
node w, T duvw) {
617 if (duvw < distance[u][w]) {
618 distance[w][u] = distance[u][w] = duvw;
619 pred[u][w] = (pred[u][v] ? pred[v][w] :
nullptr);
620 pred[w][u] = (pred[w][v] ? pred[v][u] :
nullptr);
625 for (
node u : G.nodes) {
635 for (
node u : G.nodes) {
636 distance[u].init(G, std::numeric_limits<T>::max());
637 pred[u].init(G,
nullptr);
639 for (
edge e : G.edges) {
640 const node u = e->source(), v = e->target();
641 distance[u][v] = distance[v][u] = G.weight(e);
642 pred[u][v] = pred[v][u] = e;
649 apspInit(G, distance, pred);
651 for (
node v : G.nodes) {
652 apspInnerLoop(v, G, distance, [&v, &distance, &pred](
node u,
node w, T duvw) {
653 if (duvw < distance[u][w]) {
654 distance[w][u] = distance[u][w] = duvw;
655 pred[u][w] = pred[v][w];
656 pred[w][u] = pred[v][u];
660 for (
node u : G.nodes) {
678 std::stringstream out;
680 if (isTerminal[steinerTree.
original(v)]) {
690 GA.
label(v) = out.str();
701 std::ofstream writeStream(filename, std::ofstream::out);
708 const char* filename) {
716 for (
edge e : G.edges) {
718 GA.
label(e) = to_string(G.weight(e));
726 for (
node v : G.nodes) {
727 std::stringstream out;
738 if (steinerTree.
copy(v)) {
746 GA.
label(v) = out.str();
758 std::ofstream writeStream(filename, std::ofstream::out);
Implementation of the A* informed search algorithm.
Declaration and implementation of ArrayBuffer class.
Extends the GraphCopy concept to weighted graphs.
Declaration of Fast Multipole Multilevel Method (FM^3).
Declaration of Fast Multipole Multilevel Method (FM^3) options.
Includes declaration of graph class.
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Declares class GraphIO which provides access to all graph read and write functionality.
Decralation of GraphElement and GraphList classes.
Declaration of doubly linked lists and iterators.
Priority queue interface wrapping various heaps.
Basic declarations, included by all source files.
A-Star informed search algorithm.
T call(const Graph &graph, const EdgeArray< T > &cost, const node source, const node target, NodeArray< edge > &predecessor, std::function< T(node)> heuristic=[](node) { return T(0);})
Computes the shortests path between source and target.
Class for adjacency list elements.
adjEntry succ() const
Returns the successor in the adjacency list.
edge theEdge() const
Returns the edge associated with this adjacency entry.
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
void push(E e)
Puts a new element in the buffer.
Dijkstra's single source shortest path algorithm.
void call(const Graph &G, const EdgeArray< T > &weight, const List< node > &sources, NodeArray< edge > &predecessor, NodeArray< T > &distance, bool directed=false, bool arcsReversed=false, node target=nullptr, T maxLength=std::numeric_limits< T >::max())
Calculates, based on the graph G with corresponding edge costs and source nodes, the shortest paths a...
Class for the representation of edges.
node opposite(node v) const
Returns the adjacent node different from v.
edge succ() const
Returns the successor in the list of all edges.
edge newEdge(node u, node v, T weight)
void setOriginalGraph(const Graph *wG) override
Re-initializes the copy using G (which might be null), but does not create any nodes or edges.
const EdgeArray< T > & edgeWeights() const
T weight(const edge e) const
The fast multipole multilevel layout algorithm.
FMMMOptions::QualityVsSpeed qualityVersusSpeed() const
Returns the current setting of option qualityVersusSpeed.
double unitEdgeLength() const
Returns the current setting of option unitEdgeLength.
bool useHighLevelOptions() const
Returns the current setting of option useHighLevelOptions.
virtual void call(GraphAttributes &GA) override
Calls the algorithm for graph GA and returns the layout information in GA.
bool newInitialPlacement() const
Returns the current setting of option newInitialPlacement.
@ GorgeousAndEfficient
Best quality.
Stores additional attributes of a graph (like layout information).
static const long edgeLabel
Corresponds to edge attribute label(edge).
double height(node v) const
Returns the height of the bounding box of node v.
bool directed() const
Returns if the graph is directed.
const string & label(node v) const
Returns the label of node v.
static const long edgeStyle
Corresponds to edge attributes strokeColor(edge), strokeType(edge), and strokeWidth(edge).
double width(node v) const
Returns the width of the bounding box of node v.
static const long nodeLabel
Corresponds to node attribute label(node).
const Color & fillColor(node v) const
Returns the fill color of node v.
const Color & strokeColor(node v) const
Returns the stroke color of node v.
Shape shape(node v) const
Returns the shape type of node v.
float strokeWidth(node v) const
Returns the stroke width of node v.
static const long nodeStyle
Corresponds to node attributes strokeColor(node), strokeType(node), strokeWidth(node),...
static const long edgeGraphics
Corresponds to edge attribute bends(edge).
static const long nodeGraphics
Corresponds to node attributes x(node), y(node), width(node), height(node), and shape(node).
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
void delNode(node v) override
Removes node v.
const Graph & original() const
Returns a reference to the original graph.
edge copy(edge e) const override
Returns the first edge in the list of edges corresponding to edge e.
void delEdge(edge e) override
Removes edge e and clears the list of edges corresponding to e's original edge.
int numberOfNodes() const
Returns the number of nodes in the graph.
edge firstEdge() const
Returns the first edge in the list of all edges.
int numberOfEdges() const
Returns the number of edges in the graph.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
static bool drawSVG(const GraphAttributes &A, std::ostream &os, const SVGSettings &settings)
Doubly linked lists (maintaining the length of the list).
int size() const
Returns the number of elements in the list.
iterator pushBack(const E &x)
Adds element x at the end of the list.
const_reference front() const
Returns a const reference to the first element.
const_reference back() const
Returns a const reference to the last element.
bool empty() const
Returns true iff the list is empty.
void quicksort()
Sorts the list using Quicksort.
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
static void allNodeShortestPathsStandard(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred)
Runs singleSourceShortestPathsStandard from all nodes.
static void allNodesByListShortestPaths(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, const NODELIST &nodes, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred, std::function< void(const EdgeWeightedGraph< T > &, node, const NodeArray< bool > &, NodeArray< T > &, NodeArray< edge > &)> ssspFunc)
Runs a given (or the default) single-source-shortest-paths function from all nodes.
static bool isQuasiBipartite(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Checks in O(n + m) time if a given Steiner tree problem instance is quasi-bipartite.
static void allTerminalShortestPathsPreferringTerminals(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred)
Runs singleSourceShortestPathsPreferringTerminals from all terminals.
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
Calls the Steiner tree algorithm for nontrivial cases but handles trivial cases directly.
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prunes nonterminal leaves and their paths to terminal or branching nodes.
static void getTerminals(List< node > &terminals, const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Generates a list (as List<node>) of all terminals.
static T pruneDanglingSteinerPathFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal, node start)
Prunes the dangling Steiner path beginning at a given nonterminal leaf only.
static void allPairShortestPathsPreferringTerminals(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred)
Modified all-pair-shortest-paths algorithm (Floyd-Warshall) with heuristic to prefer paths over termi...
static void allTerminalShortestPaths(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred, std::function< void(const EdgeWeightedGraph< T > &, node, const NodeArray< bool > &, NodeArray< T > &, NodeArray< edge > &)> ssspFunc=singleSourceShortestPaths)
Runs a given (or the default) single-source-shortest-paths function from all terminals.
static void apspInit(const EdgeWeightedGraph< T > &G, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred)
Common initialization routine for APSP algorithms.
static void allPairShortestPaths(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred)
The default all-pair-shortest-paths algorithm.
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.
virtual T computeSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)=0
Computes the actual Steiner tree.
static void ssspInit(const EdgeWeightedGraph< T > &G, node source, PrioritizedMapQueue< node, T > &queue, NodeArray< T > &distance, NodeArray< edge > &pred)
Common initialization routine for SSSP algorithms.
static T removeCyclesFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Remove remaining cycles from a Steiner "almost" tree.
static void singleSourceShortestPathsPreferringTerminals(const EdgeWeightedGraph< T > &G, node source, const NodeArray< bool > &isTerminal, NodeArray< T > &distance, NodeArray< edge > &pred)
Modified single-source-shortest-paths (Dijkstra) with heuristic to prefer paths over terminals.
static void drawSVG(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const EdgeWeightedGraphCopy< T > &steinerTree, const char *filename)
Writes an SVG file of a minimum Steiner tree in the original graph.
static void sortTerminals(List< node > &terminals)
Sort terminals by index.
static void allPairShortestPathsStandard(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred)
Standard all-pair-shortest-paths algorithm (Floyd-Warshall)
static void allNodeShortestPaths(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred, std::function< void(const EdgeWeightedGraph< T > &, node, const NodeArray< bool > &, NodeArray< T > &, NodeArray< edge > &)> ssspFunc=singleSourceShortestPaths)
Runs a given (or the default) single-source-shortest-paths function from all nodes.
static void singleSourceShortestPathsStandard(const EdgeWeightedGraph< T > &G, node source, const NodeArray< bool > &, NodeArray< T > &distance, NodeArray< edge > &pred)
Standard single-source-shortest-paths algoritm (Dijkstra)
static void allNodeShortestPathsPreferringTerminals(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred)
Runs singleSourceShortestPathsPreferringTerminals from all nodes.
static void apspInnerLoop(node v, const EdgeWeightedGraph< T > &G, NodeArray< NodeArray< T > > &distance, std::function< void(node, node, T)> func)
The inner loop for APSP algorithm to avoid code duplication.
static bool isSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, const EdgeWeightedGraphCopy< T > &steinerTree)
Checks in O(n) time if a given tree is acually a Steiner Tree.
static void drawSVG(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const char *filename)
Writes an SVG file of the instance graph.
static void drawSteinerTreeSVG(const EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal, const char *filename)
Writes a SVG that shows only the given Steiner tree.
static void allTerminalShortestPathsStandard(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred)
Runs singleSourceShortestPathsStandard from all terminals.
static T pruneDanglingSteinerPathsFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal, const List< node > &start)
Prunes dangling Steiner paths beginning at given nonterminal leaves only.
static void getNonterminals(ArrayBuffer< node > &nonterminals, const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Generates a list (as ArrayBuffer<node>) of all nonterminals.
virtual ~MinSteinerTreeModule()
Do nothing on destruction.
Class for the representation of nodes.
int degree() const
Returns the degree of the node (indegree + outdegree).
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
node succ() const
Returns the successor in the list of all nodes.
int index() const
Returns the (unique) node index.
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Prioritized queue interface wrapper for heaps.
bool empty() const
Checks whether the queue is empty.
void init(const Base *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
RegisteredArray for nodes, edges and adjEntries of a graph.
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
void pop()
Removes the topmost element from the queue.
void push(const E &element, const P &priority)
Adds a new element to the queue.
const E & topElement() const
Returns the topmost element in the queue.
Declarations for Comparer objects.
Declaration of extended graph algorithms.
Implementation of Dijkstra's single source shortest path algorithm.
Declaration of basic types for graphics.
bool isConnected(const Graph &G)
Returns true iff G is connected.
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim's algorithm.
bool isTree(const Graph &G)
Returns true iff G is a tree, i.e. contains no undirected cycle and is connected.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
The namespace for all OGDF objects.
Declaration of simple graph algorithms.
Compare elements based on a single comparable attribute.