A few m3 tips
Street Segment Length / Travel Time Need to compute in double precision –Otherwise too much round off See piazza
Speed Tuning 1.Algorithm 2.Data structures 3.Low level code string streetName1, streetName2; if (streetName1 != streetName2) {... int streetId1, streetId2; if (streetId1 != streetId2) {... Any speed difference?
Debugging Algorithms Start with a very simple test case –Adjacent intersections! –Step / breakpoint code in debugger –Make sure it does what you expect!
Debugging Algorithms Try a slightly harder case –Two intersections apart –Step / breakpoint code in debugger again! –Make sure code behaving exactly as you expect
Bigger Cases: Graphics Helps Hard to see large amounts of data in debugger Use graphics to animate / visualize #define VISUALIZE while (wavefront.size() != 0) { waveElem = wavefront.front(); #ifdef VISUALIZE draw_street_seg (waveElem.edgeID); flushinput(); // Draw right away #endif... Can’t leave this in when I submit (will fail unit tests). How do I turn this off when speed testing & before submitting code?
Milestone 4: Courier Company
Problem Definition Return: low travel time path Starting and ending at some depot And reaching all 2N delivery intersections And always picking up a package before delivering it and Given M courier truck depots Given N deliveries (pick up, drop off) M = 3 here, all intersections N = 4 here, all intersections DD AA AA BB BB CC CC DD
No dropping off packages before they’re picked up! Possible Solution? AA AA BB BB CC CC DD DD
BB CC Legal Solution AA AA BB CC DD DD Output: vector of street segment ids
How? Re-use m3 path-finder // Go from first depot to first package pick up path = find_path (depot[0], delivery[0].pickUp); // Complete deliveries, in order for (i = 0; i < N-1; i++) { path += find_path (delivery[i].pickUp, delivery[i].dropOff); path += find_path (delivery[i].dropOff, delivery[i+1].pickUp); } // Drop off last package path += find_path (delivery[N-1].pickUp, delivery[N-1].dropOff); // Go back to the first depot to drop off the truck path += find_path (delivery[N-1].dropOff, depot[0]);
Lots of wasted travel! Possible Solution AA AA BB BB CC CC DD DD
BB CC AA AA BB CC DD DD Need to optimize delivery order Recall: More Logical Solution
Exhaustive Algorithm? Try all possible delivery orders Pick the one with lowest travel time How many combinations? –M truck depots –N deliveries 2N pick-up + drop-off intersections –Pick one of M starting locations –Then pick one of 2N pick-up/drop-off intersections –Then one of 2N-1 for the second intersection –… (repeat until last delivery) –Then M places to drop off truck –M * 2N * (2N-1) * (2N-2) * … * 1 * M = M 2 (2N)! Some of these are illegal orders say algorithm checks legality after generating the solution
Exhaustive Algorithm? Say M = 10, N = * (2*30)! = 8.3 x Invoke find_path () 2N+1 times to get path between each intersection Say find_path takes 0.1 s (very good!) 8.3 x * 61 * 0.1 s = 5.1 x s 1.6 x years! Lifetime of universe: ~14 x 10 9 years!
Traveling Salesman Problem We are solving a variation of the traveling salesman problem Computationally hard problem –For N deliveries, no guaranteed optimal (lowest travel time solution) in polynomial time i.e. > O(N k ), for any k Means at least O(2 N ) –Need to use heuristics to solve Most research problems are computationally hard –Integrated circuit design, drug design, transportation network design, …
Pioneer of Complexity Theory Stephen Cook, U of T professor in Computer Science Created the main theory of computationally hard problems NP-complete problems –No polynomial time solution known on conventional computers –Proved that if a method to solve any of these problems in polynomial time was found, then all these problems could be solved in polynomial time –P vs. NP: most famous open problem in computer science 1970: denied tenure at UC Berkeley 1971: published most famous paper 1982: won Turing award
Digression Top 10 Programming Languages (source: IEEE Spectrum,Oct2011 Issue)
Summary C++: 9% C: 16% (25% total) Java + C#: Another 28% –(Mostly) simplified C++ –Java: Android, web apps, IBM –C#: Microsoft’s answer to Java Objective C: Another 4% –Alternative syntax for adding objects to C –Apple (Mac, iPhone, …) Total: 57% C++ related
Grace Hopper Math professor & computer engineer Lead programmer on Harvard Mark I –1 st fully programmable comp Wrote first programming manual Created first compiler A- Known as both a great engineer and great communicator –Pioneering work in programming computers & computer languages