Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

ShortestPathAlgorithms.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/SList.h>
35 #include <ogdf/basic/Graph_d.h>
37 #include <ogdf/basic/NodeArray.h>
38 #include <ogdf/basic/Array.h>
39 #include <ogdf/graphalg/Dijkstra.h>
40 
41 
42 namespace ogdf {
43 
45 
50 template<typename TCost>
51 void bfs_SPAP(const Graph& G, NodeArray<NodeArray<TCost>>& distance, TCost edgeCosts) {
52  for (node v : G.nodes) {
53  bfs_SPSS(v, G, distance[v], edgeCosts);
54  }
55 }
56 
57 
59 
64 template<typename TCost>
65 void bfs_SPSS(node s, const Graph& G, NodeArray<TCost> & distanceArray, TCost edgeCosts) {
66  NodeArray<bool> mark(G, false);
67  SListPure<node> bfs;
68  bfs.pushBack(s);
69  // mark s and set distance to itself 0
70  mark[s] = true;
71  distanceArray[s] = TCost(0);
72  while (!bfs.empty()) {
73  node w = bfs.popFrontRet();
74  TCost d = distanceArray[w] + edgeCosts;
75  for(adjEntry adj : w->adjEntries) {
76  node v = adj->twinNode();
77  if (!mark[v]) {
78  mark[v] = true;
79  bfs.pushBack(v);
80  distanceArray[v] = d;
81  }
82  }
83  }
84 }
85 
86 
88 
95 template<typename TCost>
96 double dijkstra_SPAP(const GraphAttributes& GA, NodeArray<NodeArray<TCost>>& shortestPathMatrix) {
97  const Graph& G = GA.constGraph();
98  EdgeArray<TCost> edgeCosts(G);
99  double avgCosts = 0;
100  for (edge e : G.edges) {
101  edgeCosts[e] = GA.doubleWeight(e);
102  avgCosts += edgeCosts[e];
103  }
104  dijkstra_SPAP(G, shortestPathMatrix, edgeCosts);
105  return avgCosts / G.numberOfEdges();
106 }
107 
108 
110 
115 template<typename TCost>
117  const Graph& G,
118  NodeArray<NodeArray<TCost>>& shortestPathMatrix,
119  const EdgeArray<TCost>& edgeCosts)
120 {
121  for (node v : G.nodes) {
122  dijkstra_SPSS(v, G, shortestPathMatrix[v], edgeCosts);
123  }
124 }
125 
126 
128 
135 template<typename TCost>
137  node s,
138  const Graph& G,
139  NodeArray<TCost>& shortestPathMatrix,
140  const EdgeArray<TCost>& edgeCosts)
141 {
142  NodeArray<edge> predecessor;
143  Dijkstra<TCost> sssp;
144  sssp.call(G, edgeCosts, s, predecessor, shortestPathMatrix);
145 }
146 
147 
149 
155 template<typename TCost>
156 void floydWarshall_SPAP(NodeArray<NodeArray<TCost>>& shortestPathMatrix, const Graph& G) {
157  for (node u : G.nodes) {
158  for (node v : G.nodes) {
159  for (node w : G.nodes) {
160  Math::updateMin(shortestPathMatrix[v][w], shortestPathMatrix[u][v] + shortestPathMatrix[u][w]);
161  }
162  }
163  }
164 }
165 
166 }
Declaration of singly linked lists and iterators.
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Dijkstra&#39;s single source shortest path algorithm.
Definition: Dijkstra.h:52
bool empty() const
Returns true iff the list is empty.
Definition: SList.h:222
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
Singly linked lists.
Definition: SList.h:38
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...
Definition: Dijkstra.h:61
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&#39;s algorithm.
Declaration and implementation of Array class and Array algorithms.
void updateMin(T &min, const T &newValue)
Stores the minimum of min and newValue in min.
Definition: Math.h:101
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:568
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:601
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:438
Implementation of Dijkstra&#39;s single source shortest path algorithm.
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: SList.h:451
Stores additional attributes of a graph (like layout information).
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:108
Class for the representation of edges.
Definition: Graph_d.h:292
Class for the representation of nodes.
Definition: Graph_d.h:169
const Graph & constGraph() const
Returns a reference to the associated graph.
Declaration and implementation of NodeArray class.
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 floydWarshall_SPAP(NodeArray< NodeArray< TCost >> &shortestPathMatrix, const Graph &G)
Computes all-pairs shortest paths in graph G using Floyd-Warshall&#39;s algorithm.
double dijkstra_SPAP(const GraphAttributes &GA, NodeArray< NodeArray< TCost >> &shortestPathMatrix)
Computes all-pairs shortest paths in GA using Dijkstra&#39;s algorithm.
Pure declaration header, find template implementation in Graph.h.
E popFrontRet()
Removes the first element from the list and returns it.
Definition: SList.h:508
double doubleWeight(edge e) const
Returns the (real number) weight of edge e.
The namespace for all OGDF objects.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
Class for adjacency list elements.
Definition: Graph_d.h:79
void bfs_SPAP(const Graph &G, NodeArray< NodeArray< TCost >> &distance, TCost edgeCosts)
Computes all-pairs shortest paths in G using breadth-first serach (BFS).
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200