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
All-pairs shortest paths... “Floyd-Warshall algorithm” A B E D C A B C D E FROM TO Matrix representation D0D0 ABCDE
All-pairs shortest paths A B C D E D 0 = (d ij ) 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, A B C D E
All-pairs shortest paths A B C D E D 2 = (d ij ) A B C D E D 3 = (d ij ) 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
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
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; }
#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!
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
Blazing Convex Hull