150 likes | 311 Views
Computer Science Club. It is easier to change the specification to fit the program than vice versa. Last Week’s Problem. Denise’s fabulousness Set of items, each with a weight and fabulousness Max fabulousness restricted to a certain weight. Solution. public class Item {
E N D
Computer Science Club It is easier to change the specification to fit the program than vice versa.
Last Week’s Problem • Denise’s fabulousness • Set of items, each with a weight and fabulousness • Max fabulousness restricted to a certain weight
Solution publicclass Item { privateintweight; privateintfabulousness; public Item( int weight, int fabulousness ) { this.weight = weight; this.fabulousness = fabulousness; } publicintgetWeight() { returnweight; } publicintgetFabulousness() { returnfabulousness; } }
Solution (cont.) Variables: staticint[][] fabKnapsackMemo = newint[1000][9012];
Solution (cont.) publicstaticintfabulousKnapsackCorrect( Item[] items, intitemNum, intmaxWeight ) { // check bounds if( itemNum < 0 || itemNum >= items.length ) return 0; // haven't already calculated this value if( fabKnapsackMemo[itemNum][maxWeight] == 0 ) { Item item = items[itemNum]; intitemWeight = item.getWeight(); intitemValue = item.getFabulousness();
Solution (cont.) // 0 items or 0 weight will both result in a // max fabulousness of 0 fabKnapsackMemo[0][maxWeight] = 0; fabKnapsackMemo[itemNum][0] = 0; // can't add this item because it's too heavy if( itemWeight > maxWeight ) // just as good as best fabulousness with one less item fabKnapsackMemo[itemNum][maxWeight] = fabulousKnapsackCorrect( items, itemNum - 1, maxWeight );
Solution (cont.) else fabKnapsackMemo[itemNum][maxWeight] = Math.max( // best fabulousness with one less item fabulousKnapsackCorrect( items, itemNum - 1, maxWeight ), // maximum fabulousness of enough weight to accommodate this item plus the fabulousness of it fabulousKnapsackCorrect( items, itemNum - 1, maxWeight - itemWeight ) + itemValue ); } // max fabulousness returnfabKnapsackMemo[itemNum][maxWeight]; }
USACO • Today is the last day to take the contest • http://contest.usaco.org • If you want to try it out or practice, take bronze • Bronze doesn’t count for anything • Everybody should at least try silver/gold • Read the problem and directions • Read the tips for Java on the contest page • The timer starts when you click the problems • Read the input conditions • Read how they want you to process input/output
USACO (cont.) • ~3 hours (2 for qualification rounds) • ~3-4 problems (2 for qualification rounds) • Header:/*ID: youridhereLANG: C/C++/JAVAPROG: progname*/ • Scan: “progname.in” and write: “progname.out” • Good luck!
Problem of the Week • Cancelled due to USACO • Algorithm presentation instead
Dijkstra’s Algorithm Used to find the shortest path between a starting position and destination. Given a graph with specified distances of the directional paths between nodes: Task: Find the shortest path from Node a (initial node) to Node f (destination). For example, A-C-D-F has a distance of 3 + 10 + 2 = 15. The path A-C-E-F has a distance of 3 + 3 + 5 = 11. Is there a path even SHORTER than that? Can you be sure your path is the shortest possible?
Dijkstra’s(cont.)The Steps • Step 1: Each node has a distance value. (a = 0, others = ∞) • Step 2: Two node sets: visited and unvisited (init: none visited) • Step 3: a = curNode • Step 4: (recursive step!) • Update unvisited neighbors of curNodewith new shortest dist • move curNode to visited set • new curNode = smallest unvisited node 2 3
Dijkstra’s(cont.) Finishing Up When curNode = destination, shortest path = value of final node. Try to trace the algorithm and find the shortest path and minimal distance. 2 3 Consider… Why don’t we need to re-check visited nodes? Why can’t there be a shorter path to a visited node?
Wrap Up • No membership dues • Remember to sign in • Take USACO • Thanks for coming