PGROUTING  3.2
pgrouting::graph::PgrDirectedChPPGraph Class Reference

#include "pgr_chinesePostman.hpp"

Collaboration diagram for pgrouting::graph::PgrDirectedChPPGraph:

Public Member Functions

 PgrDirectedChPPGraph (const pgr_edge_t *dataEdges, const size_t totalEdges)
 
 ~PgrDirectedChPPGraph ()
 
double DirectedChPP () const
 
std::vector< General_path_element_tGetPathEdges () const
 

Private Member Functions

void BuildResultGraph ()
 
void BuildResultPath ()
 
bool EulerCircuitDFS (int64_t p)
 
void setPathEdges (graph::PgrCostFlowGraph &flowGraph)
 

Private Attributes

std::vector< pgr_costFlow_tedges
 
std::map< std::pair< int64_t, int64_t >, size_t > edgeToId
 
std::map< std::pair< int64_t, int64_t >, const pgr_edge_t * > edgeToIdx
 (source, target) -> idx to originalEdges; Only the one with the lower cost is kept More...
 
Identifiers< size_t > edgeVisited
 
double m_cost
 
std::vector< pgr_edge_toriginalEdges
 
std::stack< int64_t > pathStack
 
std::vector< pgr_edge_tresultEdges
 
std::vector< std::pair< int64_t, std::vector< size_t > > > resultGraph
 vector of vertex -> vector of edges More...
 
std::vector< General_path_element_tresultPath
 
std::set< int64_t > sources
 
int64_t startPoint
 
int64_t superSource
 
int64_t superTarget
 
std::set< int64_t > targets
 
double totalCost
 
int64_t totalDeg
 
Identifiers< int64_t > vertexVisited
 
Identifiers< int64_t > vertices
 
std::map< int64_t, size_t > VToVecid
 

Detailed Description

Definition at line 49 of file pgr_chinesePostman.hpp.

Constructor & Destructor Documentation

◆ PgrDirectedChPPGraph()

pgrouting::graph::PgrDirectedChPPGraph::PgrDirectedChPPGraph ( const pgr_edge_t dataEdges,
const size_t  totalEdges 
)

Definition at line 108 of file pgr_chinesePostman.hpp.

