COMP6700/2140 Recursive Operations

Similar documents
COMP6700/2140 Correctness and Efficiency

COMP6700/2140 Sorting and Searching

International Journal of Scientific & Engineering Research, Volume 4, Issue 7, July ISSN

Software Analysis. Asymptotic Performance Analysis

COMP6700/2140 Abstract Data Types: Queue, Set, Map

COMP6700/2140 Std. Lib., I/O

COMP6700/2140 Methods

Using Templates to Introduce Time Efficiency Analysis in an Algorithms Course

COMP6700/2140 Packages, Modules and Jigsaw

CS2 Algorithms and Data Structures Note 1

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

COMP6700/2140 Data and Types

COMP6700/2140 Generic Methods

Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

Lecture 10: Recursion vs Iteration

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

COMP6700/2140 Objects

CS 157: Assignment 5

Algorithms and Data Structures

Analysis of Algorithms - Introduction -

Data Structures and Algorithms

COMP6700/2140 Code as Data

COMP6700/2140 Control Flow

Algorithms and Data Structures, or

COMP6700/2140 JDK Tools

COMP6700/2140 First Program

Outline. Computer Science 331. Desirable Properties of Hash Functions. What is a Hash Function? Hash Functions. Mike Jacobson.

JPROFILE102: A System for Experimental Analysis of Algorithms

COMP6700/2140 Abstract Data Types: Lists and Iteration

Basic Data Structures and Heaps

COMP6700/2140 GUI and Event Driven Programming

Lecture 1. Introduction

Twelve Simple Algorithms to Compute Fibonacci Numbers

arxiv: v1 [cs.dm] 1 Jul 2016

Data Structures and Algorithms

Recursion. CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Introduction to Algorithms Third Edition

Partha Sarathi Mandal

CS/COE 1501

Fundamental Algorithms

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

CS 310 Advanced Data Structures and Algorithms

Design and Analysis of Algorithms. Comp 271. Mordecai Golin. Department of Computer Science, HKUST

Book List. A Collection of Books. Computer Science

The p-sized partitioning algorithm for fast computation of factorials of numbers

COMP251: Algorithms and Data Structures. Jérôme Waldispühl School of Computer Science McGill University

STRUCTURE EXITS, NOT LOOPS

Implementing Divide and Conquer Technique for a Big-Data Traffic

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

Department of Information Technology

CS 3410 Ch 7 Recursion

arxiv: v3 [cs.ds] 18 Apr 2011

A Simplified Correctness Proof for a Well-Known Algorithm Computing Strongly Connected Components

Data Structures and Algorithms 1

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

Lecture C8: Trees part II 3 C 2 A 4 D. Response to 'Muddiest Part of the Lecture Cards' (16 respondents)

Instructors: Daniel Deutch, Amir Rubinstein, Teaching Assistants: Amir Gilad, Michal Kleinbort

SRI VENKATESWARA COLLEGE OF ENGINEERING. COURSE DELIVERY PLAN - THEORY Page 1 of 6

CS3110 Spring 2017 Lecture 18: Binary Search Trees

Aman Kumar. Department of Computer Science & Engineering, IIT Kanpur 3 November, 2014

Recursion. Chapter 7

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

Algorithm Analysis and Design

Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures. Recursive design. Convert loops to recursion

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Advanced Algorithms and Data Structures

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

B. Subject-specific skills B1. Problem solving skills: Supply the student with the ability to solve different problems related to the topics

arxiv: v3 [cs.ds] 3 Apr 2018

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Slide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Objectives. Recursion. One Possible Way. How do you look up a name in the phone book? Recursive Methods Must Eventually Terminate.

PAijpam.eu A NOTE ON KNUTH S IMPLEMENTATION OF EXTENDED EUCLIDEAN GREATEST COMMON DIVISOR ALGORITHM Anton Iliev 1, Nikolay Kyurkchiev 2, Angel Golev 3

Virtual University of Pakistan

Advanced Algorithms and Data Structures

