Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Linear Time Algorithm for the k Maximum Sums Problem By Gerth S. Brodal and Allan G. Jørgensen.

Similar presentations


Presentation on theme: "A Linear Time Algorithm for the k Maximum Sums Problem By Gerth S. Brodal and Allan G. Jørgensen."— 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 7-12-316 5-2

3 74978-4 Kadanes Algorithm Scan array and update Best current suffix Best sum so far 7-12116-35-2 11 189

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 7-12-316 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 Example( k=4 ) 9 86 4 -8 -12 7 3 -11 -3 -55 21 -1216-35-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 7-12-316 5-2 (1,4,-7) (2,4,-4) (3,4,-11) (4,4,1)

9 Construction Equation

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

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 Inserting 7 in an Iheap 9 3 4 5 7 7 3 7 4 5 7 T1 T2T3T4 T3T2 3 4 5 T3 T4 T3 T2

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 Example 0 7-12-3 0 3 0 3 -4 {-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 Before and After 2 3 0 3 -4 0 0 3 0 3 {-3} {4,7} {-8,-5,-12}

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 k+1-2k k best for 1-2

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

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

26


Download ppt "A Linear Time Algorithm for the k Maximum Sums Problem By Gerth S. Brodal and Allan G. Jørgensen."

Similar presentations


Ads by Google