110  :
111  totalDeg(0), totalCost(0), vertices(),
114  pathStack(), resultPath(),
115  edges(), sources(), targets() {
116  pgassert(totalEdges > 0);
117  pgassert(pathStack.empty());
118 
119  pgassert(originalEdges.empty());
120  startPoint = dataEdges[0].source;
121  for (size_t i = 0; i < totalEdges; ++i) {
122  if (dataEdges[i].cost > 0) {
123  auto edge(dataEdges[i]);
124  edge.reverse_cost = -1.0;
125  totalCost += edge.cost;
126  originalEdges.push_back(edge);
127  vertices += dataEdges[i].source;
128  vertices += dataEdges[i].target;
129  }
130  if (dataEdges[i].reverse_cost > 0) {
131  auto edge(dataEdges[i]);
132  std::swap(edge.source, edge.target);
133  std::swap(edge.cost, edge.reverse_cost);
134  edge.reverse_cost = -1.0;
135  totalCost += edge.cost;
136  originalEdges.push_back(edge);
137  vertices += dataEdges[i].source;
138  vertices += dataEdges[i].target;
139  pgassert(dataEdges[i].source == edge.target);
140  pgassert(dataEdges[i].target == edge.source);
141  }
142  }
143 
144  // calcu deg & build part of edges
145  std::map<int64_t, int> deg;
146  size_t i(0);
147  for (const auto &e : originalEdges) {
148  pgassert(e.cost > 0);
149  /* has out going edge */
150  deg[e.source]++;
151  /* has out incoming edge */
152  deg[e.target]--;
153 
154  auto current_edge(std::make_pair(e.source, e.target));
155  if (edgeToIdx.find(current_edge) == edgeToIdx.end()) {
156  edgeToIdx[current_edge] = &e;
157  } else {
158  if (edgeToIdx[current_edge]->cost > e.cost) {
159  edgeToIdx[current_edge] = &e;
160  }
161  }
162 
164  edge.edge_id = e.id;
165  edge.reverse_capacity = -1;
166  edge.reverse_cost = -1.0;
167  edge.source = e.source;
168  edge.target = e.target;
169  edge.capacity = (std::numeric_limits<int32_t>::max)();
170  edge.cost = e.cost;
171  edges.push_back(edge);
172  ++i;
173  }
174 
175  superSource = deg.rbegin()->first + 1;
176  superTarget = deg.rbegin()->first + 2;
177 
178  sources.insert(superSource);
179  targets.insert(superTarget);
180 
181  // build full edges
182  std::map<int64_t, int>::iterator iter;
183  totalDeg = 0;
184  for (iter = deg.begin(); iter != deg.end(); ++iter) {
185  int64_t p = iter->first;
186  int d = iter->second;
187  if (d == 0)
188  continue;
189  if (d > 0)
190  totalDeg += d;
192  edge.reverse_capacity = -1;
193  edge.reverse_cost = -1.0;
194  edge.cost = 0.0;
195  if (d > 0)
196  edge.capacity = d;
197  else
198  edge.capacity = -d;
199  edge.edge_id = 0;
200  if (d > 0)
202  if (d < 0)
204  edges.push_back(edge);
205  }
206 
207  PgrCostFlowGraph flowGraph(edges, sources, targets);
208  pgassert(pathStack.empty());
209  try {
211  auto minAddedCost = digraph1.MinCostMaxFlow();
212  auto maxFlow = digraph1.GetMaxFlow();
213  m_cost = (maxFlow == totalDeg)? minAddedCost + totalCost : -1.0;
214  } catch (...) {
215  m_cost = -1;
216  }
217  setPathEdges(flowGraph);
218 }

References edge::cost, edges, edgeToIdx, pgrouting::graph::PgrCostFlowGraph::GetMaxFlow(), edge::id, m_cost, pgrouting::graph::PgrCostFlowGraph::MinCostMaxFlow(), originalEdges, pathStack, pgassert, edge::reverse_cost, setPathEdges(), pgr_edge_t::source, edge::source, sources, startPoint, superSource, superTarget, pgr_edge_t::target, edge::target, targets, totalCost, totalDeg, and vertices.

◆ ~PgrDirectedChPPGraph()

pgrouting::graph::PgrDirectedChPPGraph::~PgrDirectedChPPGraph ( )

Definition at line 105 of file pgr_chinesePostman.hpp.

105  {
106  edgeToIdx.clear();
107 }

References edgeToIdx.

Member Function Documentation

◆ BuildResultGraph()

void pgrouting::graph::PgrDirectedChPPGraph::BuildResultGraph ( )
private

Definition at line 326 of file pgr_chinesePostman.hpp.

326  {
327  pgassert(resultGraph.empty());
328  pgassert(VToVecid.empty());
329  resultGraph.clear();
330  VToVecid.clear();
331  size_t e_id(0);
332  for (const auto &edge_t : resultEdges) {
333  if (VToVecid.find(edge_t.source) == VToVecid.end()) {
334  VToVecid[edge_t.source] = resultGraph.size();
335  resultGraph.resize(resultGraph.size() + 1);
336  }
337  size_t vid = VToVecid[edge_t.source];
338  resultGraph[vid].second.push_back(e_id);
339  resultGraph[vid].first = edge_t.source;
340  ++e_id;
341  }
342 }

References pgassert, resultEdges, resultGraph, edge_t::source, and VToVecid.

Referenced by setPathEdges().

◆ BuildResultPath()

void pgrouting::graph::PgrDirectedChPPGraph::BuildResultPath ( )
private

Definition at line 268 of file pgr_chinesePostman.hpp.

