146 #ifdef OGDF_MINSTEINERTREEMODULE_SHORTEST_PATHS_STANDARD 203 allNodeShortestPaths(G, terminals, isTerminal, distance, pred, singleSourceShortestPathsPreferringTerminals);
234 #ifdef OGDF_MINSTEINERTREEMODULE_SHORTEST_PATHS_STANDARD 265 drawSVG(G, isTerminal, emptySteinerTree, filename);
331 if (!isTerminal[v]) {
332 nonterminals.
push(v);
359 const T duv = distance[u][v];
360 if (duv < std::numeric_limits<T>::max()) {
362 if (distance[v][w] < std::numeric_limits<T>::max()) {
363 func(u, w, duv + distance[v][w]);
371 template<
typename NODELIST>
376 const NODELIST& nodes,
382 for (
node u : nodes) {
383 ssspFunc(G, u, isTerminal, distance[u], pred[u]);
397 if (terminals.
size() > 2) {
403 if (!terminals.
empty()) {
406 if (terminals.
size() <= 1) {
417 for (
node t = terminals.
back(); t != terminals.
front(); t = pred[t]->opposite(t)) {
418 const edge e = pred[t];
434 if (!
isTree(steinerTree)) {
439 for(
node v : terminals) {
441 if (!u || (terminals.size() > 1 && u->
degree() < 1)) {
448 if (!isTerminal[steinerTree.
original(u)]
461 if (!isTerminal[v]) {
462 for (
adjEntry adj = v->firstAdj(); adj; adj = adj->
succ()) {
463 if (!isTerminal[adj->twinNode()]) {
479 && !isTerminal[steinerTree.
original(u)]) {
493 for (
node v : start) {
505 && !isTerminal[steinerTree.
original(u)]) {
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) {
536 return oldCost - newCost;
544 distance.
init(G, std::numeric_limits<T>::max());
545 distance[source] = 0;
548 queue.
push(v, distance[v]);
551 pred.
init(G,
nullptr);
559 ssspInit(G, source, queue, distance, pred);
568 if (distance[w] > G.
weight(e)) {
574 auto setPredecessor = [&](
node w,
edge e) {
575 bool wOnPathWithTerminal = isTerminal[v] || pred[v] ==
nullptr;
576 pred[w] = wOnPathWithTerminal ? nullptr : e;
578 while (!queue.
empty()) {
582 if (distance[v] == std::numeric_limits<T>::max()) {
588 T dist = distance[v] + G.
weight(e);
589 if (distance[w] > dist) {
590 queue.
decrease(w, (distance[w] = dist));
591 setPredecessor(w, e);
593 if (distance[w] == dist
594 && pred[w] !=
nullptr) {
595 setPredecessor(w, e);
609 if (duvw <= distance[u][w]) {
610 distance[w][u] = distance[u][w] = duvw;
611 pred[w][u] = pred[u][w] =
nullptr;
616 if (duvw < distance[u][w]) {
617 distance[w][u] = distance[u][w] = duvw;
618 pred[u][w] = (pred[u][v] ? pred[v][w] :
nullptr);
619 pred[w][u] = (pred[w][v] ? pred[v][u] :
nullptr);
635 distance[u].
init(G, std::numeric_limits<T>::max());
636 pred[u].
init(G,
nullptr);
640 distance[u][v] = distance[v][u] = G.
weight(e);
641 pred[u][v] = pred[v][u] = e;
652 if (duvw < distance[u][w]) {
653 distance[w][u] = distance[u][w] = duvw;
654 pred[u][w] = pred[v][w];
655 pred[w][u] = pred[v][u];
680 std::stringstream out;
682 if (isTerminal[steinerTree.
original(v)]) {
692 GA.
label(v) = out.str();
703 std::ofstream writeStream(filename, std::ofstream::out);
731 std::stringstream out;
742 if (steinerTree.
copy(v)) {
750 GA.
label(v) = out.str();
762 std::ofstream writeStream(filename, std::ofstream::out);
static void apspInit(const EdgeWeightedGraph< T > &G, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred)
Common initialization routine for APSP algorithms.
node target() const
Returns the target node of the edge.
virtual ~MinSteinerTreeModule()
Do nothing on destruction.
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.
void pop()
Removes the topmost element from the queue.
bool newInitialPlacement() const
Returns the current setting of option newInitialPlacement.
double width(node v) const
Returns the width of the bounding box of node v.
Declaration of class GraphAttributes which extends a Graph by additional attributes.
edge theEdge() const
Returns the edge associated with this adjacency entry.
node copy(node v) const
Returns the node in the graph copy corresponding to v.
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.
Dijkstra's single source shortest path algorithm.
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.
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
FMMMOptions::QualityVsSpeed qualityVersusSpeed() const
Returns the current setting of option qualityVersusSpeed.
iterator pushBack(const E &x)
Adds element x at the end of the list.
static bool drawSVG(const GraphAttributes &A, std::ostream &os, const SVGSettings &settings)
const Color & fillColor(node v) const
Returns the fill color of node v.
double unitEdgeLength() const
Returns the current setting of option unitEdgeLength.
void push(const E &element, const P &priority)
Adds a new element to the queue.
static const long edgeLabel
Corresponds to edge attribute label(edge).
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.
void init()
Reinitializes the array. Associates the array with no graph.
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim's 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.
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 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.
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.
const Color & strokeColor(node v) const
Returns the stroke color of node v.
static void sortTerminals(List< node > &terminals)
Sort terminals by index.
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
int degree() const
Returns the degree of the node (indegree + outdegree).
void call(const Graph &G, const EdgeArray< T > &weight, const List< node > &sources, NodeArray< edge > &predecessor, NodeArray< T > &distance, bool directed=false)
Calculates, based on the graph G with corresponding edge costs and source nodes, the shortest paths a...
edge newEdge(node u, node v, T weight)
const EdgeArray< T > & edgeWeights() const
node firstNode() const
Returns the first node in the list of all nodes.
void push(E e)
Puts a new element in the buffer.
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.
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Extends the GraphCopy concept to weighted graphs.
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 directed() const
Returns if the graph is directed.
node succ() const
Returns the successor in the list of all nodes.
static const long nodeStyle
Corresponds to node attributes strokeColor(node), strokeType(node), strokeWidth(node), fillPattern(node), fillColor(node), and fillBgColor(node).
Prioritized queue interface wrapper for heaps.
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 drawSVG(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const char *filename)
Writes an SVG file of the instance graph.
T weight(const edge e) const
double height(node v) const
Returns the height of the bounding box of node v.
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.
virtual void delEdge(edge e) override
Removes edge e and clears the list of edges corresponding to e's original edge.
The fast multipole multilevel layout algorithm.
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.
void createEmpty(const Graph &wG)
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
int numberOfEdges() const
Returns the number of edges in the graph.
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...
const E & topElement() const
Returns the topmost element in the queue.
Implementation of Dijkstra's single source shortest path algorithm.
void quicksort()
Sorts the list using Quicksort.
const string & label(node v) const
Returns the label of node v.
static const long nodeLabel
Corresponds to node attribute label(node).
edge firstEdge() const
Returns the first edge in the list of all edges.
static T pruneDanglingSteinerPathsFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal, const List< node > &start)
Prunes dangling Steiner paths beginning at given nonterminal leaves only.
const_reference front() const
Returns a const reference to the first element.
const EdgeArray< T > & edgeWeights() const
Stores additional attributes of a graph (like layout information).
Doubly linked lists (maintaining the length of the list).
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
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...
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Class for the representation of edges.
Class for the representation of nodes.
Priority queue interface wrapping various heaps.
Declaration of Fast Multipole Multilevel Method (FM^3).
bool useHighLevelOptions() const
Returns the current setting of option useHighLevelOptions.
node opposite(node v) const
Returns the adjacent node different from v.
Definition of a Triple used in contraction-based approximation algorithm for the minimum Steiner tree...
static const long edgeGraphics
Corresponds to edge attribute bends(edge).
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.
const Graph & original() const
Returns a reference to the original graph.
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prunes nonterminal leaves and their paths to terminal or branching nodes.
static void getNonterminals(ArrayBuffer< node > &nonterminals, const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Generates a list (as ArrayBuffer<node>) of all nonterminals.
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.
int index() const
Returns the (unique) node index.
edge succ() const
Returns the successor in the list of all edges.
bool empty() const
Checks whether the queue is empty.
A-Star informed search algorithm.
adjEntry succ() const
Returns the successor in the adjacency list.
int size() const
Returns the number of elements in the list.
virtual T computeSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)=0
Computes the actual Steiner tree.
virtual void call(GraphAttributes &GA) override
Calls the algorithm for graph GA and returns the layout information in GA.
static T removeCyclesFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Remove remaining cycles from a Steiner "almost" tree.
Declaration of simple graph algorithms.
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.
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.
The namespace for all OGDF objects.
Declaration of extended graph algorithms.
float strokeWidth(node v) const
Returns the stroke width of node v.
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 drawSteinerTreeSVG(const EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal, const char *filename)
Writes a SVG that shows only the given Steiner tree.
int numberOfNodes() const
Returns the number of nodes in the graph.
virtual void delNode(node v) override
Removes node v and all its adjacent edges cleaning-up their corresponding lists of original edges...
Declares class GraphIO which provides access to all graph read and write functionality.
Compare elements based on a single comparable attribute.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
static T pruneDanglingSteinerPathFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal, node start)
Prunes the dangling Steiner path beginning at a given nonterminal leaf only.
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
bool isConnected(const Graph &G)
Returns true iff G is connected.
Implementation of the A* informed search algorithm.
bool empty() const
Returns true iff the list is empty.
Class for adjacency list elements.
Shape shape(node v) const
Returns the shape type of node v.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
T weight(const edge e) const
static const long nodeGraphics
Corresponds to node attributes x(node), y(node), width(node), height(node), and shape(node).
static void getTerminals(List< node > &terminals, const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Generates a list (as List<node>) of all terminals.
const_reference back() const
Returns a const reference to the last element.
static const long edgeStyle
Corresponds to edge attributes strokeColor(edge), strokeType(edge), and strokeWidth(edge).
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.