Week 10 Lecture 17 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Week 10 Lecture 17

Description:

Not so obvious stuff ... Essentially, you create a heap and then pop the Min (or Max) item until the heap ... least amount of raw work on average (comparisons ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 16
Provided by: CSU98
Category:
Tags: lecture | week

less

Transcript and Presenter's Notes

Title: Week 10 Lecture 17


1
Week 10 Lecture 17
  • Overview
  • Project 3
  • Mistakes from last lecture
  • Nearest neighbor search
  • Summary of Hashing and Heaps
  • Intro to sorting

2
Mistakes from last lecture
  • Mistake 1 You do not have to implement partial
    range queries, which I showed last lecture.
  • Four operations
  • Range queryinput x and y range
    (rectangle)output all the points in the
    rectangle
  • Exact match queryinput x and y coordinate (a
    single point)output Yes or no!
  • Partial match queryinput x and y coordinate (a
    single point)output All the points (x, ) or
    all the points (, y)
  • Nearest Neighborinput x and y coordinate (a
    single point)output The coordinates of the
    nearest neighbor

3
Another Mistake sorry ?
  • If the number of points in a sub-region is odd,
    then
  • If we split vertically
  • The left sub-region has one more point
  • If we split horizontally
  • The bottom sub-region has one more point

4
Hints Nearest Neighbor Searches
  • Obvious stuff
  • The nearest neighbor is NOT always in the
    bounding box of the query point
  • It is not necessary to search all the bounding
    boxes
  • Not so obvious stuff
  • Searching the bounding boxes that are direct
    neighbors of the query points bounding box is
    not enough

5
Example
6
Example continued
7
Example More Savings
8
Hashing Summary
  • If certain conditions are satisfied
  • Hashing provides constant time insert, remove,
    and find
  • Hash tables provide NO in-order iteration (AVL
    trees do!)
  • Name 3 conditions necessary to make hashing
    efficient (constant time)?

9
Binary Heap Summary
  • Implementation for priority queues
  • Constant time insertion (on average)
  • Log n time for remove Min value
  • Binary heap provides no in-order iteration
    without destroying the heap
  • Everything is implemented with percolate up and
    percolate down.

10
Sorting
  • Weve already talked a little about
  • Insertion sort (O(n2))
  • Merge sort (O(n log n))
  • Same purpose very different results

11
Sorting Overview
  • Important considerations
  • Time is the primary consideration
  • Space is also a consideration
  • O(n2) are clearly inferior, unless you know that
    you data is small (n lt 20). If so, O(n2)
    algorithms might do better in terms of true clock
    time.
  • There are 3 important O(n log n) algorithms
  • Mergesort
  • Heapsort
  • Quicksort
  • While order analysis tells us that these
    algorithms are the same (up to a constant), they
    are different and choosing the best one depends
    on many factors

12
O(n log n) algorithms overview
  • Quicksort
  • Must be carefully implemented!!!
  • most efficient in terms of true clock time
  • requires random access data storage
  • Heapsort
  • Least efficient in terms of clock time
  • Its hard to write a bad implementation
  • Works for both random access and linked list data
    storage
  • Mergesort
  • Most appropriate for data that is NOT random
    access
  • Works for random access data but requires O(n)
    extra memory

13
Mergesort
  • Lab 8 will cover all the important details
  • STL uses mergesort to sort list and slists
  • Merge sort requires O(n) extra memory when used
    to sort arrays (random access)
  • Some applications (bioinformatics and astronomy)
    use all available memory to store the data in
    random access structures
  • In these cases, mergesort can not be used
    (period)!
  • After lab 8, you should know
  • Why merge sort requires extra memory when sorting
    arrays, but doesnt require extra memory when
    sorting linked lists.
  • Why merge sort is O(n log n) regardless of the
    input.

14
Heapsort
  • Essentially, you create a heap and then pop the
    Min (or Max) item until the heap is empty.
  • The whole process takes O(n log n) time
  • Even if the data is stored in an array, heapsort
    can be used to sort the data without using extra
    memory (makeHeap)
  • Heapsort is not as efficient at Mergesort because
    n pops must be performed and each pop is almost
    always log n. Why?
  • Mergesort requires log n recursive levels (very
    strict) but for each level it is possible (and
    common) to do less than n work. Hopefully, in
    lab youll see why?

15
Quicksort
  • When properly implemented, it requires the least
    amount of raw work on average (comparisons and
    swaps)
  • The result is the fastest pure running time.
  • Quicksort is used to implement the STL generic
    sort routine (works only for random access
    iterators)
  • Quicksort is implemented using many tricks and is
    my favorite algorithm ?
Write a Comment
User Comments (0)
About PowerShow.com