Tutorial 8 An optional eighth tutorial will be held the week of March 6. This tutorial will give you practice with and feedback on oral presentation and slide design. If possible, bring your laptop with any slides you are developing for your presentation. Monday, March 6: 1-2pm; 2-3pm (GB304) Wednesday, March 8: 9-10am; 10-11am (BA1180) Thursday, March 9: 3-4pm; 4-5pm (GB304)
Milestone 3: Finding Routes ECE 297
Milestone 3 Overview Find shortest travel time routes Between two intersections From intersection to point of interest e.g. Tim Horton’s with minimum travel time 13% of final mark 8% algorithm (automarked) Find some path? Find shortest travel time path? Find it with low CPU time? 4% user interface (TA demo) 1% code style
Milestone 3 UI User Interface: 4% Requirements Can click on two intersections find path Can enter intersection/POI names find path Visualize path in UI Print or draw travel directions Have help button or command Optional Bonus: up to +1%: Support partial name matches E.g. Yong might complete as Yonge Street
Milestone 3 UI Usability matters Travel directions: Try it on a friend or family member! Can he/she enter intersections without lots of pain? Can he/she follow the travel directions and path display? Travel directions: Directions give info you need from start to finish? Take street segment #21 then street segment #10 … Unbelievably Bad! Take Yonge Street Then Yonge Street Then Bloor Street Still bad!
Planning Most teams find milestone 3 hardest Make plan to divide work Start early! Make plan to divide work Too big for complete pair/triplet programming Separate pieces for parallel development? UI: input (text & mouse) UI: output (directions & drawing) Hard code path to test before algorithm working Algorithm Two path searches, quite related Can split code & test Or split some helper functions (needs close comm.)
Algorithmic Approaches
Directions: How?
Model as Graph Problem (Node) (Edge) Bathurst dest source Path: sequence of connected nodes from a source to a dest
Now sequence of nodes doesn’t uniquely describe path Model as Graph Problem Bathurst back lane Now sequence of nodes doesn’t uniquely describe path dest source Path: store as sequence of nodes?
Model as Graph Problem Bathurst dest source Better: sequence of connected edges from a source to a dest
Finding Routes Find path from source to dest Any path? Fewest nodes? Is there only one path? Any path? Fewest nodes? Fewest edges? Minimum travel time! Graph texts: minimum weight path or shortest path dest source
Minimum Travel Time Definition Distance / speed limit + turn_penalty per street change Blue path: 200 m / 40 kmh + 300 m / 50 kmh + 1 turn 18 s + 21.6 s + 15 s = 54.6 s 300 m dest Red path: 100 m / 40 kmh + 200 m / 40 kmh + 200 m / 50 kmh + 2 turns 9 s + 18 s + 14.4 s + 2*15 s = 71.4 s 200 m source 100 m Which is better if turn_penalty = 15s?
Minimum Travel Time Definition Don’t left turns take longer than right turns? Yes, we’re ignoring to keep simple Street change with no turn? Still 15 s No penalty Yonge St. Bloor St. West Bloor St. East 15 s penalty Yonge St.
Other Shortest Path Applications Any Ideas?
Internet Routing Nodes: computers and switches Edges: cables source dest Nodes: computers and switches Edges: cables Find a path to connect computers Typical minimum weight path definition: Fewest routers Or least congested
Circuit Board Design Nodes: small square for metal Edges: squares we can connect Find paths to connect chip I/Os
Integrated Circuits Nodes: small grid squares for metal Edges: squares we can connect Find paths to connect gates Huge graph (tens of millions of nodes) need fast algorithms
Facebook Entire social network is stored as > billion-node graph Shortest path: how close to knowing someone? Algorithms built on shortest path: social marketing
Shortest Path Algorithm #1 Depth First Search Shortest Path Algorithm #1
Recursion? source dest int main () { Node *sourceNode = getNodebyID (sourceID); bool found = findPath (sourceNode, destID); . . . } bool findPath (Node* currNode, int destID) { ...
Recursion? bool findPath (Node* currNode, int destID) { if (currNode->id == destID) return (true); for each (outEdge of currNode) { Node *toNode = outEdge.toNode; bool found = findPath (toNode, destID); if (found) } return (false); 1 source dest 2 3 4 3
Recursion? Infinite Loop! bool findPath (Node* currNode, int destID) { if (currNode->id == destID) return (true); for each (outEdge of currNode) { Node *toNode = outEdge.toNode; bool found = findPath (toNode, destID); if (found) } return (false); Infinite Loop! source dest
How to Fix? bool findPath (Node* currNode, int destID) { if (currNode->id == destID) return (true); currNode->visited = true; for each (outEdge of currNode) { Node *toNode = outEdge.toNode; if (!toNode->visited) { bool found = findPath (toNode, destID); if (found) } return (false); toNode visited dest source
Output? Says whether or not a path was found But not what the path is! bool findPath (Node* currNode, int destID) { . . . return (true); } Says whether or not a path was found But not what the path is! Worst directions ever: yes, a path exists! How to fix? dest source