1 / 26

A Linear Time Algorithm for the k Maximum Sums Problem

A Linear Time Algorithm for the k Maximum Sums Problem. By Gerth S. Brodal and Allan G. Jørgensen. The Maximum Sum Problem. Given array of numbers, find the subarray(vector) with the largest aggregate. -3. 7. -12. 1. 6. -3. 5. -2. Kadanes Algorithm. Scan array and update

kaoru
Download Presentation

A Linear Time Algorithm for the k Maximum Sums Problem

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. A Linear Time Algorithm for the k Maximum Sums Problem By Gerth S. Brodal and Allan G. Jørgensen

  2. The Maximum Sum Problem • Given array of numbers, find the subarray(vector) with the largest aggregate -3 7 -12 1 6 -3 5 -2

  3. Kadanes Algorithm • Scan array and update • Best current suffix • Best sum so far 1 7 -12 1 6 -3 5 -2 -4 1 8 1 7 7 9 4 8 1 9

  4. The k Maximal Sums Problem • Given array of numbers, find the k subarrays with the largest aggregates. They may overlap • Example with k=2 -3 7 -12 1 6 -3 5 -2 9 8

  5. Main Idea(Intuition) • Build all sums and insert them into a heap ordered binary tree. There are • Find the k largest using Frederickson’s heap selection algorithm

  6. -12 1 6 -3 5 -2 Example(k=4) 9 6 8 4 3 -3 7 -3 -8 -5 5 -11 -12 1 2

  7. Representing the Sums • The partial sums are grouped by their endpoint in the array • The partial sums ending at index j is

  8. -3 7 -12 1 6 -3 5 -2 (1,4,-7) (2,4,-4) (3,4,-11) (4,4,1)

  9. Construction Equation

  10. -3 7 -12 1 6 -3 5 -2 (1,4,-7) (1,5,-1) (2,5,2) (2,4,-4) (3,4,-11) (3,5,-5) (4,4,1) (4,5,7) (5,5,6)

  11. Main Idea Continued • Represent the suffix sets as heap ordered binary trees • Combine to a single heap by assembling them into one big heap using dummy infinity keys.

  12. The Assembled Heap

  13. The Iheap • It is a heap ordered binary tree • Supports insertions in amortized constant time

  14. T2 T2 T2 5 T3 T3 T3 T3 4 T4 T4 3 T4 T4 Inserting 7 in an Iheap 9 7 5 T1 5 7 4 4 7 3 3 7

  15. Representing suffix sets: • Each set is represent by a tuple • H is an Iheap containg all the elements • is number must be added to all elements. • We get the following construction equation.

  16. 0 3 0 3 0 -4 Example -3 7 -12 {-3} {4,7} {-8,-5,-12}

  17. Pair Construction • Builiding each pair takes amortized constant time.(One insertion into Iheap) • !! But the old version disapears • Solution: Partial Persistence.

  18. Partial Persistence • Driscoll, Sarnak, Sleator, and Tarjan. Making Data Structures Persistent • Allows queris to any version. • Using node copying, the extra cost per update is amortized the cost of copying O(1) original nodes. Query time is the same.

  19. {-3} 0 {4,7} 3 3 0 3 2 {-8,-5,-12} 3 0 -4 0 -4 Before and After

  20. Last Problem • The resulting data structure is no longer a heap ordered binary tree • Fix by incremental construction following Fredericksons algorithm, which works top down

  21. Resume • Build all suffix heaps in O(n) time • Join them into a single heap • Use incremental construction while performing Fredericksons algorithm • All in all O(n+k)

  22. Space reduction • Current algorithm uses O(n+k) time and additional space. The input array is considered read only. • Kadanes algorithm uses O(1) additional space. • We want to reduce the additional space usage to O(k)

  23. Algorithm • Build the k heaps, and find the k largest as before • Repeat on the next k, using the last built heap. The rest can be discarded. • Merge the two sets using selection. • ! The size of the last heap is growing • Only the k best suffixes are usefull. Find these from the last heap and build a new heap with these. • Repeat on next k … k elements k best for 1- k k best for 1-2 k best for k+1-2k

  24. Flash-Back • For k=1 this algorithm and Kadanes algorithm from the introduction are the same.

  25. Higher Dimensions …….. • For an m x n matrix, we get • In general we get Can be reduced to 1D case.

  26. Thank You!

More Related