268  {
269  if (pathStack.empty()) return;
270  pgassert(resultPath.empty());
271 
272  int64_t preNode = pathStack.top();
273  pathStack.pop();
274 
275  General_path_element_t newElement;
276  while (!pathStack.empty()) {
277  int64_t nowNode = pathStack.top();
278  pathStack.pop();
279 
280  auto edge_t = *edgeToIdx[std::make_pair(preNode, nowNode)];
281  newElement.node = edge_t.source;
282  newElement.edge = edge_t.id;
283  newElement.cost = edge_t.cost;
284  if (resultPath.empty()) {
285  /* adding the first row because is a cycle */
286  newElement.seq = 1;
287  newElement.agg_cost = 0.0;
288  } else {
289  newElement.seq = resultPath.back().seq + 1;
290  newElement.agg_cost = resultPath.back().agg_cost + resultPath.back().cost;
291  }
292  resultPath.push_back(newElement);
293  preNode = nowNode;
294  }
295  newElement.node = preNode;
296  newElement.edge = -1;
297  newElement.cost = 0;
298  if (resultPath.empty()) {
299  newElement.seq = 1;
300  newElement.agg_cost = 0.0;
301  } else {
302  newElement.seq = resultPath.back().seq + 1;
303  newElement.agg_cost =
304  resultPath.back().agg_cost + resultPath.back().cost;
305  }
306  resultPath.push_back(newElement);
307 }

References General_path_element_t::agg_cost, edge_t::cost, General_path_element_t::cost, General_path_element_t::edge, edgeToIdx, edge_t::id, General_path_element_t::node, pathStack, pgassert, resultPath, General_path_element_t::seq, and edge_t::source.

Referenced by setPathEdges().

◆ DirectedChPP()

double pgrouting::graph::PgrDirectedChPPGraph::DirectedChPP ( ) const
inline

Definition at line 55 of file pgr_chinesePostman.hpp.

55  {
56  return m_cost;
57  }

References m_cost.

Referenced by do_pgr_directedChPP().

◆ EulerCircuitDFS()

bool pgrouting::graph::PgrDirectedChPPGraph::EulerCircuitDFS ( int64_t  p)
private

Definition at line 313 of file pgr_chinesePostman.hpp.

313  {
314  for (const auto e : resultGraph[VToVecid[vertex]].second) {
315  if (!edgeVisited.has(e)) {
316  edgeVisited += e;
317  EulerCircuitDFS(resultEdges[e].target);
318  }
319  }
320  pathStack.push(vertex);
321  vertexVisited += vertex;
322  return true;
323 }

References edgeVisited, Identifiers< T >::has(), pathStack, resultEdges, resultGraph, vertexVisited, and VToVecid.

Referenced by setPathEdges().

◆ GetPathEdges()

std::vector<General_path_element_t> pgrouting::graph::PgrDirectedChPPGraph::GetPathEdges ( ) const
inline

Definition at line 59 of file pgr_chinesePostman.hpp.

59  {
60  return resultPath;
61  }

References resultPath.

Referenced by do_pgr_directedChPP().

◆ setPathEdges()

void pgrouting::graph::PgrDirectedChPPGraph::setPathEdges ( graph::PgrCostFlowGraph flowGraph)
private

Definition at line 222 of file pgr_chinesePostman.hpp.

222  {
223  pgassert(pathStack.empty());
224  resultPath.clear();
225  if (m_cost == -1) return;
226  // catch new edges
227  try {
228  flowGraph.MinCostMaxFlow();
229  flowGraph.GetMaxFlow();
230  std::vector<pgr_flow_t> addedEdges = flowGraph.GetFlowEdges();
232  for (auto &flow_t : addedEdges) {
233  if (flow_t.source != superSource && flow_t.source != superTarget
234  && flow_t.target != superSource && flow_t.target != superTarget) {
235  auto current_edge(std::make_pair(flow_t.source, flow_t.target));
236  pgr_edge_t newEdge = *edgeToIdx[current_edge];
237  /* adding edges that need to be traversed twice */
238  while (flow_t.flow--) resultEdges.push_back(newEdge);
239  }
240  }
241  } catch (...) {
242  resultPath.clear();
243  return;
244  }
245 
246  pgassert(pathStack.empty());
248 
249  pgassert(pathStack.empty());
251 
252 
254 
255  if (!(vertices - vertexVisited).empty()) {
256  resultPath.clear();
257  return;
258  }
259  pgassert(!pathStack.empty());
260 
261  BuildResultPath();
262 
263 
264  return;
265 }

