|
Open Graph Drawing Framework |
v. 2023.09 (Elderberry)
|
|
|
Go to the documentation of this file.
60 class 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()) {
410 finalSteinerTree->
newNode(terminals.back());
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)) {
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);
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);
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()) {
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) {
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) {
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);
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
The namespace for all OGDF objects.
Stores additional attributes of a graph (like layout information).
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Declaration and implementation of ArrayBuffer class.
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.
Includes declaration of graph class.
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.
Declaration of basic types for graphics.
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 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.
const Color & fillColor(node v) const
Returns the fill color of node v.
FMMMOptions::QualityVsSpeed qualityVersusSpeed() const
Returns the current setting of option qualityVersusSpeed.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
void pop()
Removes the topmost element from the queue.
static const long edgeStyle
Corresponds to edge attributes strokeColor(edge), strokeType(edge), and strokeWidth(edge).
static const long nodeStyle
Corresponds to node attributes strokeColor(node), strokeType(node), strokeWidth(node),...
@ GorgeousAndEfficient
Best quality.
Priority queue interface wrapping various heaps.
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 bool drawSVG(const GraphAttributes &A, std::ostream &os, const SVGSettings &settings)
The fast multipole multilevel layout algorithm.
void push(const E &element, const P &priority)
Adds a new element to the queue.
int index() const
Returns the (unique) node index.
void setOriginalGraph(const Graph *wG) override
Associates the graph copy with G, but does not create any nodes or edges.
Declaration of extended graph algorithms.
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.
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim's algorithm.
edge newEdge(node u, node v, T weight)
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.
bool isConnected(const Graph &G)
Returns true iff G is connected.
bool directed() const
Returns if the graph is directed.
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 const long edgeLabel
Corresponds to edge attribute label(edge).
Class for adjacency list elements.
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...
Extends the GraphCopy concept to weighted graphs.
double unitEdgeLength() const
Returns the current setting of option unitEdgeLength.
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
bool useHighLevelOptions() const
Returns the current setting of option useHighLevelOptions.
Dijkstra's single source shortest path algorithm.
Shape shape(node v) const
Returns the shape type of node v.
edge theEdge() const
Returns the edge associated with this adjacency entry.
bool empty() const
Checks whether the queue is empty.
Implementation of the A* informed search 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.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
int numberOfEdges() const
Returns the number of edges in the graph.
virtual void call(GraphAttributes &GA) override
Calls the algorithm for graph GA and returns the layout information in GA.
int degree() const
Returns the degree of the node (indegree + outdegree).
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.
int numberOfNodes() const
Returns the number of nodes in the graph.
static void sortTerminals(List< node > &terminals)
Sort terminals by index.
Decralation of GraphElement and GraphList classes.
adjEntry succ() const
Returns the successor in the adjacency list.
Declaration of Fast Multipole Multilevel Method (FM^3).
Prioritized queue interface wrapper for heaps.
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prunes nonterminal leaves and their paths to terminal or branching nodes.
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...
Declares class GraphIO which provides access to all graph read and write functionality.
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)
virtual T computeSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)=0
Computes the actual Steiner tree.
node source() const
Returns the source node of the 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.
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Doubly linked lists (maintaining the length of the list).
RegisteredArray for nodes, edges and adjEntries of a graph.
void delNode(node v) override
Removes node v.
double height(node v) const
Returns the height of the bounding box of node v.
const E & topElement() const
Returns the topmost element in the queue.
static const long nodeLabel
Corresponds to node attribute label(node).
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
static void drawSteinerTreeSVG(const EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal, const char *filename)
Writes a SVG that shows only the given Steiner tree.
edge copy(edge e) const override
Returns the first edge in the list of edges corresponding to edge e.
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.
void delEdge(edge e) override
Removes edge e and clears the list of edges corresponding to e's original edge.
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 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.
A-Star informed search algorithm.
bool newInitialPlacement() const
Returns the current setting of option newInitialPlacement.
static const long edgeGraphics
Corresponds to edge attribute bends(edge).
edge firstEdge() const
Returns the first edge in the list of all edges.
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
void decrease(const E &element, const P &priority)
Decreases the priority of the given element.
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.
Basic declarations, included by all source files.
const EdgeArray< T > & edgeWeights() const
T weight(const edge e) const
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
static void apspInit(const EdgeWeightedGraph< T > &G, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred)
Common initialization routine for APSP algorithms.
node succ() const
Returns the successor in the list of all nodes.
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.
node opposite(node v) const
Returns the adjacent node different from v.
static T removeCyclesFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Remove remaining cycles from a Steiner "almost" tree.
Class for the representation of edges.
Implementation of Dijkstra's single source shortest path algorithm.
Declaration of doubly linked lists and iterators.
edge succ() const
Returns the successor in the list of all edges.
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.
void init(const Graph *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
int size() const
Returns the number of elements in the list.
const Color & strokeColor(node v) const
Returns the stroke color of node v.
static void getTerminals(List< node > &terminals, const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Generates a list (as List<node>) of all terminals.
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.
node target() const
Returns the target node of the edge.
virtual ~MinSteinerTreeModule()
Do nothing on destruction.
std::string to_string(const std::function< std::ostream &(std::ostream &)> &func)
static void drawSVG(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const char *filename)
Writes an SVG file of the instance graph.
Declarations for Comparer objects.
Class for the representation of nodes.
static const long nodeGraphics
Corresponds to node attributes x(node), y(node), width(node), height(node), and shape(node).
Declaration of simple graph algorithms.
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.
Compare elements based on a single comparable attribute.
bool isTree(const Graph &G)
Returns true iff G is a tree, i.e. contains no undirected cycle and is connected.
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 string & label(node v) const
Returns the label of node v.
iterator pushBack(const E &x)
Adds element x at the end of the list.
const Graph & original() const
Returns a reference to the original graph.
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
float strokeWidth(node v) const
Returns the stroke width of node v.
double width(node v) const
Returns the width of the bounding box of node v.
Declaration of Fast Multipole Multilevel Method (FM^3) options.