Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
MSc/ICY Software Workshop 2016-17, Semester 2 Uday Reddy University of Birmingham 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 1 Semester 2 Outline Recursion and Recursive data structures Lists, trees, binary search trees Mutable data structures Java Collections library Java type system and generic classes Graphical User Interfaces (GUI’s) Concurrent Threads & Network Sockets Software Engineering methods 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 2 Organisation Assessment: 4 Lab exercises (1-2 weeks each, part of 20%) 2 short class tests (quizzes, part of 20%) Group project (5 weeks, 10%) Tutorial groups same as last semester Tue, 1-2pm, 2-3pm 21 Lectures (3 per week for 7 weeks: No lectures after Week 8 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 3 Class 1 - Recursion Recursion Fundamentals of recursion Number-theoretic functions 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 4 Recursive methods An alternative way to do repetitive actions. More general than loops: A recursive method is one that calls itself. 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 5 Why study recursion? Used only occasionally in programming, but: Mathematically useful - related to principle of induction Used to specify programs (rather than write them). Represents basic principle of computation. It is the most general method of repetitive computation. Introduces functional programming style. 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 6 What basic principle? All computation is, in some sense, performed in one of two ways: Directly (e.g. addition) By solving a “simpler” version of the computation and using this solution to get the desired answer. Note that solving and using are mixed up. 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 7 Recursive methods in Java Recursive method f with argument x has the form static typename f (typename x) { if (x is simple enough) solve directly else use f(value “smaller than” x) to calculate f(x) } 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 8 Recursion on integers On integers, this becomes static int f (int x) { if (x is a small enough number) return expression using x; else { int y = f (number less than x); return expression using x and y; } } 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 9 Recursion example Given n, calculate sum n+(n-1)+ ... +1 (for n 0) static int sum (int n) { if (n == 0) return 0; else { int y = sum (n-1); return n+y; } } 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 10 Computing using sum The sum method, in mathematical style: S(0) = 0 S(n) = n + S(n-1), if n 0 Example computation S(3) = 3 + S(2) = 3 + (2 + S(1)) = 3 + (2 + (1 + S(0))) =3+2+1+0 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 11 Java computation for sum Java computation works the same way: sum(3) 6 2016-17 sum(2) 3 sum(1) 1 MSc Workshop © Kamin & Reddy sum(0) 0 1A - Recursion - 12 Recursion example Define f(i) to be the ith number in the sequence 5, 8, 11, 14, 17, … (counting from zero) static int f (int i) { if (i == 0) return 5; else { int y = f(i-1); return y+3; } } 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 13 Recursion on integers Methods returning void can also be defined by recursion. Basic principle: use computation on smaller values to perform computation on larger values. 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 14 Recursion example Given n, print numbers n, n-1, ..., 0 static void printnums (int n) { if (n == 0) { System.out.print(0); } else { System.out.print(n); printnums(n-1); } } 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 15 Thinking recursively Key ideas: Know exactly what the method is supposed to do. Assume the method works for smaller values. Then just figure out how to use it to compute the method for larger values. Don’t forget base cases - small values that can be calculated directly. 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 16 Example Given n, print numbers 0, ..., n-1, n. 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 17 Example Given n, calculate n!, defined to be the nth number in the list 1, 1, 2, 6, 24, 120, … (counting from zero) 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 18 Example Given n >=0, print n asterisks. > java stars Input number: 4 **** 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 19 Example Given n>=0, print a triangle out of asterisks. > java triangle Input number: 4 **** *** ** * 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 20 Example Given integers m, n >= 0, compute mn 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 21 Example Given integers m, n >= 0, compute mn more efficiently, using this idea: mn = 1, if n=0 (mn/2)2, if n even mmn-1, if n odd 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 22 Why more efficient? Count multiplications: pow(m,n): n-1 multiplications fastpow(m,n): log2 n multiplications One advantage of recursion is that it sometimes suggests much more efficient algorithms. 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 23 Binomial coefficients m n ( ) (“m choose n”) = number of ways of selecting n items from m>=n objects (disregarding order). E.g. ( 4 ) 2 = 6: 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 24 Binomial coefficients (cont.) m’ n’ Assume we can calculate ( ) for m’ <= m and/or n’ <= n. How does this help us m calculate ( n ) ? Select element e out of the m elements. To choose the n elements, either: Include e: choose n-1 out of remaining m-1 objects; or Exclude e: choose n out of remaining m-1 objects. 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 25 Binomial coefficients (cont.) Thus, binomial coefficient can be calculated recursively: m m-1 m-1 ( n ) = ( n-1 ) + ( n ) Base cases: m ( 0 )= 1 0 ( n )= 0 2016-17 MSc Workshop © Kamin & Reddy 1A - Recursion - 26