1 / 28

Data Structure and Algorithms

Data Structure and Algorithms. 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

keagan
Télécharger la présentation

Data Structure and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structure and Algorithms

  2. Algorithms • Algorithms: efficiency and complexity • Recursion • Reading

  3. Algorithms • 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 • Close own door • Travel 100 miles by bus • Knock on friend’s door Travel 100 miles by bus • Code Example • Statement 1; • for (int miles = 0; miles < 100; miles ++) • Statement 2; • Statement 3; • Statement 2;

  8. Different Algorithms for Same Problem Second b2 x b b10 x b10 10 + 1 times First b x b x b b x b … 20 times • set answer to 1 • Loop 1 to power times • 2.1 answer becomes answer * number • 3 report answer Finding number power eg b3 b 20 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 3 Report answer

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

  10. Complexities in Increasing Order • O(1) • O(log n) • O (n) • O (n log n) • O (n2) • O (n3) • O (2n) 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 • A recursive call (a call to itself) • An end case to stop recursion • 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. 3 Test 2 End case stops recursion 1 Recursive call Recursive method for Factorial public int factorial(int N) { if (N = = 1) { return 1; } else { return N * factorial(N-1); } }

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

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

  21. Approaching Recursive Algorithms • 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 • Problem Specification • A value for NAssuming N>= 0 sum the first N integers (if N=0 the sum is 0) • nSum(N)

  22. Approaching Recursive Algorithms • 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 • Strategy for Solution • Solve nSum(N-1) • Add N to the result so obtained

  23. Approaching Recursive Algorithms • 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 • 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 • 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 • 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 • Problem Specification Pr(A,N):  Find the largest value in first N positions of array A • 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 • 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 aint 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]; } } }

More Related