
CS 758/858: Algorithms ■ COVID http://www.cs.unh.edu/~ruml/cs758 Tries BSTs Algorithms DP Wheeler Ruml (UNH) Class 8, CS 758 – 1 / 24 COVID ■ COVID Tries BSTs Algorithms DP ■ check your Wildcat Pass before coming to campus ■ if you have concerns, let me know Wheeler Ruml (UNH) Class 8, CS 758 – 2 / 24 ■ COVID Tries ■ Problem ■ Searching ■ Searching ■ Tries ■ Not Tries ■ Searching ■ Problem ■ Break Tries BSTs Algorithms DP Wheeler Ruml (UNH) Class 8, CS 758 – 3 / 24 Problem Statement ■ COVID Given a linked list of items, print all possible subsets. Tries ■ Problem ■ Searching ■ Searching running time? ■ Tries ■ Not Tries ■ Searching ■ Problem ■ Break BSTs Algorithms DP Wheeler Ruml (UNH) Class 8, CS 758 – 4 / 24 Searching ■ COVID Structure Find Insert Delete Tries List (unsorted) ■ Problem ■ Searching List (sorted) ■ Searching Array (unsorted) ■ Tries ■ Not Tries Array (sorted) ■ Searching Heap ■ Problem ■ Break Hash table BSTs Binary tree (unbalanced) Algorithms Binary tree (balanced) DP Wheeler Ruml (UNH) Class 8, CS 758 – 5 / 24 Searching ■ COVID What about long keys? Tries ■ Problem ■ Searching Can we detect miss without examining entire key? ■ Searching ■ Tries ■ Not Tries ■ Searching ■ Problem ■ Break BSTs Algorithms DP Wheeler Ruml (UNH) Class 8, CS 758 – 6 / 24 Tries ■ COVID trie: test digits of key, branching on values Tries ■ ■ Problem some nodes do not hold values ■ Searching ■ fixed order ■ Searching ■ Tries ■ depth = length ■ Not Tries ■ ■ Searching canonical representation ■ Problem ■ Break retrieval BSTs Algorithms CLRS: ‘trie’ = ‘radix tree’ DP Wikipedia: ‘trie’ 6= ‘radix tree’ Sedgewick: ‘trie’ 6= ‘digital search tree’ duplicate keys? what’s their weakness? Wheeler Ruml (UNH) Class 8, CS 758 – 7 / 24 Not Tries ■ COVID Wikipedia ‘radix tree’ = ‘radix trie’ = ‘patricia trie’: compressed Tries trie, every internal node has at least two leaves beneath ■ Problem ■ Searching ■ Searching Sedgewick: ‘digital search tree’: value at every node, just like ■ Tries ■ Not Tries binary trees except test bits instead of full compare ■ Searching ■ Problem ■ Break BSTs Algorithms DP Wheeler Ruml (UNH) Class 8, CS 758 – 8 / 24 Searching ■ COVID Structure Find Insert Delete Tries List (unsorted) ■ Problem ■ Searching List (sorted) ■ Searching Array (unsorted) ■ Tries ■ Not Tries Array (sorted) ■ Searching Heap ■ Problem ■ Break Hash table BSTs Binary tree (unbalanced) Algorithms Binary tree (balanced) DP Trie Wheeler Ruml (UNH) Class 8, CS 758 – 9 / 24 Problem Statement ■ COVID Given a list of records, which may contain duplicates, return a Tries list containing each record at most once. ■ Problem ■ Searching ■ Searching ■ Tries ■ Not Tries ■ Searching ■ Problem ■ Break BSTs Algorithms DP Wheeler Ruml (UNH) Class 8, CS 758 – 10 / 24 Break ■ ■ COVID asst 4 ■ Tries asst 5 ■ Problem ■ Searching ■ Searching ■ Tries ■ Not Tries ■ Searching ■ Problem ■ Break BSTs Algorithms DP Wheeler Ruml (UNH) Class 8, CS 758 – 11 / 24 ■ COVID Tries BSTs ■ BST Insert ■ Find-Parent ■ Property 1 ■ Property 2 Algorithms DP Binary Search Trees Wheeler Ruml (UNH) Class 8, CS 758 – 12 / 24 Binary Search Tree Insertion ■ COVID Invariant: For each node n, all nodes in n’s left subtree ≤ n, Tries all nodes in right subtree ≥ n. BSTs ■ BST Insert ■ Find-Parent ■ Property 1 insert (n) ■ Property 2 1. n’s parent ← find-parent(n, root, nil) Algorithms 2. if parent is nil DP 3. root ← n 4. else 5. if n should be before parent 6. parent’s left child ← n 7. else 8. parent’s right child ← n Wheeler Ruml (UNH) Class 8, CS 758 – 13 / 24 Find-Parent Specification ■ COVID Need: Tries 1) find-parent returns nil iff empty tree, BSTs 2) find-parent returns leaf node p directly adjacent to n. Ie, ■ BST Insert ■ Find-Parent either ■ Property 1 ■ Property 2 predecessor(p) ≤ n ≤ p or Algorithms p ≤ n ≤ successor(p) DP so that attaching n to p preserves BST ordering. Wheeler Ruml (UNH) Class 8, CS 758 – 14 / 24 Property 1 ■ COVID find-parent(n, curr, parent) Tries 9. if curr doesn’t exist BSTs ■ BST Insert 10. return parent ■ Find-Parent 11. if n should be before curr ■ Property 1 ■ Property 2 12. return find-parent(n, curr’s left child, curr) Algorithms 13. else DP 14. return find-parent(n, curr’s right child, curr) 1) called as find-parent(n, root, nil), so only way return value is nil is if curr=root=nil (ie, empty tree). 2) Invariant: if we replace curr with n (and ignore curr’s children), n is in proper place in the tree. Wheeler Ruml (UNH) Class 8, CS 758 – 15 / 24 Property 2 ■ COVID Invariant: If we replace curr with n (and ignore curr’s children), Tries n is in proper place in the tree. BSTs ■ BST Insert Initialization: curr is root. Replacing root with n has n in correct ■ Find-Parent place with respect to the zero remaining nodes. ■ Property 1 ■ Property 2 Maintenance: We were the correct position if curr were replaced Algorithms DP by n. To set up next iteration, we move to the correct side of curr. This preserves BST ordering with respect to curr as we move below it and it (and its other child) enters the ‘active tree’. Termination: We terminate when we move off the correct side of a leaf. The BST invariant holds everywhere if that null pointer were replaced by n because there are no children to ignore. Thus, we know that: predecessor(p) ≤ n ≤ p or p ≤ n ≤ successor(p) Wheeler Ruml (UNH) Class 8, CS 758 – 16 / 24 ■ COVID Tries BSTs Algorithms ■ Algorithms ■ Types of Algs DP Algorithms Wheeler Ruml (UNH) Class 8, CS 758 – 17 / 24 Algorithms Beyond craftsmanship lies invention, and it is ■ COVID here that lean, spare, fast programs are born. Almost Tries always these are the result of strategic breakthrough BSTs rather than tactical cleverness. Sometimes the Algorithms ■ Algorithms strategic breakthrough will be a new algorithm, such ■ Types of Algs the Cooley-Tukey Fast Fourier transform or the DP substitution of an n log n sort for an n2 set of comparisons. Much more often, strategic breakthrough will come from redoing the representation of the data or tables. This is where the heart of a program lies. Show me your flowchart and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won’t usually need your flowchart; it’ll be obvious. — Fred Brooks, 1974 (lead on IBM System/360, Turing Award 1999) Wheeler Ruml (UNH) Class 8, CS 758 – 18 / 24 Algorithms: Modern Version ■ COVID Smart data structures and dumb code works a lot Tries better than the other way around. BSTs Algorithms — Guy Steele, 2002 (ACM Fellow, inventor of ■ Algorithms Scheme, editor of The Hacker’s Dictionary) ■ Types of Algs DP Wheeler Ruml (UNH) Class 8, CS 758 – 19 / 24 Types of Algorithms ■ ■ COVID divide and conquer Tries ■ dynamic programming BSTs ■ greedy Algorithms ■ backtracking ■ Algorithms ■ ■ Types of Algs (reduction) DP Wheeler Ruml (UNH) Class 8, CS 758 – 20 / 24 ■ COVID Tries BSTs Algorithms DP ■ Fibonacci ■ Memoization ■ EOLQs Dynamic Programming Wheeler Ruml (UNH) Class 8, CS 758 – 21 / 24 Fibonacci Numbers ■ COVID 0 if n =0 Tries Fn = 1 if n =1 BSTs Fn−1 + Fn−2 for n ≥ 2 Algorithms DP ■ Fibonacci What is the complexity of the naive algorithm? ■ Memoization ■ EOLQs How to make this efficient? Wheeler Ruml (UNH) Class 8, CS 758 – 22 / 24 Memoization ■ ■ COVID recursive decomposition Tries ■ polynomial number of subproblems BSTs ■ cache results in look-up table Algorithms DP one form of dynamic programming ■ Fibonacci ■ Memoization ■ EOLQs Wheeler Ruml (UNH) Class 8, CS 758 – 23 / 24 EOLQs ■ ■ COVID What’s still confusing? Tries ■ What question didn’t you get to ask today? BSTs ■ What would you like to hear more about? Algorithms DP Please write down your most pressing question about algorithms ■ Fibonacci and put it in the box on your way out. ■ Memoization ■ EOLQs Thanks! Wheeler Ruml (UNH) Class 8, CS 758 – 24 / 24.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages24 Page
-
File Size-