References BuildResultGraph(), BuildResultPath(), edgeToIdx, edgeVisited, Identifiers< T >::empty(), EulerCircuitDFS(), pgrouting::graph::PgrCostFlowGraph::GetFlowEdges(), pgrouting::graph::PgrCostFlowGraph::GetMaxFlow(), m_cost, pgrouting::graph::PgrCostFlowGraph::MinCostMaxFlow(), originalEdges, pathStack, pgassert, resultEdges, resultPath, startPoint, superSource, superTarget, vertexVisited, and vertices.

Referenced by PgrDirectedChPPGraph().

Member Data Documentation

◆ edges

std::vector<pgr_costFlow_t> pgrouting::graph::PgrDirectedChPPGraph::edges
private

Definition at line 100 of file pgr_chinesePostman.hpp.

Referenced by PgrDirectedChPPGraph().

◆ edgeToId

std::map<std::pair<int64_t, int64_t>, size_t> pgrouting::graph::PgrDirectedChPPGraph::edgeToId
private

Definition at line 85 of file pgr_chinesePostman.hpp.

◆ edgeToIdx

std::map<std::pair<int64_t, int64_t>, const pgr_edge_t*> pgrouting::graph::PgrDirectedChPPGraph::edgeToIdx
private

(source, target) -> idx to originalEdges; Only the one with the lower cost is kept

Definition at line 83 of file pgr_chinesePostman.hpp.

Referenced by BuildResultPath(), PgrDirectedChPPGraph(), setPathEdges(), and ~PgrDirectedChPPGraph().

◆ edgeVisited

Identifiers<size_t> pgrouting::graph::PgrDirectedChPPGraph::edgeVisited
private

Definition at line 93 of file pgr_chinesePostman.hpp.

Referenced by EulerCircuitDFS(), and setPathEdges().

◆ m_cost

double pgrouting::graph::PgrDirectedChPPGraph::m_cost
private

Definition at line 77 of file pgr_chinesePostman.hpp.

Referenced by DirectedChPP(), PgrDirectedChPPGraph(), and setPathEdges().

◆ originalEdges

std::vector<pgr_edge_t> pgrouting::graph::PgrDirectedChPPGraph::originalEdges
private

Definition at line 87 of file pgr_chinesePostman.hpp.

Referenced by PgrDirectedChPPGraph(), and setPathEdges().

◆ pathStack

std::stack<int64_t> pgrouting::graph::PgrDirectedChPPGraph::pathStack
private

◆ resultEdges

std::vector<pgr_edge_t> pgrouting::graph::PgrDirectedChPPGraph::resultEdges
private

Definition at line 88 of file pgr_chinesePostman.hpp.

Referenced by BuildResultGraph(), EulerCircuitDFS(), and setPathEdges().

◆ resultGraph

std::vector<std::pair<int64_t, std::vector<size_t> > > pgrouting::graph::PgrDirectedChPPGraph::resultGraph
private

vector of vertex -> vector of edges

Definition at line 91 of file pgr_chinesePostman.hpp.

Referenced by BuildResultGraph(), and EulerCircuitDFS().

◆ resultPath

std::vector<General_path_element_t> pgrouting::graph::PgrDirectedChPPGraph::resultPath
private

Definition at line 97 of file pgr_chinesePostman.hpp.

Referenced by BuildResultPath(), GetPathEdges(), and setPathEdges().

