top down approach to algorithms
play

Top-Down Approach to Algorithms Understanding Algorithms Amtoft - PowerPoint PPT Presentation

Top-Down Approach to Algorithms Understanding Algorithms Amtoft (Howell) Introduction Sorting Reduction: Solve a problem by using a solution to a Maximum Subsequence Sum simpler problem. Top-Down Approach to Algorithms Understanding


  1. Top-Down Approach to Algorithms Understanding Algorithms Amtoft (Howell) Introduction Sorting Reduction: Solve a problem by using a solution to a Maximum Subsequence Sum “simpler” problem.

  2. Top-Down Approach to Algorithms Understanding Algorithms Amtoft (Howell) Introduction Sorting Reduction: Solve a problem by using a solution to a Maximum Subsequence Sum “simpler” problem. The selection problem: ◮ Input: An array A [1 .. n ] of Number s and a Nat k . ◮ Output: The k th smallest element of A .

  3. Top-Down Approach to Algorithms Understanding Algorithms Amtoft (Howell) Introduction Sorting Reduction: Solve a problem by using a solution to a Maximum Subsequence Sum “simpler” problem. The selection problem: ◮ Input: An array A [1 .. n ] of Number s and a Nat k . ◮ Output: The k th smallest element of A . One solution: 1. Sort A . 2. Return A [ k ].

  4. Top-Down Approach to Algorithms Understanding Algorithms Amtoft (Howell) Introduction Sorting Reduction: Solve a problem by using a solution to a Maximum Subsequence Sum “simpler” problem. The selection problem: ◮ Input: An array A [1 .. n ] of Number s and a Nat k . ◮ Output: The k th smallest element of A . One solution: 1. Sort A . 2. Return A [ k ]. We have reduced selection to sorting.

  5. Sorting Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We may sort an array A [1 .. n ] for n > 1 by

  6. Sorting Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We may sort an array A [1 .. n ] for n > 1 by 1. sorting A [1 .. n − 1]; then

  7. Sorting Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We may sort an array A [1 .. n ] for n > 1 by 1. sorting A [1 .. n − 1]; then 2. inserting A [ n ] into A [1 .. n − 1] at the proper location.

  8. Sorting Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We may sort an array A [1 .. n ] for n > 1 by 1. sorting A [1 .. n − 1]; then 2. inserting A [ n ] into A [1 .. n − 1] at the proper location. If n ≤ 1, then A [1 .. n ] is already sorted.

  9. Sorting Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We may sort an array A [1 .. n ] for n > 1 by 1. sorting A [1 .. n − 1]; then 2. inserting A [ n ] into A [1 .. n − 1] at the proper location. If n ≤ 1, then A [1 .. n ] is already sorted. We have reduced larger instances of sorting to smaller instances.

  10. Recursive Insertion Sort Understanding Algorithms Amtoft (Howell) Introduction Precondition: A [1 .. n ] is an array of Number s, n is a Nat . Sorting Postcondition: A [1 .. n ] is a permutation of its initial values Maximum Subsequence Sum such that for 1 ≤ i < j ≤ n , A [ i ] ≤ A [ j ]. InsertSort ( A [1 .. n ]) if n > 1 InsertSort ( A [1 .. n − 1]) Insert ( A [1 .. n ])

  11. Recursive Insertion Sort Understanding Algorithms Amtoft (Howell) Introduction Precondition: A [1 .. n ] is an array of Number s, n is a Nat . Sorting Postcondition: A [1 .. n ] is a permutation of its initial values Maximum Subsequence Sum such that for 1 ≤ i < j ≤ n , A [ i ] ≤ A [ j ]. InsertSort ( A [1 .. n ]) if n > 1 InsertSort ( A [1 .. n − 1]) Insert ( A [1 .. n ]) Precondition: A [1 .. n ] is an array of Number s such that n is a Nat , and for 1 ≤ i < j ≤ n − 1, A [ i ] ≤ A [ j ]. Postcondition: A [1 .. n ] is a permutation of its initial values such that for 1 ≤ i < j ≤ n , A [ i ] ≤ A [ j ]. Insert ( A [1 .. n ])

  12. Maximum Subsequence Sum Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum Input: An array A [0 .. n − 1] of (possibly negative) Number s. Output: The maximum sum of any contiguous subsequence of A ; i.e., � j − 1 � � max A [ k ] | 0 ≤ i ≤ j ≤ n . k = i

  13. A Naive Algorithm Understanding Algorithms Amtoft (Howell) Introduction Precondition: A [0 .. n − 1] is an array of Number s, n is Sorting a Nat . Maximum Subsequence Sum Postcondition: Returns the maximum subsequence sum of A . MaxSumIter ( A [0 .. n − 1]) m ← 0 for i ← 0 to n for j ← i to n sum ← 0 for k ← i to j − 1 sum ← sum + A [ k ] m ← Max ( m , sum ) return m

  14. Improving the Naive Algorithm Understanding Algorithms Amtoft (Howell) Introduction Sorting Precondition: A [0 .. n − 1] is an array of Number s, n is Maximum Subsequence a Nat . Sum Postcondition: Returns the maximum subsequence sum of A . MaxSumOpt ( A [0 .. n − 1]) m ← 0 for i ← 0 to n − 1 sum ← 0 for k ← i to n − 1 sum ← sum + A [ k ] m ← Max ( m , sum ) return m

  15. Reducing to a Smaller Problem Understanding Algorithms Amtoft (Howell) Introduction Sorting We can reduce an instance of size n > 0 to an instance of Maximum Subsequence size n − 1: Sum

  16. Reducing to a Smaller Problem Understanding Algorithms Amtoft (Howell) Introduction Sorting We can reduce an instance of size n > 0 to an instance of Maximum Subsequence size n − 1: Sum 1. Find the maximum subsequence sum of the first n − 1 elements.

  17. Reducing to a Smaller Problem Understanding Algorithms Amtoft (Howell) Introduction Sorting We can reduce an instance of size n > 0 to an instance of Maximum Subsequence size n − 1: Sum 1. Find the maximum subsequence sum of the first n − 1 elements. 2. Find the maximum suffix sum; i.e., � n − 1 � � max A [ k ] | 0 ≤ i ≤ n . k = i

  18. Reducing to a Smaller Problem Understanding Algorithms Amtoft (Howell) Introduction Sorting We can reduce an instance of size n > 0 to an instance of Maximum Subsequence size n − 1: Sum 1. Find the maximum subsequence sum of the first n − 1 elements. 2. Find the maximum suffix sum; i.e., � n − 1 � � max A [ k ] | 0 ≤ i ≤ n . k = i 3. Return the maximum of these two values.

  19. Reducing to a Smaller Problem Understanding Algorithms Amtoft (Howell) Introduction Sorting We can reduce an instance of size n > 0 to an instance of Maximum Subsequence size n − 1: Sum 1. Find the maximum subsequence sum of the first n − 1 elements. 2. Find the maximum suffix sum; i.e., � n − 1 � � max A [ k ] | 0 ≤ i ≤ n . k = i 3. Return the maximum of these two values. If n = 0, the maximum subsequence sum is 0.

  20. Finding the Maximum Suffix Sum Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We can find the maximum suffix sum in a similar way; i.e., if n > 0:

  21. Finding the Maximum Suffix Sum Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We can find the maximum suffix sum in a similar way; i.e., if n > 0: 1. Find the maximum suffix sum of the first n − 1 elements.

  22. Finding the Maximum Suffix Sum Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We can find the maximum suffix sum in a similar way; i.e., if n > 0: 1. Find the maximum suffix sum of the first n − 1 elements. 2. Add the last element.

  23. Finding the Maximum Suffix Sum Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We can find the maximum suffix sum in a similar way; i.e., if n > 0: 1. Find the maximum suffix sum of the first n − 1 elements. 2. Add the last element. 3. Return the maximum of this sum and 0.

  24. Finding the Maximum Suffix Sum Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We can find the maximum suffix sum in a similar way; i.e., if n > 0: 1. Find the maximum suffix sum of the first n − 1 elements. 2. Add the last element. 3. Return the maximum of this sum and 0. If n = 0, the maximum subsequence sum is 0.

  25. Maximal Subsequence Sum, Top-Down Understanding Algorithms Amtoft (Howell) Introduction Sorting Precondition: A [0 .. n − 1] is an array of Number s, n is Maximum Subsequence Sum a Nat . Postcondition: Returns the maximum subsequence sum of A . MaxSumTD ( A [0 .. n − 1]) if n = 0 return 0 else return Max ( MaxSumTD ( A [0 .. n − 2]) , MaxSuffixTD ( A [0 .. n − 1]))

  26. Maximal Suffix Sum, Computed Top-Down Understanding Algorithms Amtoft (Howell) Introduction Sorting Precondition: A [0 .. n − 1] is an array of Number s, n is Maximum Subsequence Sum a Nat . Postcondition: Returns the maximum suffix sum of A . MaxSuffixTD ( A [0 .. n − 1]) if n = 0 return 0 else return Max (0 , A [ n − 1]+ MaxSuffixTD ( A [0 .. n − 2]))

  27. Divide and Conquer Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We can reduce an instance of size n > 1 to instances of size ⌊ n / 2 ⌋ and ⌈ n / 2 ⌉ .

  28. Divide and Conquer Understanding Algorithms Amtoft (Howell) Introduction Sorting Maximum Subsequence Sum We can reduce an instance of size n > 1 to instances of size ⌊ n / 2 ⌋ and ⌈ n / 2 ⌉ . The maximum of the solutions to the smaller instances does not include any segments that start in the first instance and end in the last instance.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend