MSc/ICY Software Workshop , Semester 2

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Advertisements

Lecture 12 Oct 13, 2008 Some simple recursive programs recursive problem solving, connection to induction Some examples involving recursion Announcements.
ICS103 Programming in C Lecture 11: Recursive Functions
Fundamental in Computer Science Recursive algorithms 1.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Data Structures Mohamed Mustaq Ahmed Chapter 2- Algorithms.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
2-1 2 Algorithms Principles. Efficiency. Complexity. O-notation. Recursion. © 2001, D.A. Watt and D.F. Brown.
Lecture#16 Discrete Mathematics. Recursion Now, 1 is an odd positive integer by the definition base. With k = 1, = 3, so 3 is an odd positive integer.
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
Recursion Chapter 11. How it works “A recursive computation solves a problem by using the solution of the same problem with simpler inputs” Big Java,
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
R ECURRSION Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter.
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 1 Class 5 - Lists r The list data type r Recursive methods on lists.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Ms N Nashandi Dr SH Nggada Week 08 – Recursion. Outline We shall be covering the following What is a recursion? Iteration versus Recursion. Recursion.
Recursion.
Algorithm Analysis 1.
CS314 – Section 5 Recitation 10
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion CSE 2320 – Algorithms and Data Structures
Class 2 – Recursion on Lists
Recursion Topic 5.
Lecture 9: introduction to recursion reading: 12.1
EECE 310: Software Engineering
Building Java Programs
Recursion DRILL: Please take out your notes on Recursion
Decrease-and-Conquer Approach
Towers of Hanoi Move n (4) disks from pole A to pole C
A lightening tour in 45 minutes
Programming in Java: lecture 10
Recursion CSE 2320 – Algorithms and Data Structures
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
slides adapted from Marty Stepp and Hélène Martin
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Programming application CC213
Unit-2 Divide and Conquer
This Lecture Substitution model
slides created by Marty Stepp and Alyssa Harding
24 Searching and Sorting.
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Java Programming: Chapter 9: Recursion Second Edition
And now for something completely different . . .
This Lecture Substitution model
slides adapted from Marty Stepp and Hélène Martin
Programming with Recursion
This Lecture Substitution model
ICS103 Programming in C Lecture 11: Recursive Functions
slides created by Marty Stepp
Building Java Programs
Recursion Chapter 12.
ITEC324 Principle of CS III
Recursive Thinking.
Algorithms and data structures: basic definitions
Lecture 6 - Recursion.
Presentation transcript:

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

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

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

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

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.

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.

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.

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

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

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

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

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

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

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.

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

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.

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

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

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

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

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

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

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.

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:

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 )

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 )