160 likes | 296 Views
Computability. O(n) exercises. Searching. Shuffling Homework: review examples. Research other shuffling. Recall . O() notation has <= A function f(n) is of order Big Oh g(n) if there exists a constant c and a number n 0 such that f(n) <= g(n) if n>= n o() notation has strictly <
E N D
Computability O(n) exercises. Searching. Shuffling Homework: review examples. Research other shuffling.
Recall • O() notation has <= • A function f(n) is of order Big Oh g(n) if there exists a constant c and a number n0 such that f(n) <= g(n) if n>= n • o() notation has strictly < • A function f(n) is of order little oh g(n) if there exists a constant c and a number n0 such that f(n) < g(n) if n>= n. This is equivalent to limit f(n)/g(n) going to 0 as n goes to infinity.
O(n) and o(n) homework • Prove or disprove: 100*n = O(n) n3 = O(n) 100*n = o(n) en = o(3n)
Searching • Given an array of n items, with keys numbers, arranged in order by keys, what is a method for finding the item with key k? • NOTE: assumption that array is in order! • How do you look up a key? • How do you look up a name in a list?
Linear search • check the first one, then the second, etc. • This COULD take n steps. • Probably claim that on average, it takes n/2 steps. • So, linear search is O(n). • Average n/2 is still O(n). Remember bounds are including a coefficient. • Can we do better?
Binary search • Idea is to do a test that will halve the search space • best strategy if the answers are equally likely. • Compare value to key of item in middle of array • if equal, done • if less, search lower part • if greater, search upper part
iterative binary search function binsearch(arr,value,low,high) { while (low<=high) { var mid:int; mid = (low+high) / 2; // will take floor if (arr[mid]>value) { high = mid-1; } else if (arr[mid]<value) {low = mid+1;} else {return mid;} } return -1; }
recursive binary search function binsearch(arr,value,low, high) { var mid:int; if (low>high) return -1; mid = (low+high)/2; if (arr[mid]>value) return binsearch(arr,value,low,mid-1) else if (arr[mid]<value) return binsearch(arr,value,mid+1,high); else return mid; }
Costs of recursion • In any programming language, a recursive function call, or any function call, takes some steps • set up the call, environment, place block of data on a stack • return: pops the stack: replaces the block of data with a value
Analysis • Focus on number of compare steps. • recursive versus iteration could be significant, but it is a constant factor in terms of problem size. • Problem size [at least] halved each time. • nn/2n/4… If n = 2m , there would be m steps • Steps (compares) are bounded by log2(n)
Geography game • Three categories • man-made • natural • political • Ask true-false questions • Strategy: reduce the [remaining] search space along various/multiple dimensions. • Bifurcate the search space
Shuffling • How to shuffle a set of n items? • Definition: a good shuffle is one in which any element in the original array could end up in any position in the shuffled array with equal probability.
Fisher-Yates-Knuth • Look at end of the array. Pick slot randomly from the rest of the array. Swap. • Shrink array down 1 position. Repeat: that is, pick slot randomly from the rest of the array. Swap with element at top-1 position. • Continue.
Fisher Yates Knuth function fyk(arr) { i=arr.length – 1; while(i>0) { s = Math.floor(Math.random()*(i+1)); // random int 0 to i swap(arr,s,i); i--; } } function swap(arr,a,b) { hold = arr[a]; arr[a] = arr[b]; arr[b] = hold; }
Complexity of FYK shuffle • Number of steps = n • O(n) • Note also done in place • use one extra hold value, but space n versus space n+1 is considered the same. • Constants and coefficents do not matter. • Note: assumes Math.random takes a constant amount of time.
Homework • Research on-line and report more information on Fisher-Yates-Knuth • order of compares, average, worst case • Other ways of shuffling cards? • 7 is enough (Kruskal). What is the measure of goodness? • Other ways to search? • Search other spaces?