Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science / Procedural – 67130 Exam Information Units 20-21.

Similar presentations


Presentation on theme: "Introduction to Computer Science / Procedural – 67130 Exam Information Units 20-21."— Presentation transcript:

1 Introduction to Computer Science / Procedural – 67130 Exam Information Units 20-21

2 21- 2 Internal Representation A computer word of 32 bits is “just” a string of 1’s and 0’s: 10011001111011010101010010110110 It might represent any one of a number of things:  4 characters  An integer  Half of a double (floating point number)  A computer command in machine language

3 21- 3 Internal Representation 4 characters 10011001111011010101010010110110 An integer 10011001111011010101010010110110 Half of a double 10011001111011010101010010110110 A computer command in machine language 10011001111011010101010010110110 The computer system itself must keep track of what the bits represent --- or allow a program to decide at runtime.

4 21- 4 Binary (half) Addition 0 plus 0 is 0 0 plus 1 is 1 1 plus 0 is 1 1 plus 1 is 0, carry a 1 to next column 1100 + 1 ------ 1101 Only good for first column.

5 21- 5 Binary (full) Addition, Handles Other Columns 0 plus 0 plus 0 is 0 0 plus 0 plus 1 is 1 0 plus 1 plus 0 is 1 0 plus 1 plus 1 is 0, carry a 1 to next column 1 plus 0 plus 0 is 1 1 plus 0 plus 1 is 0, carry a 1 to next column 1 plus 1 plus 0 is 0, carry a 1 to next column 1 plus 1 plus 1 is 1, carry a 1 to next column 11 + 11 ----- 110 These actions are carried out by circuits in the computer.

6 21- 6 0’s and 1’s What represents the 0’s and 1’s? It can be (and has been) many things… Voltages, magnetic fields of various kinds, physical relays that are open or closed, vacuum tubes, transistors in different states… Anything that can be in two well- differentiated states, and that can be easily/quickly switched between the states.

7 21- 7 What is Computation? E.g., Physical Computation

