960 likes | 971 Views
Learn about the analysis of algorithms and data structures using O() notation. Explore different methods and their performance on various data structures. Available online.
E N D
Lecture 24 O() Analysis of Methods and Data Structures Reasonable vs. UnreasonableAlgorithms Using O() Analysis in Design
Now Available Online! http://www.coursesurvey.gatech.edu
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!
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
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.
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.
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
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
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
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 ?
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.
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
Example: Linked List • Let’s determine if the value 83 is in the collection: Head \\ 42 5 19 35 83 Not Found!
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
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 ???
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
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).
Binary Search • We may perform binary search on • Sorted arrays • Full and balanced binary search trees • Tosses out ½ the elements at each comparison.
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.
7 12 42 59 71 86 104 212 Binary Search Example Looking for 89
7 12 42 59 71 86 104 212 Binary Search Example Looking for 89
7 12 42 59 71 86 104 212 Binary Search Example Looking for 89
7 12 42 59 71 86 104 212 Binary Search Example 89 not found – 3 comparisons3 = Log(8)
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).
N 2 4 8 16 32 64 128 256 512 Searches 1 2 3 4 5 6 7 8 9 LB What?
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? = = = = = = = = =
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 = = = = = = = = =
LB Recall log2 N = k • log10 N k = 0.30103... So: O(lg N) = O(log N)
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
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.
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
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)
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 12 101 5 77 Shuffling Elements Note – we must have at least one empty cell Insert 29
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)
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)
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
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
Full and Balanced BST Insert Add 4 12 41 3 98 2 7 35
Full and Balanced BST Insert 12 41 3 98 2 7 35 4
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)
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
LB What about a BST Not full & balanced?
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
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
Bubblesort Review Bubblesort works by comparing and swapping values in a list 1 2 3 4 5 6 101 12 42 35 5 77
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
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
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