The University of Aizu School of Computer Science and Engineering Introduction to Programming. Course Syllabus (Special Track for Foreign Students)

School of Computing and Information Sciences. Course Title: Data Structures Date: 3/30/2010 Course Number: COP 3530 Number of Credits: 3

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

Smart Sort and its Analysis

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

COMP-202 Unit 8: Recursion. CONTENTS: Recursion in Java More complex examples

Recursion Chapter 3.5

D.Abhyankar 1, M.Ingle 2. -id: 2 M.Ingle, School of Computer Science, D.A. University, Indore M.P.

COMP6700/2140 Operators, Expressions, Statements

Programming II (CS300)

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Data & Procedure Reasoning about correctness

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Position Sort. Anuj Kumar Developer PINGA Solution Pvt. Ltd. Noida, India ABSTRACT. Keywords 1. INTRODUCTION 2. METHODS AND MATERIALS

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

Analysis of Time complexity in Binary Search Tree

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers

CITS3001. Algorithms, Agents and Artificial Intelligence. Semester 2, 2016

Transcription:

COMP6700/2140 Recursive Operations Alexei B Khorev and Josh Milthorpe Research School of Computer Science, ANU May 2017 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 1 / 12

Topics 1 Recursive Functions 2 Tail Recursion Recursion stack JVM Stack Machine Architecture 3 Tail Recursion Optimisation 4 Recursions vs Iterations 5 Best Algorithm Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 2 / 12

