1 / 96

O() Analysis of Methods and Data Structures

Learn about the analysis of algorithms and data structures using O() notation. Explore different methods and their performance on various data structures. Available online.

mattl
Download Presentation

O() Analysis of Methods and 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. Lecture 24 O() Analysis of Methods and Data Structures Reasonable vs. UnreasonableAlgorithms Using O() Analysis in Design

  2. Now Available Online! http://www.coursesurvey.gatech.edu

  3. O() Analysis of Methods and Data Structures

  4. The Scenario • We’ve talked about data structures and methods to act on these structures… • Linked lists, arrays, trees • Inserting, Deleting, Searching, Traversal, Sorting, etc. • Now that we know about O() notation, let’s discuss how each of these methods perform on these data structures!

  5. Recipe for Determining O() • Break algorithm down into “known” pieces • We’ll learn the Big-Os in this section • Identify relationships between pieces • Sequential is additive • Nested (loop / recursion) is multiplicative • Drop constants • Keep only dominant factor for each variable

  6. Array Size and Complexity How can an array change in size? MAX is 30 ArrayType definesa array[1..MAX] of Num We need to know what N is in advance to declare an array, but for analysis of complexity, we can still use N as a variable for input size.

  7. Traversals • Traversals involve visiting every element in a collection. • Because we must visit every node, a traversal must be O(N) for any data structure. • If we visit less than N elements, then it is not a traversal.

  8. LB Comparing Data Structures and Methods Data Structure Traverse Unsorted L List N Sorted L List N Unsorted Array N Sorted Array N Binary Tree N BST N F&B BST N

  9. Searching for an Element Searching involves determining if an element is a member of the collection. • Simple/Linear Search: • If there is no ordering in the data structure • If the ordering is not applicable • Binary Search: • If the data is ordered or sorted • Requires non-linear access to the elements

  10. Simple Search • Worst case: the element to be found is the Nth element examined. • Simple search must be used for: • Sorted or unsorted linked lists • Unsorted array • Binary tree • Binary Search Tree if it is not full and balanced

  11. LB Comparing Data Structures and Methods Data Structure Traverse Search Unsorted L List N N Sorted L List N N Unsorted Array N N Sorted Array N Binary Tree N N BST N N F&B BST N ?

  12. 14 11 42 7 58 Balanced Binary Search Trees If a binary search tree is not full, then in the worst case it takes on the structure and characteristics of a sorted linked list with N/2 elements.

  13. Binary Search Trees If a binary search tree is not full or balanced, then in the worst case it takes on the structure and characteristics of a sorted linked list. 7 11 14 42 58

  14. Example: Linked List • Let’s determine if the value 83 is in the collection: Head \\ 42 5 19 35 83 Not Found!

  15. Simple/Linear Search Algorithm cur <- head loop • exitif(cur = NIL) OR (cur^.data = target) • cur <- cur^.next endloop if(cur <> NIL) then • print( “Yes, target is there” ) else • print( “No, target isn’t there” ) endif

  16. Pre-Order Search Traversal Algorithm • As soon as we get to a node, check to see if we have a match • Otherwise, look for the element in the left sub-tree • Otherwise, look for the element in the right sub-tree 14 Left ??? Right ???

  17. cur 94 36 67 14 9 3 Find 9 If I have to watch one more of these I think I’m going to die. LB 22

  18. Big-O of Simple Search • The algorithm has to examine every element in the collection • To return a false • If the element to be found is the Nth element • Thus, simple search is O(N).

  19. Binary Search • We may perform binary search on • Sorted arrays • Full and balanced binary search trees • Tosses out ½ the elements at each comparison.

  20. 34 25 45 21 29 41 52 Full and Balanced Binary Search Trees Contains approximately the same number of elements in all left and right sub-trees (recursively) and is fully populated.

  21. 7 12 42 59 71 86 104 212 Binary Search Example Looking for 89

  22. 7 12 42 59 71 86 104 212 Binary Search Example Looking for 89

  23. 7 12 42 59 71 86 104 212 Binary Search Example Looking for 89

  24. 7 12 42 59 71 86 104 212 Binary Search Example 89 not found – 3 comparisons3 = Log(8)

  25. Binary Search Big-O • An element can be found by comparing and cutting the work in half. • We cut work in ½ each time • How many times can we cut in half? • Log2N • Thus binary search is O(Log N).

  26. N 2 4 8 16 32 64 128 256 512 Searches 1 2 3 4 5 6 7 8 9 LB What?

  27. log2(N) log2(2) log2(4) log2(8) log2(16) log2(32) log2(64) log2(128) log2(256) log2(512) Searches 1 2 3 4 5 6 7 8 9 LB What? = = = = = = = = =

  28. lg(N) lg(2) lg(4) lg(8) lg(16) lg(32) lg(64) lg(128) lg(256) lg(512) Searches 1 2 3 4 5 6 7 8 9 LB CS Notation = = = = = = = = =

  29. LB Recall log2 N = k • log10 N k = 0.30103... So: O(lg N) = O(log N)

  30. LB Comparing Data Structures and Methods Data Structure Traverse Search Unsorted L List N N Sorted L List N N Unsorted Array N N Sorted Array N Log N Binary Tree N N BST N N F&B BST N Log N

  31. Insertion • Inserting an element requires two steps: • Find the right location • Perform the instructions to insert • If the data structure in question is unsorted, then it is O(1) • Simply insert to the front • Simply insert to end in the case of an array • There is no work to find the right spot and only constant work to actually insert.

  32. LB Comparing Data Structures and Methods Data Structure Traverse Search Insert Unsorted L List N N 1 Sorted L List N N Unsorted Array N N 1 Sorted Array N Log N Binary Tree N N 1 BST N N F&B BST N Log N

  33. Insert into a Sorted Linked List Finding the right spot is O(N) • Recurse/iterate until found Performing the insertion is O(1) • 4-5 instructions Total work is O(N + 1) = O(N)

  34. Inserting into a Sorted Array Finding the right spot is O(Log N) • Binary search on the element to insert Performing the insertion • Shuffle the existing elements to make room for the new item

  35. 35 12 101 5 77 Shuffling Elements Note – we must have at least one empty cell Insert 29

  36. 12 5 Big-O of Shuffle Worst case: inserting the smallest number 101 35 77 Would require moving N elements… Thus shuffle is O(N)

  37. Big-O of Inserting into Sorted Array Finding the right spot is O(Log N) Performing the insertion (shuffle) is O(N) Sequential steps, so add: Total work is O(Log N + N) = O(N)

  38. LB Comparing Data Structures and Methods Data Structure Traverse Search Insert Unsorted L List N N 1 Sorted L List N N N Unsorted Array N N 1 Sorted Array N Log N N Binary Tree N N 1 BST N N F&B BST N Log N

  39. Inserting into a Full and Balanced BST Always insert when current = NIL. Find the right spot • Binary search on the element to insert Perform the insertion • 4-5 instructions to create & add node

  40. Full and Balanced BST Insert Add 4 12 41 3 98 2 7 35

  41. Full and Balanced BST Insert 12 41 3 98 2 7 35 4

  42. Big-O of Full & Balanced BST Insert Find the right spot is O(Log N) Performing the insertion is O(1) Sequential, so add: Total work is O(Log N + 1) = O(Log N)

  43. LB Comparing Data Structures and Methods Data Structure Traverse Search Insert Unsorted L List N N 1 Sorted L List N N N Unsorted Array N N 1 Sorted Array N Log N N Binary Tree N N 1 BST N N F&B BST N Log N Log N

  44. LB What about a BST Not full & balanced?

  45. LB Comparing Data Structures and Methods Data Structure Traverse Search Insert Unsorted L List N N 1 Sorted L List N N N Unsorted Array N N 1 Sorted Array N Log N N Binary Tree N N 1 BST N N N F&B BST N Log N Log N

  46. Two Sorting Algorithms • Bubblesort • Brute-force method of sorting • Loop inside of a loop • Mergesort • Divide and conquer approach • Recursively call, splitting in half • Merge sorted halves together

  47. Bubblesort Review Bubblesort works by comparing and swapping values in a list 1 2 3 4 5 6 101 12 42 35 5 77

  48. Bubblesort Review Bubblesort works by comparing and swapping values in a list 1 2 3 4 5 6 101 5 77 35 12 42 Largest value correctly placed

  49. procedure Bubblesort(A isoftype in/out Arr_Type) to_do, index isoftype Num to_do <- N – 1 loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1 endloop endprocedure // Bubblesort N-1 to_do

  50. Analysis of Bubblesort • How many comparisons in the inner loop? • to_do goes from N-1 down to 1, thus • (N-1) + (N-2) + (N-3) + ... + 2 + 1 • Average: N/2 for each “pass” of the outer loop. • How many “passes” of the outer loop? • N – 1

More Related