Download presentation
Presentation is loading. Please wait.
1
Compsci 201 Priority Queues & Autocomplete
Owen Astrachan Jeff Forbes November 10, 2017 11/10/17 Compsci 201, Fall 2017, Searching
2
T is for … TeraFLOPS Xbox One X @ 6 x1012 operations per second
Turing award Award for highest distinction in CS – 2016 Tree More versions to come Trie O(#characters) search 11/10/17 CompSci 201, Fall 2017, Search
3
Plan for the Day Introduce data structures and algorithms for search
Heaps for Priority Queues Towards Autocomplete 11/10/17 CompSci 201, Fall 2017, Search
4
What’s in Common? 11/10/17 CompSci 201, Fall 2017, Search
5
Priority! Applications of Priority Queues
Shortest Path: Google Maps to Internet Routing Event based simulation: Predicting collisions Best-first search, game-playing, AI Java code below sorts list. How? Why?
6
How are PriorityQueues implemented?
Heap is an array-based implementation of a binary tree used for implementing priority queues, supports: insert, findMin, deleteMin: complexities? Using array minimizes storage (no explicit pointers), faster too --- children are located by index/position in array Heap is a binary tree with shape property, heap/value property shape: tree filled at all levels (except perhaps last) and filled left-to-right (complete binary tree) value: each node has value smaller than both children
7
Using an array for a Heap
Store “node values” in array beginning at index 1 Could also store starting at index 0 For node with index k left child: index 2*k right child: index 2*k+1 parent: index k/2 Why is this structure conducive for maintaining heap shape? What about value property? Is the heap a search tree? Where is minimal node? 1 2 3 4 5 6 7 8 9 10 17 13 25 21 19 6 10 7 17 13 9 21 19 25
8
Heap questions Where is minimal element? Root, why?
Where is maximal element? What is complexity of find max in a min-heap? Why? Where is second smallest element? Why? 6 10 7 17 13 9 21 19 25 1 2 3 4 5 6 7 8 9 10 17 13 25 21 19
9
Adding values to heap To maintain heap shape, must add new value in left-to-right order of last level could violate heap property move value “up” if too small Change places with parent if heap property violated stop when parent is smaller stop when root is reached Pull parent down, swapping isn’t necessary (optimization) 13 6 10 7 17 9 21 19 25 insert 8 13 6 10 7 17 9 21 19 25 8 6 10 7 17 9 21 19 25 13 8 bubble 8 up 6 7 17 9 21 19 25 8 13 10
10
Heap add implementation
13 6 10 7 17 9 21 19 25 13 6 10 7 17 9 21 19 25 8 1 2 3 4 5 6 7 8 9 10 17 13 25 21 19 ArrayList<Integer> list
11
Removing extremal element
Where is minimal element? If we remove it, what changes, shape/property? How can we maintain shape? “last” element moves to root What property is violated? After moving last element, subtrees of root are heaps, why? Move root down (pull child up) does it matter where? When can we stop “re-heaping”? Less than both children Reach a leaf 13 6 10 7 17 9 21 19 25 13 25 10 7 17 9 21 19 13 7 10 25 17 9 21 19 13 7 10 9 17 25 21 19
12
Priority Queue implementations
Implementing priority queues: average and worst case Insert average Getmin (peek) worst (delete) Unsorted ArrayList O(1) O(n) Sorted ArrayList O(1)/ O(n) Heap O(log n) Balanced binary search tree O(log n)/ O(1) Heap has O(1) find-min (no delete) and O(n) build heap
13
Problem 1 Big Oh if PriorityQueue implemented with Heap?
Keep track of top M of N elements (N >> M). Big Oh if PriorityQueue implemented with Heap?
14
Problem 2 Determine the number of elements < target in heap
lessCount(heap,13) → 3 lessCount(heap,8) → 1 O(k) where k is #of elements less than target What is you had helper method that returns # elements in subheap rooted/starting at index that are less than target? int lessCount(int[] heap, int index, int target) What’s the initial call to helper method? 11/10/17 CompSci 201, Fall 2017, Search
15
What is autocomplete? As user types in search box
Give potential completions. How? Efficiency is key 50 ms or go home Data (Terms) Possible words/phrases Weights
16
Searching public interface Autocompletor { // Returns the top k matching terms in descending order of weight. public Iterable<String> topMatches(String prefix, int k); // Returns the single top matching term public String topMatch(String prefix); // Return the weight of a given term public double weightOf(String term); }
17
The Term class The Term class encapsulates a Comparable word-weight pair. Includes completed compareTo method, which sorts lexicographically. You are responsible for implementing three Term Comparators: WeightOrder, which sorts in ascending weight order ReverseWeightOrder, which sorts in descending weight order PrefixOrder, which sorts by the first r characters
18
Comparable and Comparator
Both are interfaces, there is no default implementation Contrast with .equals(), default implementation? Contrast with .toString(), default? Where do we define a Comparator? In its own .java file, nothing wrong with that Private, used for implementation and not public behavior Use a nested class, then decide on static or non-static Non-static is part of an object, access inner fields In Term class: once written initialize with new Term.ReverseWeightOrder(); or new Term.PrefixOrder(2);
19
Comparator’s compare x ≤ y
takes two Objects (of the same class) as arguments Returns a negative value if the 1st is less than 2nd zero if the arguments are equal, and a positive value if the 1st is greater than the 2nd compare will be called by methods in Arrays.sort or PriorityQueue? Why? x ≤ y is equivalent to compare(x,y) <= 0
20
PrefixOrder The goal of PrefixOrder is to sort lexicographically, but only considering the first r characters. e.g. normally we would put “energy” before “entropy” lexicographically. However, PrefixOrder with r = 2 considers them equal (PrefixOrder with r = 3 would still put “energy” before “entropy”, however). If one or both of the words is shorter than r characters, we just use normal lexicographic sorting. For full credit, PrefixOrder’s compare method should take O(r).
21
BruteAutocomplete Naïve approach to autocomplete
Store data as a Term array. Find the top k matches: iterates through the array, pushes all terms starting with the prefix onto a max-priority queue sorted by weight. Return top k terms off that priority queue Find top match is similar
22
Why is BruteAutocomplete bad?
If we have n terms, m of which start with the prefix, then topKmatches is O(n + m log m) and topMatch is O(n). Why is this bad? Imagine Google! So, we wish to improve upon BruteAutocomplete, by only considering those terms that start with prefix
23
Improving BruteAutocomplete
BruteAutocomplete had to iterate through every single term in the array because it did not have any prior knowledge as to where terms starting with the prefix could be located – i.e. the array was unsorted. If we sort the array lexicographically, then all terms which start with the prefix will be adjacent. Sorting takes O(n log n), but we only have to do it once every call to topMatch or topKMatches, regardless of inputs, can use the same sorted Term array. Need to locate terms starting with the prefix quickly. Use binary search.
24
Binary Search Search for 5? Where to go? How to reduce problem size?
25
Binary Search How did problem change?
26
Binary Search
27
BinarySearchAutocomplete
For Autocomplete: find the range of all terms comparator considers equal to key e.g., all terms with a that match prefix auto BinarySearchAutocomplete is the 2nd class you should implement. BinarySearchAutocomplete implements Autocompletor plus: public static int firstIndexOf(Term[] a, Term key, Comparator<Term> comp) public static int lastIndexOf(Term[] a, Term key, Comparator<Term> comp) Use binary search to quickly return the first and last index respectively of an element in the input array which the comparator considers equal to key. We specify first and last index because there could be multiple Terms in a which the comparator considers equal to key.
28
Binary search code
29
Modifications for BinarySearchAutocomplete
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.