Recursive methods Methods which call themselves A basic problem of calculating the factorial of N (trite, cheesy example sorry): N! = N (N 1) (N 2) 2 1 = N (N 1)! One implementation is old fashioned iterative: public static int factorial(int n) { int r = 1; for (int i=1; i<=n; i++) r=r*i; return r; } The recursive implementation: public static int factorial(int n) { assert (n >= 0) : "The factorial is tricky if the argument is negative!" if (n == 0) return 1; // Base Case: recursion stops return n * factorial(n-1); // Generic Case: a recursion call is made } Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 3 / 12

Recursion call stack Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 4 / 12

Tail recursion What if the body of a value returning recursive method can be written in such a way that the only recursive call is the one which is made in the return statement? For factorial, this would require almost pointless and trivial modification: public static int tailfactorial(int n, long a) { assert (n >= 0) : "The factorial is tricky if the argument is negative!" if (n == 0) return a; return tailfactorial(n-1,n*a); } It is obvious, that mathematically factorial(x) and tailfactorial(x,1) are the same function Yet in computing: It is often easier to do more work rather than less (quoted from the book From Mathematics to Generic Programming by Alexander A Stepanov and Daniel E Rose) So, what s the point? Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 5 / 12

Tail recursion call stack Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 6 / 12

Tail Recursion Optimisation Let s rewrite again, but using reassignment of the passed arguments: public static int tailfactorial(int n, long a) { if (n == 0) return a; a *= n; n--; return tailfactorial(n,a); // when called, the above code is repeated verbatim } Such form of a tail recursion when all the tail recursive calls are made with formal arguments of the method being the corresponding arguments is called a strict tail recursion The recursive call in the return statement results in all preceding statements of the method body repeating themselves Nothing gets changed if the last return statement is dropped, and instead the remaining body is repeated until the base case (n = 0) is realised and the iteration terminated: public static int tailfactorial(int n, long a) { while (true) { if (n == 0) return a; a *= n; n--; } } The recursion has been transformed into a loop Tail-Call Optimisation! Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 7 / 12

Tail recursion optimised Recursion call stack is gone no heavy (vertical) burden to carry Computation is horizontal now, the stack isn t consumed, there is no bound on how long the repetition can go Languages like Lisp could perform the tail recursion optimisation automatically, as the part of their code compilation/interpretation Java (and hence other JVM languages) cannot perform TCO due to restrictions imposed by the JVM architecture But there are means to deal with this problem (wait till F6 lecture on Recursion, Corecursion and Memoization in Functional Java Programming ) Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 8 / 12

The best algorithm Imagine, 10% improvement in performance of the search engine in online mega-businesses like Google or Amazon The difference is tens of millions of dollars A 10% more efficient algorithm in a mobile battery-powered device means a few hours of longer work A 10% less power consuming servers may translate into 1 2% less gross country s energy consumption (in South Korea, about 7-9% of total power is spent on running the internet and telephony servers) What affects the algorithms performance? 1 Mathematical efficiency (O, Ω and Θ); 2 Time and Space resource requirements CPU cycles T(N) and memory P(N) and the cost of their provision (how fast data can be brought to the processing unit, what is the frequency of cache misses etc); 3 Which data structures are used and how do they map into the processor architecture (size and layout of cache, bus parameters etc); as fas as the role of cache is concerned, the situation can be similar to the binary search bug which reveals itself when we move to larger data sizes Here, instead of becoming buggy, an algorithm can drastically worsen its performance an example is the HeapSort, which is on par with QuickSort (O(N log N)) and doesn t have the worst case problem, like QuickSort (O(N 2 ) on bad inputs) This changes when the size becomes large, and the cache misses begin affect the performance With all these factors playing an important role, the finding and choice of the best algorithm has been and still is a very non-trivial and important problem Finding and implementing algorithms will remain an important problem the IT industry has always to deal with Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 9 / 12

Recursions vs Loops When one is better than another Iterations are most often faster (recursions have method stack overhead), but this advantage is not crucial Iterations require less memory (recursions have limited depth due to finite stack size), and this maybe crucial Recursions often (not always!) result in a simpler (often, much simpler) implementation due to the algorithm recursive nature (either due to the structure of mathematical definition, like factorial or Fibonacci sequence, or due to recursive nature of the data structure on which the algorithm operates, eg, trees) Non-recursive algorithms on recursive data structures are possible, eg, search in a binary tree (but it also involves a stack) Recursive algorithms on non-recursive data structures (arrays, lists ) are possible (Quicksort and others) Proof of correctness for recursive algorithms often greatly assisted by the mathematical induction method When using recursion, always provide the base case (cases), and ensure that the recursive calls converge to one of the base cases Remember of the stack size limitation! Never use recursion for processing user inputs! Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 10 / 12

Other algorithms Study of algorithm is at the core of Computer Science Simple fundamental algorithms is the ultimate portable software (R Sedgewick, J Bentley) Not so fast, guys! (Alexander Stepanov) Algorithm Intensive CS Fields 1 Sorting and searching on various data structures 2 Graphs and Networks (include data mining) 3 Optimisation (incl parse trees in compiling) 4 Number theory including encryption (factorisation, elliptic curve arithmetic) 5 Computational geometry and computer graphics 6 Numerical computations (matrix multiplication and spectral analysis, Newton s method etc) Few references Introduction to Algorithms, by Thomas H Cormen, Sharles E Leiserson, Ronald L Rivest and Clifford Stein, McGraw-Hill, 3d Ed, 2007 Fundamentals of Algorithms, by Gilles Brassard and Paul Bratley, Prentice-Hall, 1996 The Art of Computer Programming 3rd Edition, by Donald Knuth, Addison-Wesley, 1997 1998; volumes: 1 Fundamental Algorithms 2 Seminumerical Algorithms 3 Sorting and Searching Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 11 / 12

Where to look for this topic in the textbook? Very little about recursive algorithms and algorithms in general is discussed in the textbook (it teaches users how to program, not developers of programming resources, like libraries and API) You should seek other sources of knowledge in these areas, both in printed form and (much more and growing) online Hortsmann s Core Java for the Impatient, Ch 33, 38 Oracle s Java Tutorial chapter Algorithms again, it deals with the JFC usage, not much with the intricacies of generic algorithm implementation One modern (and relatively light) text is Algorithms in a Nutshell, 2ed, by George T Heineman, Gary Pollice, and Stanley Selkow (O Reilly, 2017) Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations May 2017 12 / 12