1 #ifndef GRAPH_APPROXIMATE_SHORTEST_PATHS_H 2 #define GRAPH_APPROXIMATE_SHORTEST_PATHS_H 5 #include "ShortestPaths.h" 16 template <
class Node,
class Edge>
23 void InitializeSource(
int s);
24 void InitializeSources(
const vector<int>& s);
26 template <
typename WeightFunc,
typename Iterator>
27 void FindPath(
int t,WeightFunc w,Iterator it);
28 template <
typename WeightFunc,
typename Iterator>
29 int FindAPath(
const set<int>& t,WeightFunc w,Iterator it);
30 template <
typename WeightFunc,
typename Iterator>
31 inline void FindAllPaths(WeightFunc w,Iterator it) { FindPath(-1,w,it); }
35 template <
typename WeightFunc,
typename InIterator,
typename OutIterator>
36 void IncreaseUpdate(
int u,
int v,WeightFunc w,InIterator in,OutIterator out);
37 template <
typename WeightFunc,
typename InIterator,
typename OutIterator>
38 void DecreaseUpdate(
int u,
int v,WeightFunc w,InIterator in,OutIterator out);
39 template <
typename WeightFunc,
typename InIterator,
typename OutIterator>
40 void DeleteUpdate(
int u,
int v,WeightFunc w,InIterator in,OutIterator out);
42 template <
typename WeightFunc,
typename Iterator>
43 bool HasShortestPaths(
int s,WeightFunc w,Iterator it,Real epsilon=-1);
47 template <
typename WeightFunc>
48 inline void FindPath_Directed(
int t,WeightFunc w) {
51 template <
typename WeightFunc>
52 inline int FindAPath_Directed(
const set<int>& t,WeightFunc w) {
55 template <
typename WeightFunc>
56 inline void FindAllPaths_Directed(WeightFunc w) {
60 template <
typename WeightFunc>
61 inline void IncreaseUpdate_Directed(
int u,
int v,WeightFunc w) {
63 IncreaseUpdate(u,v,w,in,out);
65 template <
typename WeightFunc>
66 inline void DecreaseUpdate_Directed(
int u,
int v,WeightFunc w) {
68 DecreaseUpdate(u,v,w,in,out);
70 template <
typename WeightFunc>
71 inline void DeleteUpdate_Directed(
int u,
int v,WeightFunc w) {
73 DeleteUpdate(u,v,w,in,out);
75 template <
typename WeightFunc>
76 inline bool HasShortestPaths_Directed(
int s,WeightFunc w,Real epsilon=-1) {
78 return HasShortestPaths(s,w,it,epsilon);
81 template <
typename WeightFunc>
82 inline void FindPath_Undirected(
int t,WeightFunc w) {
86 template <
typename WeightFunc>
87 inline int FindAPath_Undirected(
const set<int>& t,WeightFunc w) {
90 template <
typename WeightFunc>
91 inline void FindAllPaths_Undirected(WeightFunc w) {
94 template <
typename WeightFunc>
95 inline void IncreaseUpdate_Undirected(
int u,
int v,WeightFunc w) {
97 IncreaseUpdate(u,v,w,it,it);
98 IncreaseUpdate(v,u,w,it,it);
100 template <
typename WeightFunc>
101 inline void DecreaseUpdate_Undirected(
int u,
int v,WeightFunc w) {
103 DecreaseUpdate(u,v,w,it,it);
104 DecreaseUpdate(v,u,w,it,it);
106 template <
typename WeightFunc>
107 inline void DeleteUpdate_Undirected(
int u,
int v,WeightFunc w) {
109 DeleteUpdate(u,v,w,it,it);
110 DeleteUpdate(v,u,w,it,it);
112 template <
typename WeightFunc>
113 inline bool HasShortestPaths_Undirected(
int s,WeightFunc w,Real epsilon=-1) {
115 return HasShortestPaths(s,w,it,epsilon);
122 virtual void SetDistance(
int n,Real dn,
int pn) { d[n]=dn; p[n]=pn; }
128 std::vector<Weight> d;
134 template <
class Node,
class Edge>
137 :g(_g),epsilon(_epsilon)
140 template <
class Node,
class Edge>
144 int nn = g.NumNodes();
147 for(
int i=0;i<nn;i++) {
154 template <
class Node,
class Edge>
158 int nn = g.NumNodes();
161 for(
int i=0;i<nn;i++) {
165 for(
size_t i=0;i<s.size();i++)
169 template <
class Node,
class Edge>
170 template <
typename WeightFunc,
typename Iterator>
175 for(
int i=0;i<nn;i++) H.push(i,-d[i]);
176 Real fudgeFactor = (1.0+epsilon);
180 int u = H.top(); H.pop();
183 for(g.Begin(u,it);!it.end();it++) {
186 Weight dvu = d[u] + w(*it,it.source(),it.target());
187 if(d[v] > dvu*fudgeFactor) {
195 template <
class Node,
class Edge>
196 template <
typename WeightFunc,
typename Iterator>
201 for(
int i=0;i<nn;i++) H.push(i,-d[i]);
203 Real fudgeFactor = (1.0+epsilon);
207 int u = H.top(); H.pop();
208 if(targetSet.count(u)!=0)
return u;
210 for(g.Begin(u,it);!it.end();it++) {
213 Weight dvu = d[u] + w(*it,it.source(),it.target());
214 if(d[v] > dvu*fudgeFactor) {
224 template <
class Node,
class Edge>
225 template <
typename WeightFunc,
typename InIterator,
typename OutIterator>
227 InIterator in,OutIterator out)
230 if(p[v] != u)
return;
233 for(g.Begin(v,in);!in.end();in++) {
235 if(d[v] == d[t]+w(*in,in.source(),in.target())) {
243 for(
size_t i=0;i<Q.size();i++) {
246 for(g.Begin(n,out);!out.end();out++) {
250 for(g.Begin(t,in);!in.end();in++) {
252 if(d[t]==d[s]+w(*in,in.source(),in.target())) {
257 if(p[t]==n) Q.push_back(t);
262 Real fudgeFactor = (1.0+epsilon);
264 for(
size_t i=0;i<Q.size();i++) {
266 for(g.Begin(n,in);!in.end();in++) {
268 double W=w(*in,in.source(),in.target());
269 if(d[n]>(d[s]+W)*fudgeFactor) {
276 int n=H.top(); H.pop();
277 for(g.Begin(n,out);!out.end();out++) {
279 double W=w(*out,out.source(),out.target());
280 if(d[t]>(d[n]+W)*fudgeFactor) {
288 template <
class Node,
class Edge>
289 template <
typename WeightFunc,
typename InIterator,
typename OutIterator>
291 InIterator in,OutIterator out)
294 Real fudgeFactor = (1.0+epsilon);
295 for(g.Begin(u,out);!out.end();out++)
296 if(out.target()==v) {
297 double W=w(*out,out.source(),out.target());
305 FatalError(
"ApproximateShortestPathProblem::DecreaseUpdate(): Warning, decreasing an edge that doesn't exist in the graph!");
310 int n=H.top(); H.pop();
311 for(g.Begin(n,out);!out.end();out++) {
313 double W=w(*out,out.source(),out.target());
314 if(d[t]>(d[n]+W)*fudgeFactor) {
322 template <
class Node,
class Edge>
323 template <
typename WeightFunc,
typename InIterator,
typename OutIterator>
325 InIterator in,OutIterator out)
327 Real fudgeFactor = 1.0+epsilon;
331 for(g.Begin(v,in);!in.end();++in) {
333 if(p[t] == v)
continue;
335 double W=w(*in,in.source(),in.target());
336 if(d[v] > (d[t]+W)*fudgeFactor) {
342 DecreaseUpdate(p[v],v,w,in,out);
344 IncreaseUpdate(p[v],v,w,in,out);
347 for(g.Begin(v,out);!out.end();++out) {
349 IncreaseUpdate(v,t,w,in,out);
355 template <
class Node,
class Edge>
356 template <
typename WeightFunc,
typename Iterator>
359 if(testEpsilon < 0) fudgeFactor = 1.0+epsilon;
360 else fudgeFactor = 1.0+testEpsilon;
363 for(
int i=0;i<nn;i++) H.push(i,-d[i]);
365 int n=H.top(); H.pop();
368 LOG4CXX_INFO(KrisLibrary::logger(),
"The start doesn't have distance 0\n");
372 for(g.Begin(n,it);!it.end();++it) {
374 double W=w(*it,it.source(),it.target());
376 if(fudgeFactor == 1.0) {
377 if(fabs(d[n]-d[t]-W) > 1e-10) {
378 LOG4CXX_INFO(KrisLibrary::logger(),
"Inconsistency in node "<< n<<
"'s weight through parent "<< t<<
", "<< d[n]<<
" vs "<<d[t]+W<<
"="<<d[t]<<
"+"<<W);
383 if(d[n]-(d[t]+W)*fudgeFactor > 1e-10) {
384 LOG4CXX_INFO(KrisLibrary::logger(),
"There exists a shorter path ("<<t<<
","<<n<<
") not ("<<p[n]<<
","<<n);
385 LOG4CXX_INFO(KrisLibrary::logger(),
"Weight 1 is "<< d[t]+W<<
" compared to "<< d[n]);
386 LOG4CXX_INFO(KrisLibrary::logger(),
"Fudge factor "<< fudgeFactor<<
", comparison value "<< d[n]-(d[t]-W)*fudgeFactor);
Implements a heap that stores objects of type type, with priority keys of type ptype.
Definition: Heap.h:19
int IsInf(double x)
Returns +1 if x is +inf, -1 if x is -inf, and 0 otherwise.
Definition: infnan.cpp:92
A heap of fixed maximum size N. Each element is indexed by an integer 0...N-1. The priority key of ea...
Definition: FixedSizeHeap.h:13
virtual void SetDistance(int n, Real dn, int pn)
Definition: ApproximateShortestPaths.h:122
The logging system used in KrisLibrary.
Namespace for all classes and functions in the Graph subdirectory.
Definition: ApproximateShortestPaths.h:7
Approximate single-source shortest paths, whose output is at most (1+epsilon)^d times the true shorte...
Definition: ApproximateShortestPaths.h:17