◆ sources

std::set<int64_t> pgrouting::graph::PgrDirectedChPPGraph::sources
private

Definition at line 101 of file pgr_chinesePostman.hpp.

Referenced by PgrDirectedChPPGraph().

◆ startPoint

int64_t pgrouting::graph::PgrDirectedChPPGraph::startPoint
private

Definition at line 76 of file pgr_chinesePostman.hpp.

Referenced by PgrDirectedChPPGraph(), and setPathEdges().

◆ superSource

int64_t pgrouting::graph::PgrDirectedChPPGraph::superSource
private

Definition at line 75 of file pgr_chinesePostman.hpp.

Referenced by PgrDirectedChPPGraph(), and setPathEdges().

◆ superTarget

int64_t pgrouting::graph::PgrDirectedChPPGraph::superTarget
private

Definition at line 75 of file pgr_chinesePostman.hpp.

Referenced by PgrDirectedChPPGraph(), and setPathEdges().

◆ targets

std::set<int64_t> pgrouting::graph::PgrDirectedChPPGraph::targets
private

Definition at line 102 of file pgr_chinesePostman.hpp.

Referenced by PgrDirectedChPPGraph().

◆ totalCost

double pgrouting::graph::PgrDirectedChPPGraph::totalCost
private

Definition at line 74 of file pgr_chinesePostman.hpp.

Referenced by PgrDirectedChPPGraph().

◆ totalDeg

int64_t pgrouting::graph::PgrDirectedChPPGraph::totalDeg
private

Definition at line 73 of file pgr_chinesePostman.hpp.

Referenced by PgrDirectedChPPGraph().

◆ vertexVisited

Identifiers<int64_t> pgrouting::graph::PgrDirectedChPPGraph::vertexVisited
private

Definition at line 94 of file pgr_chinesePostman.hpp.

Referenced by EulerCircuitDFS(), and setPathEdges().

◆ vertices

Identifiers<int64_t> pgrouting::graph::PgrDirectedChPPGraph::vertices
private

Definition at line 78 of file pgr_chinesePostman.hpp.

Referenced by PgrDirectedChPPGraph(), and setPathEdges().

◆ VToVecid

std::map<int64_t, size_t> pgrouting::graph::PgrDirectedChPPGraph::VToVecid
private

Definition at line 92 of file pgr_chinesePostman.hpp.

Referenced by BuildResultGraph(), and EulerCircuitDFS().


