1 / 16

Computability

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 <

mili
Download Presentation

Computability

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. Computability O(n) exercises. Searching. Shuffling Homework: review examples. Research other shuffling.

  2. 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.

  3. O(n) and o(n) homework • Prove or disprove: 100*n = O(n) n3 = O(n) 100*n = o(n) en = o(3n)

  4. 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?

  5. 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?

  6. 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

  7. 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; }

  8. 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; }

  9. 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

  10. 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. • nn/2n/4… If n = 2m , there would be m steps • Steps (compares) are bounded by log2(n)

  11. 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

  12. 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.

  13. 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.

  14. 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; }

  15. 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.

  16. 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?

More Related