Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms."— Presentation transcript:

1 Data Structure and Algorithms

2 Algorithms: efficiency and complexity Recursion Reading Algorithms

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

4 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

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

6 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

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

8 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 10 10 + 1 times First b x b x b b x b … 20 times

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

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

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

12 Summary When problems are large use lower order algorithms

13 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

14 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.

15 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

16 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

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

18 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.

19 Towers of Hanoi

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

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

22 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

23 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

24 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?

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

26 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

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

28


Download ppt "Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms."

Similar presentations


Ads by Google