8 21- 8 Mathematically defined computation public class PrintPrimes { public static void main(String[ ] args) { for (j = 2; j < 1000; j++) if (isPrime(j)) System.out.println(j); } private static boolean isPrime(int n) { for (int j=2; j < n; j++) if (n%j == 0) return false; return true; }

9 21- 9 Turing Machines 0 0 1 1 a a b b z z 0, R, b 1, L, b 0, L, a 1, L, g 0, L, a 1, R, x a a b b z z e e

10 21- 10 Turing Machine for adding 1 a a b b 0 0 1 1 1, _, b 0, L, a 1, _, b 0, _, b start halt

11 21- 11 Seeing How It Works, Example 1 a a b b 0 0 1 1 1, _, b 0, L, a 1, _, b 0, _, b start halt 0011 …… … head Starts in state a, sees a 0… 0 1100 + 1 ------ 1101

12 21- 12 Seeing How It Works, Example 1 a a b b 0 0 1 1 1, _, b 0, L, a 1, _, b 0, _, b start halt 0111 …… … head Starts in state a, sees a 0, so writes a 1, stays in the same location, and goes to state b -- halt 0 1100 + 1 ------ 1101

13 21- 13 Seeing How It Works, Example 2 a a b b 0 0 1 1 1, _, b 0, L, a 1, _, b 0, _, b start halt 11010 …… … head Starts in state a, sees a 1… 1011 + 1 ------ 1100

14 21- 14 Seeing How It Works, Example 2 a a b b 0 0 1 1 1, _, b 0, L, a 1, _, b 0, _, b start halt 10010 …… … head Starts in state a, sees a 1, so writes a 0, moves left, and stays in state a… 1011 + 1 ------ 1100

15 21- 15 Seeing How It Works, Example 2 a a b b 0 0 1 1 1, _, b 0, L, a 1, _, b 0, _, b start halt 10010 …… … head Still in state a, and still sees a 1, so… 1011 + 1 ------ 1100

16 21- 16 Seeing How It Works, Example 2 a a b b 0 0 1 1 1, _, b 0, L, a 1, _, b 0, _, b start halt 00010 …… … head Still in state a, and still sees a 1, so write a 0, moves left, and stays in state a… 1011 + 1 ------ 1100

17 21- 17 Seeing How It Works, Example 2 a a b b 0 0 1 1 1, _, b 0, L, a 1, _, b 0, _, b start halt 00010 …… … head Still in state a, now sees a 0, so… 1011 + 1 ------ 1100

18 21- 18 Seeing How It Works, Example 2 a a b b 0 0 1 1 1, _, b 0, L, a 1, _, b 0, _, b start halt 00110 …… … head Still in state a, now sees a 0, so writes a 1, stays in same place, and moves to state b -- halt 1011 + 1 ------ 1100

19 21- 19 Universal Computers Theoretical version: there exists a single Universal Turing Machine that can simulate all other Turing machines  The input will be a coding of another machine + an input to the other machine Hardware version: stored program computer Software version: interpreter /** * Runs the first method in the Java class that is given in * “program” on the parameter given in “input”. Returns * the value returned by the method. The method must be * static, accept a single String parameter, and return a * String value. */ static String simulate(String program, String input){ … }

20 21- 20 Church-Turing Hypothesis “Every effective computation can be carried out by a Turing machine” “A method, or procedure, M, for achieving some desired result is called ‘effective’ or ‘mechanical’ just in case  M is set out in terms of a finite number of exact instructions (each instruction being expressed by means of a finite number of symbols);  M will, if carried out without error, produce the desired result in a finite number of steps;  M can (in practice or in principle) be carried out by a human being unaided by any machinery save paper and pencil;  M demands no insight or ingenuity on the part of the human being carrying it out.”

21 21- 21 Church-Turing Hypothesis This notion of an “effective method” is informal – the key requirement that the method “demands no insight or ingenuity” is left undefined Turing published a paper in 1936 that presented a formally exact predicate to replace the informal statement ‘can be calculated by means of an effective method’ Church did the same (in 1936!) “The replacement predicates that Turing and Church proposed were, on the face of it, very different from one another, but they turned out to be equivalent, in the sense that each picks out the same set of mathematical functions” “Whenever there is an effective method for obtaining the values of a mathematical function, the function can be computed by a Turing machine”

22 21- 22 The Church-Turing Hypothesis Everything computable is computable by a Turing machine  Everything computable is computable by a Java program  All “good enough” computers are equivalent ANY "COMPUTABLE" FUNCTION IS A PARTIAL RECURSIVE FUNCTION. ══ ANY "COMPUTABLE" FUNCTION HAS A TM TO COMPUTE IT Partial recursive functions are the only computable functions, and these are the functions computable by TMs.

23 21- 23 “Intrinsic Power” vs. “Effective Power” Anything you can do with a minimally capable computer language, you can theoretically do with any other minimally capable computer language (ignoring storage constraints) But that is like saying a shovel is theoretically as capable as a tractor. In practice, using a shovel might make things very hard…

24 21- 24 What to Concentrate On The exam will not ask any questions about the robot world material The exam assumes knowledge of everything else that you’ve seen in this course, including simple understanding of complexity Concentrate more on algorithms and data structures than on “system building”

25 21- 25 Good/Bad Questions Good question:  “Write an algorithm using recursion to solve the following problem”  “Write a method that solves the following problem, using a linked list” Bad question (not the kind we’ll ask):  “Develop a class hierarchy for representing employment information for Israel’s Mas Hachnasa office”

26 21- 26 Other Things to Use as Study Resources Besides the old exams, you can look at the book “Introduction to Computer Science using Java” in the library There are questions in every chapter, with some answers at the back of the book Practice answering these questions (obviously, ignore applet sections)

27 21- 27 Translating from Previous Tests Some previous tests had file operations (reading and writing) Assume that if we asked those kinds of questions, we’ll restrict ourselves to the input/output operations you’ve seen in the class SimpleInput, or provide you with others

28 21- 28 Test-Taking Hints Read through the instructions at the beginning of the exam – we mean them! Look at all the questions quickly at the beginning. You will choose 3 out of 4 to answer. Do not necessarily work on Question 1 first – choose an order that makes sense for you. Do not spend too much time on any question – time constraints are real (no extensions). But you do have an hour for each question; don’t expect the answer to be obvious right away.

29 21- 29 Sample Question 1 You have seen the QuickSort algorithm, which made use of the partition method: int partition(double[] a, int lo, int hi) { // Choose middle element among a[lo]…a[hi], // and move other elements so that a[lo]…a[m-1] // are all less than a[m] and a[m+1]…a[hi] are // all greater than a[m] // // m is returned to the caller swap(a, lo, medianLocation(a, lo+1, hi, (lo+hi)/2)); int m = shuffle(a, lo+1, hi, a[lo]); swap(a, lo, m); return m; }

30 21- 30 Sample Question 1 Write a method called “selection” that takes two arguments: an array of ints, a[ ], and an int, k, and returns what would be the k-th element of the sorted array a[ ] – but do it without completely sorting a[ ]! You may mutate a[ ] You should use the partition method, and solve the problem using recursion What are the big O worst-case and best- case complexities of your method?

31 21- 31 Solution public static int selection(int a[], int k) { return selection(a, k, 0, a.length-1); } private static int selection(int a[], int k, int lo, int hi) { int p = partition(a, lo, hi); // index of pivot if ( p == k ) return a[p]; else if ( p > k ) return selection(a, k, lo, p-1); else // p < k return selection(a, k, p+1, hi ); }

32 21- 32 Worst Case Complexity The recurrence relation will be: T(n) = T(n – 1) + O(n) This is when partition, unluckily, keeps returning the worst possible division of the array We know that recurrence relation results in the following complexity (it’s one of the big-5 recurrence relations): O(n 2 ) Best Case Complexity: one partition and you get lucky, so it’s O(n)

33 21- 33 Average Case Complexity This recurrence relation is kind of like QuickSort’s best case, but is missing the “2” that results from having to do both halves of a problem: T(n) = T(n/2) + O(n) = [T(n/4) + n/2] + n = T(n/4) + 3n/2 = [T(n/8) + n/4] + 3n/2 = T(n/8) + 7n/4 = T(n/2 k ) + (2 k – 1)n/2 k-1 [eureka!] = T(1) + 2(n – 1)n/n[log 2 n = k] = O(n)[T(1) is 1, etc.] This isn’t a proof, we’re just noticing a pattern…

34 21- 34 Sample Question 2 You are given an unsorted array of “Flat” objects, which have a method getNum( ) that returns a value between 1 and 4 Sort the array of objects in time O(n), calling getNum( ) no more than once on each object Do not create new heap objects 3 [0][1][2][3][4][5][6][7][8][9] 144242311

35 21- 35 Question 2 This was a moed bet question; many students did a “countsort”, which they had learned in the second semester “Run through the array, count how many times each object appears, shift objects around” This did not satisfy the requirement to call getNum( ) no more than once on each object A correct answer is a simple extension of the Dutch Flag problem solution

36 21- 36 public static void sortFlats (Flat[] flats) { int border1 = -1; int border2 = 0; int border3 = flats.length-1; int border4 = flats.length; do { switch ( flats[border2].getNum( ) ) { case 1: border1++; swap(flats, border1, border2); border2++; break; case 2: border2++; break; case 3: swap(flats, border2, border3); border3--; break; case 4: border4--; swap(flats, border2, border4); if (border3 < border 4) swap(flats, border2, border3); border3--; break; } } while border2 <= border3; } and write swap( )...

37 21- 37 Sample Question 3 Assume that you have an array, u, of n integers, where no integer appears more than once. u is said to be unimodal if there is some index i (where 0 ≤ i ≤ n-1) such that u[0],…,u[i] is a strictly increasing sequence and u[i],…,u[n-1] is a strictly decreasing sequence. For example, u = 2, 4, 8, 7, 6, 5, 4, 1, 0, -1 is unimodal, and u = 5, 3, 2, 1 is unimodal, but u = 2, 3, 1, 4 is not.

38 21- 38 Sample Question 3a Write a non-recursive method that receives an array of integers, returns the value of i if the array is unimodal, and returns -1 if the array is not unimodal. Make this method as efficient as you can. What are the big-O best-case and worst-case complexities of your method? Explain.

39 21- 39 /************************************************************** * Once a unimodal array starts decreasing, it doesn’t increase * @param data * Possibly unimodal array data[ ] * @return * The location of the array’s single peak, or -1 if the array is not unimodal * Best case: O(1) --- it can discover the first two values are equal * Worst case: O(n) --- it has to run through whole array to check if it's unimodal **************************************************************/ public static int unimodalCheck (int[ ] data) { int last = data.length - 1, maxLoc = 0; // location of peak boolean falling = false; for (int i = 0; i < last; i++) { if (data[i+1] == data[i]) return -1; if (data[i+1] > data[i]) { if (falling == true) return -1; else maxLoc = i+1; } else falling = true; // data[i] > data[i+1] } return maxLoc; // we get here, it's a unimodal array }

40 21- 40 Sample Question 3b Now write a non-recursive method that is guaranteed to receive a unimodal array of integers, and returns the value of i. Make this method as efficient as you can. What are the big-O best-case and worst-case complexities of your method? Explain.

41 21- 41 /************************************************************* * A unimodal binary search, checking for a complete peak at each jump. * There are no duplicated contiguous entries in the array, by the definition of * unimodal arrays --- and the array is guaranteed unimodal, we're just * looking for the peak * @param data * Guaranteed unimodal array data[ ] * @return * The location of the array’s single peak * Best case: O(1) --- might find peak after first calculation of middle * Worst case: O(log n) --- it might have to collapse the unknown region to a * single array location, before it finds the peak; but the collapsing is done * like binary search, so it cuts the unknown region in half each time *************************************************************/

42 21- 42 public static int unimodal (int[ ] data) { int middle, lower = 0, upper = (data.length - 1); do { middle = ((lower + upper) / 2); if (middle == 0) { if (upper == 0) return 0; // array of one element else if (data[middle] > data[middle+1]) return middle; else return (middle+1); } else { if (data[middle] > data[middle+1]) { if (data[middle] > data[middle-1]) return middle; else upper = middle-1; } else lower = middle+1; } } while (lower < upper); // we stop if lower and upper meet // if we get here, data[lower] is the array’s peak return lower; } // unimodal

43 21- 43 Sample Question 4 Assume that you have an array, u, of n integers, n>0, where the same integer may appear more than once in u Write 3 non-recursive methods that receive the array u as an argument and return the value of the integer that appears most often in it – each method designed to work under one of the 3 conditions below In all cases, if multiple integers appear with the same maximal frequency, your method can return any one of them

44 21- 44 Sample Question 4a The array u is not necessarily sorted. In your solution, do not create any additional objects in the heap, and do not sort the array! What are the big-O best-case and worst-case complexities of your method? Explain.

45 21- 45 Answer to Question 4a public static int common1 (int[ ] data) { intcurFreq = 0, maxFreq = 0, maxInt = 0, size = data.length; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (data[j] == data[i]) curFreq++; } if (curFreq > maxFreq) { maxFreq = curFreq; maxInt = data[i]; } curFreq = 0; // reset the current // frequency for next int } return maxInt; } // Best and worst case running time are O(n 2 )

46 21- 46 Sample Question 4b Now assume that the you have access to a method public void qsort(int[ ] data) that sorts an array in ascending order using the QuickSort algorithm. You may use qsort( ) in your solution. What are the big-O best-case and worst-case complexities of your method? Explain.

47 21- 47 Answer to Question 4b public static int common2 (int[ ] data) { int curFreq = 1, maxFreq = 1, maxInt = data[0], size = data.length; qsort(data); for (int i = 1; i < size; i++) { if (data[i] == data[i-1]) curFreq++; else { // we have a new integer if (curFreq > maxFreq) { maxFreq = curFreq; maxInt = data[i-1]; } curFreq = 1; } // need to check if last integer in array is most frequent if (curFreq > maxFreq) maxInt = data[size-1]; return maxInt; } // Best case is O(n log n) + O(n), i.e., O(n log n) // Also because of qsort( ), the worst case is O(n 2 )

48 21- 48 Sample Question 4c Now assume the array, u, is unsorted, but the only integers appearing in it are in the range 0 to 99. What are the big-O best-case and worst-case complexities of your method? Explain.

49 21- 49 Answer to Question 4c public static int common3 (int[ ] data) { final int RANGE = 100; intmaxLoc = 0, maxNum = 0, size = data.length; int[] counts = new int[RANGE]; for (int i = 0; i < size; i++) counts[data[i]]++; for (int i = 0; i < RANGE; i++) { if (counts[i] > maxNum) { maxNum = counts[i]; maxLoc = i; } return maxLoc; } // Best and worst case are O(n)

50 21- 50 Question 5 x and y are two linked lists, with a standard Node definition (e.g., a field called ‘next’) They are “joint” if they meet at some point, such as the following example: How would you find the node at which the two lists meet?

51 21- 51 Answer to Question 5 Let n1 be the size of l1 (list1) and n2 be the size of l2 (list2) Assume that n1 >= n2 Let d = n1 - n2 (i.e., d >= 0) Let l1b be a sublist of l1 which begins in the d-th element of l1 (e.g., if d=0 then l1=l1b ; if d=1 than the head of l1b is the 2 nd element in l1, etc.) Now, l1b and l2 are lists of the same size.

52 21- 52 while ( l1b != l2 ) { // since l1b and l2 are of the same size then // either the two will become null at the same // time (which means they are disjoint), or will // become equal before both turn into null // (which means they are joint) l1b = l1b.next; l2 = l2.next; } if ( l1 == null ) { // lists are disjoint assert l2 == null; } else { // l1 is the merge-point assert l1 == l2; } Verify whether two lists of the same size are joint using a simple O(n) loop:

53 21- 53 The Brain vs. a Pentium There are 10^9 -- 10^10 neurons in the brain Each neuron receives input from 10^3 -- 10^4 synapses Each neuron can fire about 1 -- 10^2 times/sec Total computing power is 10^9 -- 10^16 ops/sec Pentium IV computing power ≈ 10^9 ops/sec

54 21- 54 Artificial Intelligence Why can’t we program computers to do what humans easily do?  Recognize faces  Understand human language (Ironically, we can program computers to do what humans cannot easily do  Play chess at world champion levels  Carry out massive optimization problems) Processing power? --- doesn’t seem to be the real issue Software?  Scruffy vs. Neat debate

55 21- 55 Artificial Intelligence: Scruffy vs. Neat The Scruffy approach says, “Build systems that work, and principles will emerge.”  E.g., the Wright Brothers building a heavier-than-air flying machine The Neat approach says, “Explore principles first, and having understood them, embody them in systems.”  E.g., radar

56 21- 56 The Turing Test

57 21- 57 Self-Reference Leads to Paradoxes Example: Things in the world can be classified according to what sets they belong to  “The set of all tables” “the set of all chairs” … … … …

58 21- 58 Self-Reference Leads to Paradoxes Sets themselves can be grouped into sets “The set of all sets having more than three members”  That set would have “the set of all chairs” inside of it The set of all sets having more than three members … …

59 21- 59 Self-Reference Leads to Paradoxes Some sets are even contained in themselves! “The set of all sets having more than three members” contains itself (since there are more than three such sets…) Some sets do not contain themselves, e.g., “the set of all chairs” does not contain itself: … … The set of all sets having more than three members

60 21- 60 Self-Reference Leads to Paradoxes We could even define “The set of all sets that do not contain themselves”  That set would contain “the set of all chairs”, for example The set of all sets that do not contain themselves

61 21- 61 Self-Reference Leads to Paradoxes Does “The set of all sets that do not contain themselves” contain itself?  If it contains itself, then it can’t contain itself (by definition)  If it doesn’t contain itself, then it contains itself (by definition) Paradox! The set of all sets that do not contain themselves

62 21- 62 The Halting problem program must be a Java class. The method that is run is the first one in the class and must be of the form static boolean XXXX(String input) /** * Return true if the program finishes * running when given the input as its * parameter. Return false if it gets into * an infinite loop. */ static boolean halt(String program, String input){ // fill in – can you do it? }

63 21- 63 Diagonalization static boolean autoHalt(String program){ return halt(program, program); } static boolean paradox(String program){ if autoHalt(program) while(true) ; // do nothing else return true; }

64 21- 64 The Halting problem cannot be solved Will paradox halt when running on its own code?  Yes ==> autoHalt(paradoxString) returns false ==> No  No ==> autoHalt(paradoxString) returns true ==> Yes Contradiction. Hence the assumption that halt(program, input) exists is wrong Theorem: the halt method cannot be written String paradoxString = “class Test {\n” + “ static boolean paradox(String program){\n” + … “ static boolean halt(String program, String input){\n” + … boolean answer = Test.paradox(paradoxString);

65 21- 65 There are Limits to Computation There are theoretical limits to what can be computed In practice, we are nowhere near the limits of what we can do with computers The most interesting question remains “What new things can we do with computer power?” In this course, you have started the journey of understanding in Computer Science, so that you can help answer that question

66 21- 66 Areas in Computer Science Algorithms and Data Structures Programming Languages Computer Architecture Operating Systems Software Engineering Symbolic and numerical computation and modeling, graphics Databases Artificial Intelligence Robotics Computer Vision

67 21- 67 How they fit together ArchitectureLanguages Software Engineering Algorithms Operating Systems Robotics Databases Artificial Intelligence Symbolic/Numerical Computation

68 21- 68 This is Not the End We have covered a lot of subjects in Computer Science (e.g., sorting, searching, recursion, simple data structures like arrays and linked lists) We have covered a lot in Java (e.g., variables, types, conditionals, loops, objects, inheritance, polymorphism) But it’s only the beginning You’ll be carrying this forward in the next semester in Data Structures

69 21- 69 The End, for Now Good Luck on the Exam!


Download ppt "Introduction to Computer Science / Procedural – 67130 Exam Information Units 20-21."

Similar presentations


Ads by Google