Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
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)
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Recursion. Binary search example postponed to end of lecture.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Recursion … just in case you didn’t love loops enough …
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
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.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
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.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Data Structures Mohamed Mustaq Ahmed Chapter 2- Algorithms.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Recursion.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
Analysis of Algorithms
2-1 2 Algorithms Principles. Efficiency. Complexity. O-notation. Recursion. © 2001, D.A. Watt and D.F. Brown.
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.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
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.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Chapter 8 Recursion Modified.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
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.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
1 Examples of Recursion Instructor: Mainak Chaudhuri
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Algorithm Analysis 1.
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Algorithm Analysis CSE 2011 Winter September 2018.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Chapter 14: Recursion Starting Out with C++ Early Objects
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Recursion Data Structures.
Recursion.
Algorithms and data structures: basic definitions
Presentation transcript:

Data Structure and Algorithms

Algorithms: efficiency and complexity Recursion Reading Algorithms

Algorithms are stepwise solutions to problems There may be more than one algorithm for a particular problem

Efficiency of Algorithms Time efficiency Space efficiency There may be several algorithms to solve the same problem Sometimes a trade-off between time and space is necessary

Factors affecting/ masking speed Number of input values Processor speed Number of simultaneous programs etc The location of items in a data structure Different implementations (Software differences)

Time efficiency Count characteristic operations eg arithmetical operations eg multiplications Examine critical section/s of algorithm –Single statement/group of statements/single operation –Central to functioning of algorithm contained in most deeply nested loops of algorithm

Critical Section Visiting a Distant Friend 1.Close own door 2.Travel 100 miles by bus 3.Knock on friend’s door Code Example Statement 1; for (int miles = 0; miles < 100; miles ++) Statement 2; Statement 3; Travel 100 miles by bus Statement 2;

Different Algorithms for Same Problem Findingnumber power eg b 3 b 20 1set answer to 1 2Loop 1 to power times 2.1answer becomes answer * number 3report answer 1 Set answer to 1, num to number, value to power 2 Loop while value is positive 2.1 if value is odd then multiply answer by num 2.2 divide value by 2 ignore remainder, multiply num by itself 3Report answer Second b 2 x b b 10 x b times First b x b x b b x b … 20 times

Algorithm Comparison The first example is said to have complexity of order nwritten as O(n) The second example is said to have complexity of order log nwritten as O(log n) O(log n) is better than O(n)

Complexities in Increasing Order 1O(1) 2O(log n) 3O (n) 4O (n log n) 5O (n 2 ) 6O (n 3 ) 7O (2 n ) For large problems always choose an asymptotically superior algorithm (ie an algorithm of the lowest order possible) Feasible algorithms are algorithms which are fast enough to be used in practice. (1-4 are always feasible)

Complexities and Search algorithms Linear search: unsorted array: O(n) Linear search: sorted array: O(n) Binary search: sorted array: O(log n)

Summary When problems are large use lower order algorithms

Recursion A recursive method is a method that calls itself. It must have 3 basic properties 1.A recursive call (a call to itself) 2.An end case to stop recursion 3.A test to determine whether to stop or continue recursion

Recursive method for factorial of N The factorial of N is the product of the first N positive integers. The simple case is that N = 1 which will represent the end case. If N is not equal to 1 then there is a need to call the method again (a recursive call to continue until the case N = 1 is reached) The method will successively make a call of the form: factorial(N-1) each time N is not equal to 1.

Recursive method for Factorial public int factorial(int N) { if (N = = 1) { return 1; } else { return N * factorial(N-1); } 3 Test 2 End case stops recursion 1 Recursive call

Recursion public int factorial(int N){ if (N = = 1) { return 1; } else { return N * factorial(N-1); } } Find the factorial of 5. (5!) 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 Sequence of Calls Sequence of Returns 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 = 2 = 6 = 24 = 120

Use Recursion Equivalent iterative solution is too complex Recursive solution is easy to understand and natural

Towers of Hanoi Move a number of disks from one peg to another peg by moving one disk at a time and never placing a larger disk on top of a smaller disk. To help this a third peg is available.

Towers of Hanoi

public void towersOfHanoi( int N, int source, int destination, int spare) { if ( N = = 1) { // move a single disk from source to destination moveOne ( source, destination ); } else { //spare is remaining pole; move tower of (N-1) disks from source to spare towersOfHanoi( N-1, source, spare, destination); //move single disk from source to destination moveOne( source, destination); //move tower of (N-1) disks from spare to destination towersOfHanoi( N-1, spare, destination, source ); } private void moveOne( int source, int destination ){ System.out.println( source + “- - - ” + destination ); }

Approaching Recursive Algorithms 1. Formulate a Concise Specification of the Problem What data/information is needed initially? What precisely is accomplished? Represent the problem using an appropriate identifier with parameters Example: Sum of 1st N Positive Integers 1.Problem Specification A value for N Assuming N>= 0 sum the first N integers (if N=0 the sum is 0) nSum(N)

Approaching Recursive Algorithms 2.Design a high-level strategy for a solution Break the solution down into stages Identify at least one stage that involves solving a simpler version of the same problem Represent the simpler version(s) of the problem using the chosen identifier with appropriate parameters 2.Strategy for Solution Solve nSum(N-1) Add N to the result so obtained

Approaching Recursive Algorithms 3.Refine steps 1 and 2 if necessary Think about the way you yourself would solve the problem in principle If no strategy can be developed consider modifying the specification 3.Detailed Solution If N <= 0 then return the result 0 Otherwise Solve nSum(N-1) Add N to the result so obtained Return this sum

Approaching Recursive Algorithms 4.Complete the details of the design Which stages need to be broken down further? Is it relatively straightforward to implement the solution? What are the stopping conditions or base cases? Are there any constraints on the parameters?

Approaching Recursive Algorithms 5.Implement in the chosen language Write a method with parameters as specified in step 1 The simpler versions of the problem identified in step 2 are solved by invoking the same method recursively Ensure that stopping conditions are correctly coded static int nSum(int n) { // Returns sum of 1st n integers using recursion if (n <= 0) return 0; else return n + nSum(n-1); }

Example: Largest in an Array of Integers 1.Problem Specification Pr(A,N): Find the largest value in first N positions of array A 2.Strategy for Solution Solve Problem Pr(A,N-1) Compare A[N-1] with the result so obtained Required result is the larger of the two values 3.Detailed Solution If N = 1 then the result is A[0] Otherwise Solve Problem Pr(A,N-1) Compare A[N-1] with the result so obtained Required result is the larger of the two values

Implementation static int largest(int[] a, int n) { // Returns largest value of first n values in a int x; if (n > 0) { if (n == 1) return a[0]; else { x = largest(a,n-1); if (x > a[n-1]) return x; else return a[n-1]; }