IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk - 1 - Sissejuhatus informaatikasse.

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Advertisements

Introduction to Recursion and Recursive Algorithms
Recursion.
Recursion. Binary search example postponed to end of lecture.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
Recursion. Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn about recursive algorithms Lecture.
More on Recursive Recursion vs. Iteration Why Recursion?
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.
Recursion.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 13 Recursion.
ISOM MIS 215 Module 4 – Recursion. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Recursion. 2 Overview  Learn about recursive definitions  Explore the base case and the general case of a recursive definition  Discover recursive.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
RECURSION Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Abdulmotaleb El Saddik University of Ottawa
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
More on Recursive Recursion vs. Iteration Why Recursion?
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Chapter 8: Recursion Java Software Solutions
Java Programming: Program Design Including Data Structures
Recursive Binary Search
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Recursion Chapter 11.
Chapter 8: Recursion Java Software Solutions
Recursion Data Structures.
Sissejuhatus informaatikasse
Chapter 11 Recursion.
Recursion When performing a repetitive task either: a loop recursion
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
CSC 143 Recursion.
The structure of programming
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Thinking procedurally
Java Software Solutions Foundations of Program Design Sixth Edition
Presentation transcript:

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Sissejuhatus informaatikasse

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Factorial: trace A subroutine is said to be recursive if it calls itself, either directly or indirectly. That is, the subroutine is used in its own definition. fact(3) ==> 3 * fact(3 - 1) ==> 3 * fact(2) ==> 3 * (2 * fact(2 - 1)) ==> 3 * (2 * fact(1)) ==> 3 * (2 * (1 * fact(1 - 1))) ==> 3 * (2 * (1 * fact(0))) ==> 3 * (2 * (1 * 1)) ==> 6

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Recursive objects in the world. Fractal objects A huge amount of of things in the real world has a recursive nature. For example, coastline of a sea has a fractal nature – fractals are recursive: their structure is repeated over and over in small details. i.e it is a rough or fragmented geometric shape that can be subdivided in parts, each of which is (at least approximately) a reduced-size copy of the whole Sierpinski triangle:

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Recursion A subroutine is said to be recursive if it calls itself, either directly or indirectly. That is, the subroutine is used in its own definition. Bad kind - infinite loop: a car is a car, int fact(int x) { return fact(x) } Recursion can often be used to solve complex problems by reducing them to simpler problems of the same type. Good kind – loop is (hopefully) terminated: An "ancestor" is either a parent or an ancestor of a parent. int fact (int x) { if (x <= 0) return 1; else return x * fact(x-1); }

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Recursive functions: main principles Important to check that recursion terminates. Code should contain: One or more base cases (no recursion involved!) One or more recursive cases. Arguments of the recursive call must be “simpler” according to some measure. NB! The “simplicity” measure may be arbitrarily complex.

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Recursive functions: main principles Recursion has same power as iteration: Every recursive function can be written using while or for loops instead Every function using while and/or for loops can be written using recursion instead However: some programming tasks are much easier to write using recursion some programming tasks are much easier to write using iteration

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Direct and indirect recursive call A recursive subroutine is one that calls itself, either directly or indirectly. To say that a subroutine calls itself directly means that its definition contains a subroutine call statement that calls the subroutine that is being defined. foo calls foo: int foo(int x) { if (x>0) return 1+foo(x-1) else return 1} To say that a subroutine calls itself indirectly means that it calls a second subroutine which in turn calls the first subroutine (either directly or indirectly). foo calls bar which calls foo: int foo(int x) { if (x>0) return 2+bar(x-2) else return 1} int bar(int x) { if (x>0) return 2*+foo(x-1) else return 1}

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Example: towers of Hanoi Goal: move all the disks from Stack 0 to Stack 1 Stack 2 can be used as a spare location. Only one disk at a time may be moved A larger disk may never be on the smaller disk

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk towers of Hanoi Move two elements So we have an algorithm to move two elements

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk towers of Hanoi Move three elements Use al algorithm to move two elements: notice evrywhere either a base or a bigger element Move the biggest Use al algorithm to move two elements: notice everywhere again either a base or a bigger element So we have an algorithm to move three elements

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk towers of Hanoi Move n elements Use al algorithm to move n-1 elements: notice evrywhere either a base or a bigger element Move the biggest Use al algorithm to move n-1 elements: notice everywhere again either a base or a bigger element So we have an algorithm to move n elements

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Recursive binary search static int binarySearch(int[] A, int loIndex, int hiIndex, int value) { if (loIndex > hiIndex) { return -1; } else { int middle = (loIndex + hiIndex) / 2; if (value == A[middle]) return middle; else if (value < A[middle]) return binarySearch(A, loIndex, middle - 1, value); else // value must be > A[middle] return binarySearch(A, middle + 1, hiIndex, value); } } // end binarySearch()

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Binary search Look for 15 1, 2, 5, 8, 13, 15, 23, 25, 45 1, 2, 5, 8, 13, 15, 23, 25, 45: index: [lowindex(1)+highindex(9)]/2=10/2=5 15>13: start binary_search(search:15, lowindex: 6 [5+1], higindex: 9) 1, 2, 5, 8, 13, 15, 23, 25, 45: index: [lowindex(6)+highindex(9)]/2=15/2=7.5=>7 15<23: start binary_search(search:15, lowindex: 6, highindex: 6 [7-1]) 1, 2, 5, 8, 13, 15, 23, 25, 45: index: [lowindex(6)+highindex(6)]/2=12/2=6 The element is found in 3 steps

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Deklaratiivne vs imperatiivne Programmeerimiskeeli ja -meetodeid saab klassifitseerida mitmel moel. Selle loengu kontekstis sobib jaotada programmeerimiskeeled kõigepealt kahte gruppi: Imperatiivsed keeled - sobivad samm-sammult, kindlas järjekorras täidetavate algoritmide esitamiseks. Programmid kujutavad endast arvutile antavate käskude jada. Tuntumad imperatiivsed keeled on C, Basic, Pascal, Java, objektorienteeritud keeled ja assemblerkeeled. Procedural programmingProcedural programming is imperative programming in which the program is built from one or more procedures (also known as subroutines or functions). subroutines Imperatiivsete keelte peamiseks eeliseks on arvuti tegevuse täpse kontrollimise ja suunamise võimaldamine, mis enamasti tagab maksimaalse töökiiruse. Miinusteks on programmeerimise suur töömahukus - lahenduskäigu kõik detailid tuleb süsteemile esitada - ning suured raskused programmideanalüüsimisel, näiteks optimeerimise, verifitseerimise või paralleliseerimise tarvis.

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Deklaratiivsed vs imperatiivsed keeled Deklaratiivsed keeled sobivad algoritmi esitamiseks käskude jadast abstraktsemal viisil. Programmeerija ei pruugi alati kõiki algoritmi detaile kirja panna, vaid võib esitada otsitava lahenduse kirjelduse, ning juba programmi täitmise käigus otsustab süsteem automaatselt, mis viisil täpselt seda lahendust otsida. Deklaratiivseteks keelteks võib lugeda loogilise programmeerimise keeled (näiteks Prolog) ja mitmed funktsionaalsed keeled (näiteks Haskell). Teoorias kasutatav lambda-arvutus on puhtalt funktsionaalse deklaratiivse keele näide. Common declarative languages include those of database query languages (e.g., SQL, XQuery), regular expressions, and mentioned abovedatabase query languagesSQLXQueryregular expressions

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Plussid ja miinused Deklaratiivsed keeled võimaldavad enamikku programme kiiremini ja mugavamalt kirjutada, kui imperatiivsed keeled - programmeerija ei pea kõigi detailide eest hoolt kandma. Tunduvalt lihtsam on ka programmide analüüs, näiteks programmi automaatsel kohandamisel paralleelarvutile, kus programmi täitmise juures töötab samaaegselt hulk protsessoreid. Peamiseks miinuseks on programmide väiksem töökiirus - deklaratiivne programm ei pruugi küll alati aeglasem olla, kui imperatiivne, kuid on seda harilikult siiski. Põhjuseks on siin keele automaattranslaatori väiksem intelligentsus kogenud programmeerijaga võrreldes.

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Funktsionaalsed ja loogilised keeled Deklaratiivsed keeled jaotatakse Funktsionaalse programmeerimise keelteks (näide: Haskell), kus lahendus kirjeldatakse funktsioonide kogu abil - ka viimast saab tegelikult käsitleda kui teatud tüüpi loogikasüsteemi. Loogilise programmeerimise keelteks (näide: Prolog), kus otsitavat lahendust kirjeldatakse loogika keeles

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Alus: lambda-arvutus Lambda-arvutuse keel on Alonzo Churchi poolt aastatel leiutatud lihtne ja universaalne meetod funktsioonide kirjapanekuks. Lambda-arvutuse teooria tegeleb arvutatavuse ja arvutatavate funktsioonide uurimisega, kasutades selleks lambda-arvutuse keelt kui universaalset programmeerimiskeelt. Churchi tees väidab, et iga algoritmi saab lambda-arvutuse keeles kirja panna. On võimalik näidata, et lambda-arvutus, nagu ka Prolog, C ja Basic on üks paljudest universaalsetest programmeerimiskeeltest. Konkreetselt on lambda-arvutuse keel ja teooria funktsionaalsete programmeerimiskeelte aluseks.

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Annonüümsed funktsioonid üks harilikumaid praktikas kasutatavaid funktsioonide kirjapaneku viise on selline: f(x) = x*x + 1 Funktsioon esitatakse, andes talle samas nime, konkreetses näites f. Lambda-arvutuses esitatakse funktsioone, vastupidi, kui anonüümseid, nimeta terme. äsjatoodud näide on lambda-kirjaviisis x. x*x + 1 Lambda-sümboli järele kirjutatakse funktsiooni formaalseks parameetriks olev muutuja, seejärel punkt ja funktsiooni keha.

IT Kolledzh/TTÜ 2002 T.Tammet IT sissejuhatus loeng 11 lk Lambda-arvutus Mitme formaalse parameetriga funktsioone esitatakse mitme üksteise sees oleva üheparameetrilise funktsioonina: x. y. x*x+y*y.