Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

ShortestPathAlgorithms.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Array.h>
36 #include <ogdf/basic/Graph_d.h>
37 #include <ogdf/basic/NodeArray.h>
38 #include <ogdf/basic/SList.h>
39 #include <ogdf/graphalg/Dijkstra.h>
40 
41 namespace ogdf {
42 
44 
49 template<typename TCost>
50 void 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 
62 template<typename TCost>
63 void bfs_SPSS(node s, const Graph& G, NodeArray<TCost>& distanceArray, TCost edgeCosts) {
64  NodeArray<bool> mark(G, false);
65  SListPure<node> bfs;
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 
92 template<typename TCost>
93 double 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 
111 template<typename TCost>
112 void 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 
127 template<typename TCost>
128 void 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 
142 template<typename TCost>
143 void 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 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:66
GraphAttributes.h
Declaration of class GraphAttributes which extends a Graph by additional attributes.
ogdf::bfs_SPAP
void bfs_SPAP(const Graph &G, NodeArray< NodeArray< TCost >> &distance, TCost edgeCosts)
Computes all-pairs shortest paths in G using breadth-first serach (BFS).
Definition: ShortestPathAlgorithms.h:50
ogdf::bfs_SPSS
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).
Definition: ShortestPathAlgorithms.h:63
ogdf::GraphAttributes::constGraph
const Graph & constGraph() const
Returns a reference to the associated graph.
Definition: GraphAttributes.h:217
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:135
ogdf::floydWarshall_SPAP
void floydWarshall_SPAP(NodeArray< NodeArray< TCost >> &shortestPathMatrix, const Graph &G)
Computes all-pairs shortest paths in graph G using Floyd-Warshall's algorithm.
Definition: ShortestPathAlgorithms.h:143
ogdf::SListPure::empty
bool empty() const
Returns true iff the list is empty.
Definition: SList.h:226
ogdf::Dijkstra
Dijkstra's single source shortest path algorithm.
Definition: Dijkstra.h:53
SList.h
Declaration of singly linked lists and iterators.
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:264
ogdf::Dijkstra::call
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:246
ogdf::SListPure
Singly linked lists.
Definition: SList.h:39
ogdf::Math::updateMin
void updateMin(T &min, const T &newValue)
Stores the minimum of min and newValue in min.
Definition: Math.h:98
ogdf::dijkstra_SPAP
double dijkstra_SPAP(const GraphAttributes &GA, NodeArray< NodeArray< TCost >> &shortestPathMatrix)
Computes all-pairs shortest paths in GA using Dijkstra's algorithm.
Definition: ShortestPathAlgorithms.h:93
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:168
NodeArray.h
Declaration and implementation of NodeArray class.
ogdf::SListPure::popFrontRet
E popFrontRet()
Removes the first element from the list and returns it.
Definition: SList.h:532
Array.h
Declaration and implementation of Array class and Array algorithms.
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
Dijkstra.h
Implementation of Dijkstra's single source shortest path algorithm.
ogdf::SListPure::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: SList.h:469
ogdf::GraphAttributes::doubleWeight
double doubleWeight(edge e) const
Returns the (real number) weight of edge e.
Definition: GraphAttributes.h:786
Graph_d.h
Pure declaration header, find template implementation in Graph.h.
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::dijkstra_SPSS
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.
Definition: ShortestPathAlgorithms.h:128
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709