Transit Planner
1.0
An experiment on transfer patterns robustness
|
00001 // Copyright 2012: Eugen Sawin, Philip Stahl, Jonas Sternisko 00002 #ifndef SRC_QUERYGRAPH_H_ 00003 #define SRC_QUERYGRAPH_H_ 00004 00005 #include <queue> 00006 #include <set> 00007 #include <string> 00008 #include <vector> 00009 #include <utility> 00010 #include <map> 00011 #include "gtest/gtest_prod.h" // Needed for FRIEND_TEST in this case. 00012 00013 using std::set; 00014 using std::string; 00015 using std::vector; 00016 00017 class DirectConnection; 00018 class TransferPatternsGraph; 00019 typedef TransferPatternsGraph TPG; 00020 class LabelVec; 00021 00022 typedef std::pair<int, int> IntPair; 00023 // Label and nodes of all optimal paths. 00024 typedef std::map<IntPair, vector<int> > SearchResult; 00025 00026 00027 // Represents a QueryGraph for the Query(A,B) 00028 class QueryGraph { 00029 public: 00030 static const int INVALID_NODE; 00031 00032 // Constructs a QueryGraph for query (TPG(stop A), stop B). 00033 QueryGraph(const TPG& tpgOrigin, const int destStop); 00034 FRIEND_TEST(QueryGraphTest, Constructor); 00035 00036 // Returns the list of successors of a node. 00037 const set<int>& successors(const int node) const; 00038 00039 // Get the stop index of a node. 00040 int stopIndex(const int node) const; 00041 00042 // Get the node Index of a stop. Returns INVALID_NODE if the stop is not in 00043 // the graph. 00044 int nodeIndex(const int stop) const; 00045 00046 // Get source and target nodes. 00047 const int sourceNode() const { return 0; } 00048 const int targetNode() const; 00049 00050 // Get the size of the query graph (the number of nodes) 00051 const size_t size() const { return _stops.size(); } 00052 00053 // Get the number of arcs in the query graph 00054 const size_t countArcs() const; 00055 00056 // Returns true, if there is no outgoing arc from the origin node. 00057 bool empty() const; 00058 00059 // Merge the query graph with a second query graph specified by the given tpg 00060 void merge(const TPG& tpgOrigin, const int destStop); 00061 FRIEND_TEST(QueryGraphTest, merge); 00062 FRIEND_TEST(QueryGraphTest, mergeGraphs); 00063 00064 // Adds the given stop to the query graph 00065 int addStop(const int stop, const set<int>& successors); 00066 FRIEND_TEST(QueryGraphTest, addStop); 00067 00068 // Checks if the query graph contains a transfer pattern. 00069 bool containsPattern(const vector<int>& stops) const; 00070 // Checks if the query graph contains a transfer pattern and returns the 00071 // the corresponding QueryGraph. Returns the empty query graph otherwise. 00072 const QueryGraph findPattern(const vector<int>& stops) const; 00073 00074 // Debug: Creates a string representation of the graph. 00075 string debugString() const; 00076 // Debug: Generates all transfer patterns represented by the graph. 00077 const vector<vector<int> > generateTransferPatternsRecursive() const; 00078 // Debug: Generates all transfer patterns represented by the graph (iterative) 00079 const vector<vector<int> > generateTransferPatterns() const; 00080 00081 private: 00082 // Standard constructor is private. 00083 QueryGraph(); 00084 // Recursively walks the graph until the destination node. Stores the 00085 // sequences of nodes traversed (transfer patterns). 00086 void recurse(const int node, vector<int> stops, 00087 vector<vector<int> >* patterns) const; 00088 00089 // Stops in the QueryGraph. Origin is at position 0, destination at 1. 00090 vector<int> _stops; 00091 // Map from stop index to node index; 00092 std::map<int, int> _nodeIndex; 00093 00094 // List of succeeding nodes for each node in the QueryGraph. == AdjacencyLists 00095 vector<set<int> > _successors; 00096 }; 00097 00098 00099 #endif // SRC_QUERYGRAPH_H_