KrisLibrary  1.0.0
graph/Path.h
1 #ifndef GRAPH_PATH_H
2 #define GRAPH_PATH_H
3 
4 #include <KrisLibrary/Logger.h>
5 #include <vector>
6 #include <list>
7 #include <map>
8 
14 namespace Graph {
15 
25 inline bool GetAncestorPath(const std::vector<int>& p,
26  int n,int lastAncestor,
27  std::list<int>& path) {
28  path.clear();
29  path.push_front(n);
30  if(n == lastAncestor) return true;
31  int i=0;
32  while(p[n] != -1) {
33  n = p[n];
34  path.push_front(n);
35  if(n == lastAncestor) return true;
36  if(i++ > (int)p.size()) {
37  LOG4CXX_INFO(KrisLibrary::logger(),"GetAncestorPath(): Iterated more than the number of nodes, aborting\n");
38  i=0;
39  for(std::list<int>::iterator it=path.begin();it!=path.end()&&i<20;it++,i++)
40  LOG4CXX_INFO(KrisLibrary::logger(),""<<*it);
41  LOG4CXX_INFO(KrisLibrary::logger(),"\n");
42  LOG4CXX_INFO(KrisLibrary::logger(),"...\n");
43  std::list<int>::iterator it = path.end();
44  for(int i=0;i<20;i++) it--;
45  while(it != path.end()) {
46  LOG4CXX_INFO(KrisLibrary::logger(),""<<*it);
47  it++;
48  }
49  LOG4CXX_INFO(KrisLibrary::logger(),"\n");
50  //abort();
51  return false;
52  }
53  }
54  return (lastAncestor == -1);
55 }
56 
60 inline bool GetAncestorPath(const std::vector<int>& p,
61  int n,int lastAncestor,
62  std::vector<int>& path) {
63  std::list<int> lpath;
64  if(!GetAncestorPath(p,n,lastAncestor,lpath)) return false;
65  path.resize(0);
66  path.insert(path.end(),lpath.begin(),lpath.end());
67  return true;
68 }
69 
73 template <class Node>
74 inline void GetAncestorPath(const std::map<Node,Node>& p,
75  Node n,std::list<Node>& path)
76 {
77  path.clear();
78  path.push_front(n);
79  int i=0;
80  while(p.count(n) != 0) {
81  n = p[n];
82  path.push_front(n);
83  if(i++ > p.size()) {
84  LOG4CXX_INFO(KrisLibrary::logger(),"GetAncestorPath(): Iterated more than the number of nodes, aborting\n");
85  abort();
86  }
87  }
88 }
89 
90 
91 } //namespace Graph;
92 
93 #endif
bool GetAncestorPath(const std::vector< int > &p, int n, int lastAncestor, std::list< int > &path)
Calculates a path of nodes from a list of parents.
Definition: graph/Path.h:25
A tree graph structure, represented directly at the node level.
Definition: Tree.h:25
The logging system used in KrisLibrary.
Namespace for all classes and functions in the Graph subdirectory.
Definition: ApproximateShortestPaths.h:7