Download presentation
Presentation is loading. Please wait.
Published byQuentin Philip Jenkins Modified over 9 years ago
1
Another dynamic program... +----------+ %...*..*..*% %..........% %*..*******% %*.*.***.*.% %...@.*...*% Q: What is the shortest path that delivers papers to each * ? door stairs Table: tracks the shortest path that delivers all of the papers up to floor N, ending on both the left and the right. Ends left Ends right Floor21345 measured up to the last paper
2
All-pairs shortest paths... “Floyd-Warshall algorithm” A B E D C 8 13 1 6 12 9 7 0 11 0813-1 -0-612 -90-- 7-00- ---110 A B C D E FROM TO Matrix representation D0D0 ABCDE
3
All-pairs shortest paths... 0813-1 -0-612 -90-- 7-00- ---110 A B C D E D 0 = (d ij ) 0 0813-1 -0-612 -90-- 7-00- ---110 A B C D E D 1 = (d ij ) 1 d ij = shortest distance from i to j through {1, …, k} k d ij = k In general, 0813-1 -0-612 -90-- 7 15 00 8 ---110 A B C D E
4
All-pairs shortest paths... 0813 14 1 -0-612 -901521 715008 ---110 A B C D E D 2 = (d ij ) 2 0813141 -0-612 -901521 7 9 008 ---110 A B C D E D 3 = (d ij ) 3 0813141 130 6 612 22901521 79008 182011 0 A B C D E D 4 = (d ij ) 4 A B C D E D 5 = (d ij ) 5 to store the path, another matrix can track the last intermediate vertex 0812 1 1306612 22901521 79008 182011 0
5
Floyd-Warshall Pseudocode Input:(the initial edge-cost matrix) Output: (the final path-cost matrix) D 0 = (d ij ) 0 D n = (d ij ) n for k = 1 to n // intermediate vertices considered for i = 1 to n // the “from” vertex for j = 1 to n // the “to” vertex d ij = min{ d ij, d ik + d kj } k-1 k best, ignoring vertex k best, including vertex k
6
Code quirks for ( i = cols - 2; i >= 0; i++ ) { if ( reachable[i + 1] && !reachable[i] && fits(row, i) ) { reachable[i] = 1; } #include void f(int& i) { i++; } void f(int& i, int& j) { i = j = i += j; } int main() { int i = 10, j = 100; f(( i++, j )); printf("i is %d and j is %d\n",i,j); return 0; }
7
#include,,,,, string start, finish; // the starting and ending cities map > dests; // children (destinations) of each city void BFS() { map visited; // a list of cities already visited in the BFS queue > q; // the queue of nodes not yet visited pair current(start,0); // an STL pair is used to maintain path length list ::iterator i; // used to run through each city’s children bool failed = false; while (current.first != finish) // current.first == current city checked for goal { string city = current.first; // give current.first a simpler name visited[city] = true; // mark that city as already visited for(i = dests[city].begin(); i != dests[city].end(); i++) // for each child, if (!visited[*i]) // if it’s unvisited, q.push( pair (*i, current.second+1) ); // we put it and its path length // onto q if (q.empty()) { failed = true; break; } current = q.front(); // set up for next loop q.pop(); } if (failed) cout << "Try again next week." << endl; // no way to get thre else cout << current.second << endl; // prints path length } BFS from We ship cheap! message: STL is good!
8
Things to consider... Saturday: meet at Olin 1265 (or the parking lot north of Olin) at 8:15 AM (a bit earlier to go for breakfast) What to consider bringing We have whiteboards & markers printouts of ACM problems’ code other code you know/want to use C/C++ references algorithm references (CLR, e.g.) configuration files:.emacs,.vimrc,.bashrc,.cshrc,.aliases, etc. anything else you might want to have (Any written material is OK, digital media is not permitted and there is no web access.) Other Info 60 second CPU time limit C++ files must end in.C available commands: compile myfile.C submit # myfile.C question filename answers score, timeleft, getdata compiles a file submits a file for problem number # asks the judges a question (in a file) looks at all answers to questions gets score, time remaining, or data files
9
Blazing Convex Hull
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.