Presentation is loading. Please wait.

Presentation is loading. Please wait.

MSc/ICY Software Workshop , Semester 2

Similar presentations


Presentation on theme: "MSc/ICY Software Workshop , Semester 2"— Presentation transcript:

1 MSc/ICY Software Workshop 2016-17, Semester 2
Uday Reddy University of Birmingham

2 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

3 Organisation Assessment: Tutorial groups same as last semester
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

4 Class 1 - Recursion Recursion Fundamentals of recursion
Number-theoretic functions

5 A recursive method is one
Recursive methods An alternative way to do repetitive actions. More general than loops: A recursive method is one that calls itself.

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

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

8 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) }

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

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

11 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))) =

12 Java computation for sum
Java computation works the same way: sum(3) sum(2) sum(1) sum(0)

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

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

15 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);

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

17 Example Given n, print numbers 0, ..., n-1, n.

18 Example Given n, calculate n!, defined to be the nth number in the list 1, 1, 2, 6, 24, 120, … (counting from zero)

19 Example Given n >=0, print n asterisks. > java stars
Input number: 4 ****

20 Example Given n>=0, print a triangle out of asterisks.
> java triangle Input number: 4 **** *** ** *

21 Example Given integers m, n >= 0, compute mn

22 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

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

24 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:

25 Binomial coefficients (cont.)
Assume we can calculate for m’ <= m and/or n’ <= n. How does this help us calculate ? 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. ( m n )

26 Binomial coefficients (cont.)
Thus, binomial coefficient can be calculated recursively: = Base cases: = 1 = 0 ( m n ) ( m-1 n-1 ) ( m-1 n ) ( m ) ( n )


Download ppt "MSc/ICY Software Workshop , Semester 2"

Similar presentations


Ads by Google