1 / 22

CS261 Data Structures

CS261 Data Structures. Ordered Bag Dynamic Array Implementation. Goals. Understand the downside of unordered containers Binary Search Ordered Bag ADT. Downside of unordered collections. What is the complexity of finding a particular element in the dynamic array implementation of the Bag?

moriah
Download Presentation

CS261 Data Structures

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. CS261 Data Structures Ordered Bag Dynamic Array Implementation

  2. Goals • Understand the downside of unordered containers • Binary Search • Ordered Bag ADT

  3. Downside of unordered collections • What is the complexity of finding a particular element in the dynamic array implementation of the Bag? • What about the linked list implementation of the Bag? • Many applications will require a significant number of accesses of particular values… so we need a more efficient means for finding values.

  4. Power of Ordered Collections • Why do you suppose that dictionaries or phonebooks keep their elements in order? • Suppose I asked you to find the phone number for Chris Smith? • Suppose I asked you who was the person with phone number 753-6692?

  5. Guess My Number • We all know the heuristic of cutting a collection in half from the game “guess my number” • I’m thinking of a number between 1 and 100. What questions will you ask to find my number (efficiently)?

  6. Binary Search • The formal name for this process is binary search • Each step cuts region containing the value in half • Starting with n items, how many times can I cut in half before reaching a set of size one?

  7. Binary Search: O(log n) • A O(log n) search is much much faster than an O(n)search (for large n) • Log2 1,000,000 ~ 20 • Log of largest unsigned integer value in C (4294967295) is 32 • Instead of 4 billion comparisons, you only need 32!

  8. Binary Search… • What are the requirements for performing a binary search?

  9. Binary Search Requirments • Random access to the elements • Elements are already in sorted order

  10. Binary Search Ordered Array: Intuition • Compute the middle index • Check for the value at that index • If found the value, done, return the index • If not found • If value is less than value at the index, repeat with left half of array • Else repeat with right half of array

  11. Binary Search: Ordered Array Algorithm int _binarySearch(TYPE * data, int size, TYPE val) { int low = 0; int high = size; int mid; while (low < high) { mid = (low + high) / 2; //mid less than val looking for if (LT(data[mid],val)) low = mid + 1; else high = mid; } return low; }

  12. Binary Search Ordered Array: Return Value • If value is found, returns index of that value • If value is not found, returns position where it can be inserted without violating ordering • NOTE: returned index can be larger than a legal index int _binarySearch(TYPE * data, int size, TYPE val) { int low = 0; int high = size; int mid; while (low < high) { mid = (low + high) / 2; if (LT(data[mid],val)) low = mid + 1; else high = mid; } return low; }

  13. Ordered Bag Abstraction • Same operations as Bag ADT • Add • Contains • Remove • Property: elements maintain sorted order • How can we do this efficiently?

  14. Which operation is now faster? Using a dynamic array for an Ordered Bag, which of the following operations is made faster by using a binary search? • add(element) • contains(element) • remove(element) • Are any made slower?

  15. Applications of Ordered Collections • You will get your chance to write an implementation of a ordered bag • But first, other reasons for keeping a collection of elements in order: • Fast merge • Set operations  union, intersection, etc.

  16. Fast Merge 5 9 10 12 17 1 8 11 20 32 5 9 10 12 17 1 8 11 20 32 1 5 9 10 12 17 1 8 11 20 32 1 5 5 9 10 12 17 1 8 11 20 32 1 5 8 5 9 10 12 17 1 8 11 20 32 1 5 8 9 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10

  17. Fast Merge (cont.) 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10 11 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10 11 12 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10 11 12 17 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10 11 12 17 20 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10 11 12 17 20 32

  18. Set Operations: Similar to Merge • You can quickly merge two ordered arrays into a new ordered array • What is its complexity? • Set operations (intersection, union, difference, subset) are similar to merge • Try these on your own…(See Chapter 9)  O(n)

  19. Summary • Searching DynArr and LinkedLists are O(N) on average • Binary Search provides O(log N) search but requires that • We have random access to data (ie. data is in an array) • The data is ordered • This means, of course, that we can only do efficient binary search on an array (NOT a linked list)

  20. Question? • Why not just sort the array every time you add an element to the collection? Is it more/less efficient…or the same?

  21. Bug?? • In 2006, a bug was found by Google • http://googleresearch.blogspot.com/2006/06/ • Where is it? • How can we fix it? int _binarySearch(TYPE * data, int size, TYPE val) { int low = 0; int high = size; int mid; while (low < high) { mid = (low + high) / 2; if (LT(data[mid],val)) low = mid + 1; else high = mid; } return low; } mid = low + ((high-low) / 2)

  22. Your Turn • Now that we have _binarySearch, how do the following change? • addBag • containsBag • removeBag • Complete Worksheet #26 • Read Binary Search Correctness Argument

More Related