110 likes | 196 Views
CSS342: Project. Instructor: Munehiro Fukuda. Statement of Work. Find the shortest bus route from some origin bus stop to some destination? There may be multiple bus routes between those two buses
E N D
CSS342: Project Instructor: Munehiro Fukuda CSS342: Project
Statement of Work • Find the shortest bus route from some origin bus stop to some destination? • There may be multiple bus routes between those two buses • Such a query may be given repeatedly, but new routes may be added or existing routes may be deleted between those queries. • Let’s start from an easier problem: find if there is a bus route from some origin bus stop to some destination. CSS342: Project
Z Y W S R P T X Q Route Search by Backtrack How can you find a path from bus stop P to Z? • Exhaustive search: • Go forward to a neighboring city until • You eventually reach Z • You reach X where no departing buses are served. • You reach W twice after visiting S and T • Backtrack to the previous bus stop • if encountering the last two of the above conditions. How can you backtrack to the previous city? • Recursively call findPath( )to maintain the sequence of visited bus stops in each activation record What if you come back to the origin? • This means there are no more routes to the destination. #5 10min #4 10min #4 20min #3 10min #3 15min #3 10min #4 10min #1 10min #1 10min #2 10min CSS342: Project
Z Y W S R P T #6 15min X Q Transactions #5 10min • Assume that all bus stops are given in priori. • We will receive the following three transactions during execution. • Add a new bus route #6 from P to X which takes 15 minutes A P X 6 15 • Delete the existing bus route #4 from Y to R which takes 20 minutes D Y R 4 20 • Query if there is an route from P to Z Q P Z 0 0 Your answer should be: • Take route #4 from P to W (10min) • Take route #4 from W to Y (10min) • Take route #5 from Y to Z (10min) #4 10min #4 20min #3 10min #3 15min #3 10min #4 10min #1 10min #1 10min #2 10min CSS342: Project
Map class Map { public: Map( ); // read bus stop information into the map ~Map( );// free all dynamically allocated data bool addRoute( Transaction& transaction ); bool deleteRoute( Transaction& transaction ); bool findPath( Transaction ); // internally call a private findPath( ) Private: Busstop *busstop; // a pointer to an internal data structure // maintaining bus stop information bool findPath( string const &origin, string const &destination ); // more utility functions } CSS342: Project
A Recursive Solution bool Map::findPath( const string& origin, const string& destination) { Mark origin as visited; if ( origin is destination ) // A base case return true; else for ( each unvisited bus stopB adjacent to origin ) findPath( C, destination ); } Smaller search problem You have reached Smaller search problem P W You have reached Y Z Then, what if you don’t find any path? T S R CSS342: Project
A Recursive Solution (cnt’d) When there are no more next cities or search failed, it is a base case. bool Map::findPath( const string& origin, const string& destination) { string nextBusStop; bool nextBusStopFound, searchDone; markVisited(origin); if ( origin == destination ) return true; else { searchDone = false; nextBusStopFound = getnextBusStop( origin, nextBusStop ); while ( nextBusStopFound && !searchDone ) { searchDone = findPath( nextBusStop, destination ); if ( !searchDone ) nextBusStopFound = getNextBusStop( origin, nextBusStop ); } return searchDone; } } CSS342: Project
Z org = r dest=z findPath(x): f no more stop return f org = x dest=z no more stop return f Y org = w dest=z findPath(s): f findPath(y): t return t org = s dest=z findPath(t): f no more stop return f org = t dest=z no more stop return f found W S R P T org = y dest=z findPath(z): t return t org = z dest=z reached! return t X Q Tracing a Recursive Solution org = p dest=z findPath(R): f findPath(w): t return t CSS342: Project
What If We Are Interested in Finding the Shortest Path? • What does bool Map::shortestpath( … ) have to do? A A 10min 10min 10min Then, how about this rout? 10min C B B C 15min 10min 15min E 10min Then, should we go further? 10min D D Instead of simply marking each bus stop, you should write down the driving time so far. If it was larger than the current driving so far, you should go further, otherwise simply perform backtrack. You really need an exhaustive search CSS342: Project
Z Y W S R P T X Q Adjacency List for the Flight Map P head R W Q head X R head X S head T T head W W head S Y X head head Y R Z Z head CSS342: Project
What you have to follow: • Use a queue for storing transactions • Use an array for bus stops, each maintaining a adjacency list of bus stops. • Use Quicksort to sort bus stop array in the alphabetical order of their names. • Use a stack for traversing reverse routes from a destination to an origin. CSS342: Project