1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

Starting Out with Java: From Control Structures through Objects
Factorial Recursion stack Binary Search Towers of Hanoi
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain.
FIT FIT1002 Computer Programming Unit 20 Recursion.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 5.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 CS 201 Data Structures & Algorithms Chapter 1 Introduction Text: Read Weiss, §1.1 – 1.3 Izmir University of Economics.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Recursion.
CMPE13 Cyrus Bazeghi Chapter 17 Recursion. CMPE13 What is Recursion? A recursive function is one that solves its task by calling itself on smaller pieces.
1 CE 221 Data Structures & Algorithms Chapter 1 Introduction Text: Read Weiss, §1.1 – 1.3 Izmir University of Economics.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Recursion CMPE231, Spring 2012 Aleaxander G. Chefranov 1.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
CS212: Data Structures and Algorithms
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
CS1120: Recursion.
7.Recursion Recursion is the name given for expression anything in terms of itself. Recursive function is a function which calls itself until a particular.
Chapter 17 Recursion.
CSC 143 Recursion.
ITEC324 Principle of CS III
Recursive Thinking.
Presentation transcript:

1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms for problem solving (very useful in handling large amount of data) -algorithm analysis

2 Chapter 1: Introduction COMP 1220/1170 Revision  Basic Programming Concept  Type, Operators and Expressions  Control Flow -if else, while, do while, for, switch..... etc  Functions and Program Structure -advantages; -declare, define, invoke a function; -return value, call by value, call by address (reference);

3 Chapter 1: Introduction COMP 1220/1170 Revision con’t  Arrays and Pointers –group of related variables; –character strings – searching/matching/validation; –pointers and functions argument;  Structures –basic of structures, user define type; –pointers to structures, self referential structure; –linked list;  Storage Class and Type Qualifiers  Input and Output

4 Chapter 1: Introduction Problem: Given a group of N numbers, determine the kth largest, where N > k. Solution 1: –(1) read N number into an array, (2) sort the array in decreasing order, (3) return the element in position k. Solution 2: –(1) read the first k elements into an array and sort them in decreasing order, (2) each remaining element is read one by one, (2.1) it is ignored if it is smaller than the kth element in the array (2.2) it is placed in its correct spot in the array, bumping one element out of the array. (3) the element in the kth position is returned as the answer

5 Chapter 1: Introduction Which solution is better when (1) N ~ k and (2) N » k ? why?

6 1.2: Mathematics Review Basic formulas for derive and reviews basic proof techniques Exponents

7 1.2: Mathematics Review Logarithms All log are to be base 2 unless specified otherwise. Definition:

8 1.2: Mathematics Review Series: Geometric seriesDerivation Let S = 1+A+A 2 +…… (1) where, 0<A<1 then AS = A+A 2 +A 3 +…(2) Subtracting (1) and (2), we get S-AS = 1, i.e.

9 1.2: Mathematics Review Series: Arithmetic seriesExample: To find the sum ….+ (3k-1) = 3(1+2+3+…+k) - (1+1+1+….+1)

10 1.2: Mathematics Review The P word - to proof a false statement: proof by counter example - to proof a correct statement - proof by induction (1) proving a base case (2) inductive hypothesis - proof by contradiction (1) assume it is false (2) show that this assumption is false

Recursion What is recursion? - self-reference - recursive function: based upon itself e.g. n factorial:

Recursion Characteristics of a recursive definition - It has a stopping point (base case). -It (recursively) allows evaluation of an expression involving a variable n from a higher value to a lower value of n. Just like the process in finding a vocabulary from a dictionary

Recursion Recursive process Recursive definition defines an object in terms of a simpler case of itself. Iterative process Iterative evaluation calls for explicit repetition of similar computations. Such computations can generally be carried out in a for loop operation.

Recursion : n factorial Recursive processIterative process

Recursion : n factorial

Recursion: Fibonacci numbers Using recursion fib (int n) { int x, y; if (n <= 1) return (n); else { x = fib (n - 1); y = fib (n - 2); return (x + y); } Using iterative if (n <= 1) return (n); lofib = 0; hifib = 1; for (i=2; i<=n; i++) { x = lofib; lofib = hifib; hifib = x + lofib; } return (hifib);

Recursion:Binary Search Using recursion int binsrch (int a[], int x, int low, int high) { int mid; if (low > high) return (-1); else { mid = (low + high) / 2; return (x == a [mid] ? mid : x < a [mid] ? binsrch (a, x, low, mid-1) : binsrch (a, x, mid+1, high): ); } Using iterative while (low <= high) {mid = (low + high) / 2; if (x == a [mid ]) return (mid); else if (x < a [mid]) high = mid - 1; else low = mid + 1; }

Recursion:Binary Search

Recursion Questions: -When should we use recursion? -When should we use simple for loop?

Recursion: Tower of Hanoi Problem: constraints: (1) only one disk can be moved at a time, and (2) at no time may a disk be placed on top of a smaller disk.

Recursion: Tower of Hanoi Take n = 3

Recursion: Tower of Hanoi Solution -By making use of an auxiliary peg, move a stack of disks from one peg to another, making sure that a larger disk is always below a smaller disk at any time -Strategy If n = 1, move the single disk from A to C and stop; Otherwise move the top n-1 disks from A to B, using C as auxiliary, move the remaining disk from A to C, move the n-1 disks from B to C, using A as auxiliary.

Recursion: Tower of Hanoi move disk 1 from peg Ato pegB move disk2from peg Ato pegC move disk1from pegBto pegC move disk3from pegAto pegB move disk1from pegCto pegA move disk2from pegCto pegB move disk1from pegAto pegB move disk4from pegAto pegC move disk1from pegBto pegC move disk2from pegBto pegA move disk1from pegCto pegA move disk3from pegBto pegC move disk1from pegAto pegB move disk2from pegAto pegC move disk1from pegBto pegC n = 4

Recursion: Tower of Hanoi towers (int n, char frompeg, char topeg, char auxpeg) { /* If only one disk, make the move and return */ if (n == 1) printf (“\n%s%c%s%c”, “move disk 1 from peg “, frompeg, “ to peg “, topeg); else { /* move top n-1 disks from A to B, C as auxiliary*/ towers (n-1, frompeg, auxpeg, topeg); /* move remaining disk from A to C */ printf (“n%s%d%s%c%s%c”, “move disk “, n,“ from peg “, frompeg, “ to peg “, topeg); /* move n-1 disks from B to C, A as auxiliary */ towers (n-1, auxpeg, topeg, frompeg); }

Recursion 4 Basic Rules of Recursion - Base cases - Making progress - Design rule - Compound interest rule