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/Graph.h>
36 #include <ogdf/basic/GraphList.h>
37 #include <ogdf/basic/Math.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: multilevelmixer.cpp:39
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:72
GraphAttributes.h
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Graph.h
Includes declaration of graph class.
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:223
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
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:238
ogdf::Dijkstra
Dijkstra's single source shortest path algorithm.
Definition: Dijkstra.h:60
SList.h
Declaration of singly linked lists and iterators.
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:271
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:253
ogdf::SListPure
Singly linked lists.
Definition: SList.h:52
ogdf::Math::updateMin
void updateMin(T &min, const T &newValue)
Stores the minimum of min and newValue in min.
Definition: Math.h:102
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:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
Math.h
Mathematical Helpers.
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:175
ogdf::SListPure::popFrontRet
E popFrontRet()
Removes the first element from the list and returns it.
Definition: SList.h:544
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
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:481
ogdf::GraphAttributes::doubleWeight
double doubleWeight(edge e) const
Returns the (real number) weight of edge e.
Definition: GraphAttributes.h:792
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
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:716