lecture 20 peak finding in 2d
play

Lecture 20: Peak Finding in 2D COMS10007 - Algorithms Dr. Christian - PowerPoint PPT Presentation

Lecture 20: Peak Finding in 2D COMS10007 - Algorithms Dr. Christian Konrad 30.04.2019 Dr. Christian Konrad Lecture 20: Peak Finding in 2D 1 / 14 Peak Finding Let A = a 0 , a 1 , . . . , a n 1 be an array of integers of length n 0 1 2


  1. Lecture 20: Peak Finding in 2D COMS10007 - Algorithms Dr. Christian Konrad 30.04.2019 Dr. Christian Konrad Lecture 20: Peak Finding in 2D 1 / 14

  2. Peak Finding Let A = a 0 , a 1 , . . . , a n − 1 be an array of integers of length n 0 1 2 3 4 5 6 7 8 9 a 0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 Definition: (Peak) Integer a i is a peak if adjacent integers are not larger than a i Example: 4 3 9 10 14 8 7 2 2 2 Dr. Christian Konrad Lecture 20: Peak Finding in 2D 2 / 14

  3. Peak Finding Let A = a 0 , a 1 , . . . , a n − 1 be an array of integers of length n 0 1 2 3 4 5 6 7 8 9 a 0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 Definition: (Peak) Integer a i is a peak if adjacent integers are not larger than a i Example: 4 3 9 10 14 8 7 2 2 2 Dr. Christian Konrad Lecture 20: Peak Finding in 2D 2 / 14

  4. Peak Finding Let A be an n -by- m matrix of integers   A 11 A 12 A 13 . . . A 1 m ...   A 21     ... A =   A 31   .  ...  .   .   . . . A n 1 A n 2 A n 3 A nm Definition: (Peak in 2D) Integer A ij is a peak if adjacent integers are not larger than A ij Dr. Christian Konrad Lecture 20: Peak Finding in 2D 3 / 14

  5. Example and Trivial Algorithm How many peaks are contained in this matrix?  1 5 8 3  2 1 8 9     3 1 1 2     7 7 8 10   2 1 1 1 Trivial Algorithm For each position i , j , check whether A i , j is a peak There are n · m positions Checking whether A i , j is a peak takes time O (1) Runtime: O ( nm ) Dr. Christian Konrad Lecture 20: Peak Finding in 2D 4 / 14

  6. Example and Trivial Algorithm How many peaks are contained in this matrix?  1 5 8 3  2 1 8 9     3 1 1 2     7 7 8 10   2 1 1 1 Trivial Algorithm For each position i , j , check whether A i , j is a peak There are n · m positions Checking whether A i , j is a peak takes time O (1) Runtime: O ( nm ) How can we do better? Dr. Christian Konrad Lecture 20: Peak Finding in 2D 4 / 14

  7. Divide-and-Conquer Solution Divide-and-Conquer Divide the problem into a number of subproblems that are smaller instances of the same problem. Conquer the subproblems by solving them recursively. If the subproblems are small enough, just solve them in a straightforward manner. Combine the solutions to the subproblems into the solution for the original problem. Dr. Christian Konrad Lecture 20: Peak Finding in 2D 5 / 14

  8. 1D Peak Finding Divide-and-Conquer in 1D: Fast-Peak-Finding 1 Check whether A [ ⌊ n / 2 ⌋ ] is a peak, if yes then return A [ ⌊ n / 2 ⌋ ] 2 Else, if A [ ⌊ n / 2 ⌋ − 1] > A [ ⌊ n / 2 ⌋ ] then recursively find a peak in A [0 , ⌊ n / 2 ⌋ − 1] 3 Else, recursively find a peak in A [ ⌊ n / 2 ⌋ + 1 , n − 1] Dr. Christian Konrad Lecture 20: Peak Finding in 2D 6 / 14

  9. Crucial Property Crucial Property: When recursing on subarray, need to make sure that peak in subarray is also peak in initial array Example: A = 1 2 3 4 5 6 7 8 Algorithm first inspects 4 and recurses on right half 5 6 7 8 Will eventually find the only peak 8 Suppose we recursed on left half 1 2 3 peak in 1 2 3 is not a peak in A ! Dr. Christian Konrad Lecture 20: Peak Finding in 2D 7 / 14

  10. 2D Peak Finding Divide-and-Conquer: Divide step Find maximum among central column and boundary If it is not a peak, conquer either on left or right half   A 11 . . . A 1 , m A 1 , m A 1 , m . . . A 1 m 2 − 1 2 +1 2 A 21 . . . A 2 , m A 2 , m A 2 , m . . . A 2 m   2 − 1 2 +1  2  . . .   . . . . . .     . . . . . . A n − 1 , 1 A n − 1 , m A n − 1 , m A n − 1 , m A n − 1 , m   2 − 1 2 +1  2  A n , 1 . . . A n , m A n , m A n , m . . . A n , m 2 − 1 2 +1 2 In each recursive call, number of elements in matrix halves (at least) Hence O (log( mn )) recursive calls In each call: O ( n + m ), thus in total O (( n + m ) log( nm )) Can be improved to O (max { m , n } ) ! Dr. Christian Konrad Lecture 20: Peak Finding in 2D 8 / 14

  11. 2D Peak Finding Divide-and-Conquer: Divide step Find maximum among central column and boundary If it is not a peak, conquer either on left or right half   A 11 . . . A 1 , m 2 − 1 A 21 . . . A 2 , m   2 − 1   .   . .     . . . A n − 1 , 1 A n − 1 , m   2 − 1   A n , 1 . . . A n , m 2 − 1 In each recursive call, number of elements in matrix halves (at least) Hence O (log( mn )) recursive calls In each call: O ( n + m ), thus in total O (( n + m ) log( nm )) Can be improved to O (max { m , n } ) ! Dr. Christian Konrad Lecture 20: Peak Finding in 2D 8 / 14

  12. Recursion on which side? Recursion on which side? Since maximum not a peak, a not considered neighbor larger Recurse on the side that contains this larger neighbor   . . . . . . A 11 A 1 , m A 1 , m A 1 , m A 1 m 2 − 1 2 +1 2 A 21 . . . A 2 , m A 2 , m A 2 , m . . . A 2 m   2 − 1 2 +1  2  . . .  . . .  . . .     A n − 1 , 1 . . . A n − 1 , m A n − 1 , m A n − 1 , m . . . A n − 1 , m   2 − 1 2 +1  2  A n , 1 . . . A n , m A n , m A n , m . . . A n , m 2 − 1 2 +1 2 Dr. Christian Konrad Lecture 20: Peak Finding in 2D 9 / 14

  13. Recursion on which side? Recursion on which side? Since maximum not a peak, a not considered neighbor larger Recurse on the side that contains this larger neighbor   . . . . . . A 11 A 1 , m A 1 , m A 1 , m A 1 m 2 − 1 2 +1 2 A 21 . . . A 2 , m A 2 , m A 2 , m . . . A 2 m   2 − 1 2 +1  2  . . .  . . .  . . .     A n − 1 , 1 . . . A n − 1 , m A n − 1 , m A n − 1 , m . . . A n − 1 , m   2 − 1 2 +1  2  A n , 1 . . . A n , m A n , m A n , m . . . A n , m 2 − 1 2 + 1 2 Dr. Christian Konrad Lecture 20: Peak Finding in 2D 9 / 14

  14. Recursion on which side? Recursion on which side? Since maximum not a peak, a not considered neighbor larger Recurse on the side that contains this larger neighbor   . . . . . . A 11 A 1 , m A 1 , m A 1 , m A 1 m 2 − 1 2 +1 2 A 21 . . . A 2 , m A 2 , m A 2 , m . . . A 2 m   2 − 1 2 +1  2  . . .  . . .  . . .     A n − 1 , 1 . . . A n − 1 , m A n − 1 , m A n − 1 , m . . . A n − 1 , m   2 − 1 2 +1  2  A n , 1 . . . A n , m A n , m A n , m . . . A n , m 2 − 1 2 +1 2 Dr. Christian Konrad Lecture 20: Peak Finding in 2D 9 / 14

  15. Why Does it Work? Correctness: Suppose algorithm finds peak in a submatrix A ′ Why is this also a peak in A ? First Case: Peak is in central column of A ′ � Second Case: Peak in bottom or top boundary of A ′ Only happens in first iteration �  A ′ . . . A ′ A ′ A ′ . . . A ′  11 1 , m ′ 1 , m ′ 1 , m ′ 1 m ′ 2 − 1 2 +1 2  A ′ . . . A ′ A ′ A ′ . . . A ′  21 2 , m ′ 2 , m ′ 2 , m ′ 2 m ′   2 − 1 2 +1  2  . . .  . . .  . . .     A ′ . . . A ′ A ′ A ′ . . . A ′   n ′ − 1 , 1 n ′ − 1 , m ′ n ′ − 1 , m ′ n ′ − 1 , m ′ n ′ − 1 , m ′  2 − 1 2 +1  2   A ′ . . . A ′ A ′ A ′ . . . A ′ n ′ , 1 n ′ , m ′ n ′ , m ′ n ′ , m ′ n ′ , m ′ 2 − 1 2 +1 2 Dr. Christian Konrad Lecture 20: Peak Finding in 2D 10 / 14

  16. Why Does it Work? Correctness: Suppose algorithm finds peak in a submatrix A ′ Why is this also a peak in A ? First Case: Peak is in central column of A ′ � Second Case: Peak in bottom or top boundary of A ′ Only happens in first iteration �  A ′ . . . A ′ A ′ A ′ . . . A ′  11 1 , m ′ 1 , m ′ 1 , m ′ 1 m ′ 2 − 1 2 +1 2  A ′ . . . A ′ A ′ A ′ . . . A ′  21 2 , m ′ 2 , m ′ 2 , m ′ 2 m ′   2 − 1 2 +1  2  . . .  . . .  . . .     A ′ . . . A ′ A ′ A ′ . . . A ′   n ′ − 1 , 1 n ′ − 1 , m ′ n ′ − 1 , m ′ n ′ − 1 , m ′ n ′ − 1 , m ′  2 − 1 2 +1  2   A ′ . . . A ′ A ′ A ′ . . . A ′ n ′ , 1 n ′ , m ′ n ′ , m ′ n ′ , m ′ n ′ , m ′ 2 − 1 2 +1 2 Dr. Christian Konrad Lecture 20: Peak Finding in 2D 10 / 14

  17. Why Does it Work? Correctness: Suppose algorithm finds peak in a submatrix A ′ Why is this also a peak in A ? First Case: Peak is in central column of A ′ � Second Case: Peak in bottom or top boundary of A ′ Only happens in first iteration �  A ′ . . . A ′ A ′ A ′ . . . A ′  11 1 , m ′ 1 , m ′ 1 , m ′ 1 m ′ 2 − 1 2 +1 2  A ′ . . . A ′ A ′ A ′ . . . A ′  21 2 , m ′ 2 , m ′ 2 , m ′ 2 m ′   2 − 1 2 +1  2  . . .  . . .  . . .     A ′ . . . A ′ A ′ A ′ . . . A ′   n ′ − 1 , 1 n ′ − 1 , m ′ n ′ − 1 , m ′ n ′ − 1 , m ′ n ′ − 1 , m ′  2 − 1 2 +1  2   A ′ . . . A ′ A ′ A ′ . . . A ′ n ′ , 1 n ′ , m ′ n ′ , m ′ n ′ , m ′ n ′ , m ′ 2 − 1 2 +1 2 Dr. Christian Konrad Lecture 20: Peak Finding in 2D 10 / 14

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