m is for
play

M is for Linked-List Code and APTs Markov, Maps Assignment you - PowerPoint PPT Presentation

Compsci 201 M is for Linked-List Code and APTs Markov, Maps Assignment you are working on Method A function by any other name Memory New Node, New ArrayList, Susan Rodger February 26, 2020 2/26/2020 CompSci 201,


  1. Compsci 201 M is for … Linked-List Code and APTs • Markov, Maps • Assignment you are working on • Method • A function by any other name • Memory • New Node, New ArrayList, … Susan Rodger February 26, 2020 2/26/2020 CompSci 201, Spring 2020 1 2/26/2020 CompSci 201, Spring 2020 2 Announcements Plan for Today and Week • Exam 1 – Ask for Regrade in Gradescope • Review Linked List APTs • I do all the regrades • ListNode class and using it in a project • Ask for Regrades in Gradescope by Sunday, • Creating your own main for testing March 1 • Assignment P3 due tomorrow • Changing linked lists in methods • Invariants, pass-and-return, • Assignment P4 out Friday with a Part1 and Part2 • Part 1 due March 5, Part 2 due March 19 • Visualize, reason, think, code • APT 4 due Tuesday! 2/26/2020 CompSci 201, Spring 2020 3 2/26/2020 CompSci 201, Spring 2020 4

  2. Markov 2: Efficiency Naïve, Brute Force Idea • Idea related to machine learning • Given training text "the theatre through that helps" • Given a training text, use it to create a model • Generate random text based on frequencies • Using the model, generate random text • For a model-2 Markov process: start with "th" • Characters after "th": {"e","e","r","a"} • Infinite Monkey Theorem? • Don't type at random • Choose one at random, say "e": generate! • Now use with "he", since "th" + "e" = "he" • Use letter frequencies!! • Following "he": {" ", "a", "l"} • Why naïve? Re-scan text every time for follows 2/26/2020 CompSci 201, Spring 2020 5 2/26/2020 CompSci 201, Spring 2020 6 Finding Follow Characters Don't Scan N times, Scan Once • Scan entire text looking for key • https://coursework.cs.duke.edu/201spring20/p3-markovpart2-sp20 • Loop O(T) for myText with T characters • We generate N random characters • Again? • Get follows N times, each O(T), total is O(NT) • Suppose we find all N-grams, e.g., 2-grams • "th" -> {"e", "e", "r", "a"} • "he" -> {" ", "a", "l"} • … • Map of 2-gram to ArrayList of following chars • Create in O(T) time. Get follows is O(1) • So total is O(N + T) 2/ 2/ 6/2020 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/26/2020 2/2 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/ / / / 0 CompSci 201, Spring 2020 CompSci 201, Spring 2020 7 7 2/21/2020 CompSci 201, Spring 2020 8

  3. Markov Big Picture From Last Time WOTO (go over) • Use BaseMarkov as a start, create EfficientMarkov http://bit.ly/201spring20-0221-2 • Make constructors work, create map • @Override getFollows to be O(1) not O(T) • Benchmark these programs • Use WordGram rather than String • Generate word-based random text, not char • String is collection of characters, WordGram is collection of Strings • Use same idea for map, but use WordGram 2/26/2020 CompSci 201, Spring 2020 9 2/26/2020 CompSci 201, Spring 2020 10 Linked list with one Node What’s in a Node? First • Some information • Place to snap another node Bo Bo • In Java we’ll see • String reference: info • Node reference: next 2/26/2020 CompSci 201, Spring 2020 11 2/26/2020 CompSci 201, Spring 2020 12

  4. 1) Add a Node to the front 1) Add a Node to the front First First Bo Bo Bo Bo Fa Bo N nodes in linked list Running time to add one Node to front? 2/26/2020 CompSci 201, Spring 2020 13 2/26/2020 CompSci 201, Spring 2020 16 2) Add a Node to the end 4) Again - Add a Node to the end First First Bo Bo Bo Bo Me Bo Fa Fa Bo Bo 2/26/2020 CompSci 201, Spring 2020 18 2/26/2020 CompSci 201, Spring 2020 23

  5. 5) First and Last 4) Again - Add a Node to the end Add a Node to the end First First Bo Bo Bo Bo Me Me Bo Bo Fa Fa Bo Bo Temp So So Bo Bo Last N nodes in linked list Running time to add one node to end? 2/26/2020 CompSci 201, Spring 2020 29 2/26/2020 CompSci 201, Spring 2020 31 5) First and Last Where does Node go for APT? Add a Node to the end First • Where does the class Node live? Use ListNode • In same package/folder as class, e.g., APT Bo Bo Me Bo Fa Bo So Bo Bo N nodes in linked list Running time to add La Bo one node to end? Last 2/26/2020 CompSci 201, Spring 2020 35 2/26/2020 CompSci 201, Spring 2020 37

  6. Visualizing Running Code – Java Tutor Where does Node go for P4: next assignment • Simple node demo: • Where does the class Node live? • New nodes added at front: 3->2->1->0 • Nested/inner class, e.g., in LinkedStrand.java 2/26/2020 CompSci 201, Spring 2020 38 2/26/2020 CompSci 201, Spring 2020 39 ListDemo Class - Node and Count Code - create and main 2/26/2020 CompSci 201, Spring 2020 40 2/26/2020 CompSci 201, Spring 2020 41

  7. Alternatives to “plain” linked lists Alternatives to “plain” linked lists • Doubly-linked lists, node has .prev and .next • Lists with “header nodes” • Facilitates iterating from front and back • Create “dummy” node, not part of list • Some code easier don’t need “peek ahead” • First now always points to a “dummy” node First • Last always points to a node • Google "dummy header node" for details Ti Bo Bo Bo Last First Last Here is a list Re R La Bo Bo B Bo with 0 nodes 2/26/2020 CompSci 201, Spring 2020 42 2/26/2020 CompSci 201, Spring 2020 43 Source code in the wild Alternatives to “plain” linked lists • http://bit.ly/java8-linked • OpenJDK Java 8 • Lists with “header nodes” • LinkedList<String> means Node<String> • Create “dummy” node, not part of list • LinkedList<Integer> means Node<Integer> • First now always points to a “dummy” node • Last always points to a node • Google "dummy header node" for details First Last Here is a list Bo Fa with 1 node Bo 2/26/2020 CompSci 201, Spring 2020 44 2/26/2020 CompSci 201, Spring 2020 45

  8. Maintaining Invariant Class Invariant • How do we remove • Invariant: always true after each method executes the first node? • Maintain first and last as pointers to the first and • If there is a node, last nodes. What does this mean? re-assign first, • Note || notice assert! • What is GC? • Lines 177-178 … • What is first? • Line 182? first 2/26/2020 /2020 02 020 02 020 02 020 020 020 020 020 020 020 20 2 20 20 20 20 2 20 20 0 Com Com Com Com C m CompSci 201, Spring 2020 Com C m Com Com Com Com Com om om om om om m mpSc pSc ci 2 i 2 i 2 i 2 i 2 i 2 i 2 i 2 i 2 i 2 i 2 201 2 2 2 01 01 01 01 01, 01 01 01 01 01 01 01 01 01 01 01 Sp 01 01 1 Sp Sp S ring 2020 Sp Sp Sp 46 46 2/26/2020 CompSci 201, Spring 2020 47 Same code, larger , g ListCount - ListNode APT • Must use the ListNode class in same package • Not an inner class, used by all APT programs • Create project, create class, copy from APT • https://www2.cs.duke.edu/csed/newapt/listcount.html 2/26/2020 20 20 CompSci 201, Spring 2020 Com CompSc pSci 2 i 201 01, Sp Sprin ring 2 g 2020 020 48 2/26/2020 CompSci 201, Spring 2020 49

  9. Similarities and Summary WOTO • Code typically uses while(list != null) http://bit.ly/201spring20-0226-1 • Move list = list.next in body of loop • If you want to stop on last node rather than after? • Must make sure that list cannot be initially null 2/26/2020 CompSci 201, Spring 2020 50 2/26/2020 CompSci 201, Spring 2020 51 Josh Bloch Josh Bloch • Led design of Java Collections • Led design of Java Collections Framework Framework • Formerly Java Chief Architect • Formerly Java Chief Architect at Google at Google • Professor of the Practice CMU • Professor of the Practice CMU APIs should be easy to use and hard to misuse. It APIs should be easy to use and hard to misuse. It should be easy to do simple things; possible to do should be easy to do simple things; possible to do complex things; and impossible, or at least difficult, to complex things; and impossible, or at least difficult, to do wrong things. do wrong things. 2/26/2020 CompSci 201, Spring 2020 52 2/26/2020 CompSci 201, Spring 2020 53

Recommend


More recommend