The documentation for this class was generated from the following file:
pgrouting::graph::PgrDirectedChPPGraph::vertexVisited
Identifiers< int64_t > vertexVisited
Definition: pgr_chinesePostman.hpp:94
edge::reverse_cost
float8 reverse_cost
Definition: trsp.h:46
pgrouting::graph::PgrCostFlowGraph
Definition: pgr_minCostMaxFlow.hpp:51
pgrouting::graph::PgrDirectedChPPGraph::sources
std::set< int64_t > sources
Definition: pgr_chinesePostman.hpp:101
General_path_element_t::edge
int64_t edge
Definition: general_path_element_t.h:42
edge::cost
float8 cost
Definition: trsp.h:45
pgrouting::graph::PgrDirectedChPPGraph::BuildResultGraph
void BuildResultGraph()
Definition: pgr_chinesePostman.hpp:326
pgrouting::graph::PgrDirectedChPPGraph::m_cost
double m_cost
Definition: pgr_chinesePostman.hpp:77
edge_t::source
int64_t source
Definition: trsp_types.h:39
edge::target
int64 target
Definition: trsp.h:44
pgr_edge_t
Definition: pgr_edge_t.h:37
General_path_element_t::seq
int seq
Definition: general_path_element_t.h:38
edge_t
Definition: trsp_types.h:37
pgrouting::graph::PgrDirectedChPPGraph::originalEdges
std::vector< pgr_edge_t > originalEdges
Definition: pgr_chinesePostman.hpp:87
pgr_edge_t::source
int64_t source
Definition: pgr_edge_t.h:39
pgrouting::graph::PgrDirectedChPPGraph::vertices
Identifiers< int64_t > vertices
Definition: pgr_chinesePostman.hpp:78
pgrouting::graph::PgrDirectedChPPGraph::edgeVisited
Identifiers< size_t > edgeVisited
Definition: pgr_chinesePostman.hpp:93
pgrouting::graph::PgrDirectedChPPGraph::resultPath
std::vector< General_path_element_t > resultPath
Definition: pgr_chinesePostman.hpp:97
pgr_edge_t::target
int64_t target
Definition: pgr_edge_t.h:40
pgrouting::graph::PgrDirectedChPPGraph::edgeToIdx
std::map< std::pair< int64_t, int64_t >, const pgr_edge_t * > edgeToIdx
(source, target) -> idx to originalEdges; Only the one with the lower cost is kept
Definition: pgr_chinesePostman.hpp:83
edge
Definition: trsp.h:41
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::graph::PgrDirectedChPPGraph::edges
std::vector< pgr_costFlow_t > edges
Definition: pgr_chinesePostman.hpp:100
pgrouting::graph::PgrDirectedChPPGraph::BuildResultPath
void BuildResultPath()
Definition: pgr_chinesePostman.hpp:268
edge::source
int64 source
Definition: trsp.h:43
pgrouting::graph::PgrDirectedChPPGraph::pathStack
std::stack< int64_t > pathStack
Definition: pgr_chinesePostman.hpp:96
Identifiers::empty
bool empty() const
Definition: identifiers.hpp:79
pgrouting::graph::PgrDirectedChPPGraph::resultEdges
std::vector< pgr_edge_t > resultEdges
Definition: pgr_chinesePostman.hpp:88
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
pgrouting::graph::PgrDirectedChPPGraph::EulerCircuitDFS
bool EulerCircuitDFS(int64_t p)
Definition: pgr_chinesePostman.hpp:313
edge_t::id
int64_t id
Definition: trsp_types.h:38
pgrouting::graph::PgrDirectedChPPGraph::resultGraph
std::vector< std::pair< int64_t, std::vector< size_t > > > resultGraph
vector of vertex -> vector of edges
Definition: pgr_chinesePostman.hpp:91
General_path_element_t
Definition: general_path_element_t.h:37
pgrouting::graph::PgrDirectedChPPGraph::targets
std::set< int64_t > targets
Definition: pgr_chinesePostman.hpp:102
pgrouting::graph::PgrDirectedChPPGraph::startPoint
int64_t startPoint
Definition: pgr_chinesePostman.hpp:76
pgrouting::graph::PgrDirectedChPPGraph::totalDeg
int64_t totalDeg
Definition: pgr_chinesePostman.hpp:73
pgrouting::graph::PgrDirectedChPPGraph::VToVecid
std::map< int64_t, size_t > VToVecid
Definition: pgr_chinesePostman.hpp:92
pgrouting::graph::PgrDirectedChPPGraph::superSource
int64_t superSource
Definition: pgr_chinesePostman.hpp:75
Identifiers::has
bool has(const T other) const
true ids() has element
Definition: identifiers.hpp:98
pgr_costFlow_t
Definition: pgr_costFlow_t.h:38
edge::id
int64 id
Definition: trsp.h:42
pgrouting::graph::PgrDirectedChPPGraph::totalCost
double totalCost
Definition: pgr_chinesePostman.hpp:74
pgrouting::graph::PgrDirectedChPPGraph::setPathEdges
void setPathEdges(graph::PgrCostFlowGraph &flowGraph)
Definition: pgr_chinesePostman.hpp:222
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
pgrouting::graph::PgrDirectedChPPGraph::superTarget
int64_t superTarget
Definition: pgr_chinesePostman.hpp:75
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44
edge_t::cost
double cost
Definition: trsp_types.h:41