1 / 14

Computer Science Club

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 {

colby-riley
Télécharger la présentation

Computer Science Club

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science Club It is easier to change the specification to fit the program than vice versa.

  2. Last Week’s Problem • Denise’s fabulousness • Set of items, each with a weight and fabulousness • Max fabulousness restricted to a certain weight

  3. Solution publicclass Item { privateintweight; privateintfabulousness; public Item( int weight, int fabulousness ) { this.weight = weight; this.fabulousness = fabulousness; } publicintgetWeight() { returnweight; } publicintgetFabulousness() { returnfabulousness; } }

  4. Solution (cont.) Variables: staticint[][] fabKnapsackMemo = newint[1000][9012];

  5. 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();

  6. 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 );

  7. 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]; }

  8. 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

  9. 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!

  10. Problem of the Week • Cancelled due to USACO • Algorithm presentation instead

  11. 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?

  12. 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

  13. 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?

  14. Wrap Up • No membership dues • Remember to sign in • Take USACO • Thanks for coming

More Related