Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
ShortestPathAlgorithms.h
Go to the documentation of this file.
1
32#pragma once
33
34#include <ogdf/basic/Graph.h>
37#include <ogdf/basic/Math.h>
38#include <ogdf/basic/SList.h>
40
41namespace ogdf {
42
44
49template<typename TCost>
50void bfs_SPAP(const Graph& G, NodeArray<NodeArray<TCost>>& distance, TCost edgeCosts) {
51 for (node v : G.nodes) {
52 bfs_SPSS(v, G, distance[v], edgeCosts);
53 }
54}
55
57
62template<typename TCost>
63void bfs_SPSS(node s, const Graph& G, NodeArray<TCost>& distanceArray, TCost edgeCosts) {
64 NodeArray<bool> mark(G, false);
66 bfs.pushBack(s);
67 // mark s and set distance to itself 0
68 mark[s] = true;
69 distanceArray[s] = TCost(0);
70 while (!bfs.empty()) {
71 node w = bfs.popFrontRet();
72 TCost d = distanceArray[w] + edgeCosts;
73 for (adjEntry adj : w->adjEntries) {
74 node v = adj->twinNode();
75 if (!mark[v]) {
76 mark[v] = true;
77 bfs.pushBack(v);
78 distanceArray[v] = d;
79 }
80 }
81 }
82}
83
85
92template<typename TCost>
93double dijkstra_SPAP(const GraphAttributes& GA, NodeArray<NodeArray<TCost>>& shortestPathMatrix) {
94 const Graph& G = GA.constGraph();
95 EdgeArray<TCost> edgeCosts(G);
96 double avgCosts = 0;
97 for (edge e : G.edges) {
98 edgeCosts[e] = GA.doubleWeight(e);
99 avgCosts += edgeCosts[e];
100 }
101 dijkstra_SPAP(G, shortestPathMatrix, edgeCosts);
102 return avgCosts / G.numberOfEdges();
103}
104
106
111template<typename TCost>
112void dijkstra_SPAP(const Graph& G, NodeArray<NodeArray<TCost>>& shortestPathMatrix,
113 const EdgeArray<TCost>& edgeCosts) {
114 for (node v : G.nodes) {
115 dijkstra_SPSS(v, G, shortestPathMatrix[v], edgeCosts);
116 }
117}
118
120
127template<typename TCost>
128void dijkstra_SPSS(node s, const Graph& G, NodeArray<TCost>& shortestPathMatrix,
129 const EdgeArray<TCost>& edgeCosts) {
130 NodeArray<edge> predecessor;
131 Dijkstra<TCost> sssp;
132 sssp.call(G, edgeCosts, s, predecessor, shortestPathMatrix);
133}
134
136
142template<typename TCost>
143void floydWarshall_SPAP(NodeArray<NodeArray<TCost>>& shortestPathMatrix, const Graph& G) {
144 for (node u : G.nodes) {
145 for (node v : G.nodes) {
146 for (node w : G.nodes) {
147 Math::updateMin(shortestPathMatrix[v][w],
148 shortestPathMatrix[u][v] + shortestPathMatrix[u][w]);
149 }
150 }
151 }
152}
153
154}
Includes declaration of graph class.
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Decralation of GraphElement and GraphList classes.
Declaration of singly linked lists and iterators.
Mathematical Helpers.
Class for adjacency list elements.
Definition Graph_d.h:143
Dijkstra's single source shortest path algorithm.
Definition Dijkstra.h:60
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...
Definition Dijkstra.h:253
Class for the representation of edges.
Definition Graph_d.h:364
Stores additional attributes of a graph (like layout information).
const Graph & constGraph() const
Returns a reference to the associated graph.
double doubleWeight(edge e) const
Returns the (real number) weight of edge e.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
Class for the representation of nodes.
Definition Graph_d.h:241
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition Graph_d.h:272
Singly linked lists.
Definition SList.h:191
bool empty() const
Returns true iff the list is empty.
Definition SList.h:238
E popFrontRet()
Removes the first element from the list and returns it.
Definition SList.h:544
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition SList.h:481
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
Implementation of Dijkstra's single source shortest path algorithm.
void bfs_SPAP(const Graph &G, NodeArray< NodeArray< TCost > > &distance, TCost edgeCosts)
Computes all-pairs shortest paths in G using breadth-first serach (BFS).
void floydWarshall_SPAP(NodeArray< NodeArray< TCost > > &shortestPathMatrix, const Graph &G)
Computes all-pairs shortest paths in graph G using Floyd-Warshall's algorithm.
void bfs_SPSS(node s, const Graph &G, NodeArray< TCost > &distanceArray, TCost edgeCosts)
Computes single-source shortest paths from s in G using breadth-first search (BFS).
void dijkstra_SPSS(node s, const Graph &G, NodeArray< TCost > &shortestPathMatrix, const EdgeArray< TCost > &edgeCosts)
Computes single-source shortest paths from node s in G using Disjkstra's algorithm.
double dijkstra_SPAP(const GraphAttributes &GA, NodeArray< NodeArray< TCost > > &shortestPathMatrix)
Computes all-pairs shortest paths in GA using Dijkstra's algorithm.
void updateMin(T &min, const T &newValue)
Stores the minimum of min and newValue in min.
Definition Math.h:102
The namespace for all OGDF objects.