1 / 11

CSS342: Project

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

sadah
Download Presentation

CSS342: Project

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSS342: Project Instructor: Munehiro Fukuda CSS342: Project

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

More Related