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

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

Embed Size (px)

Citation preview

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

A Linear Time Algorithm for the k Maximum Sums Problem

By Gerth S. Brodal and

Allan G. Jørgensen

Page 2: 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

7 -12-3 1 6 -3 5 -2

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

7 4 9 78 -4

Kadanes Algorithm

• Scan array and update• Best current suffix• Best sum so far

7 -121 1 6 -3 5 -2

1 1

1 8 9

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

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-3 1 6 -3 5 -2

9

8

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

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

2( )2

nn O n

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

Example(k=4)

9

86

4

-8

-12

73

-11

-3

-3

-5 5

21

-12 1 6 -3 5 -2

( )O k

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

Representing the Sums

• The partial sums are grouped by their endpoint in the array

• The partial sums ending at index j is

j

is

j sAsumjisumjiQ 1|),,(suf

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

7 -12-3 1 6 -3 5 -2

4sufQ

(1,4,-7)

(2,4,-4)

(3,4,-11)

(4,4,1)

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

Construction Equation

}),1,(|])[,,{(

])}[,,{(1

suf

suf

j

j

QsjijAsji

jAjjQ

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

7 -12-3 1 6 -3 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)

45 from ngConstructi sufsuf QQ

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

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.

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

The Assembled Heap

1sufH

5sufH4

sufH3sufH

2sufH

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

The Iheap

• It is a heap ordered binary tree

• Supports insertions in amortized constant time

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

Inserting 7 in an Iheap

9

3

4

5

7

7

3

7

4

5

7T1

T2

T3

T4

T4

T3

T2

3

4

5

T3

T4

T4

T3

T2

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

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.

}{],1[, suf

1jsuf1 j

jjj HjAH

jQsuf jj H suf,

j

{},0, 0suf0 H

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

Example

31 0

7 -12-3

0

342

0

3

-4

83

)3(3Set

)0(-Insert

01

0

δ

)4(7Set

)3(Insert

12

1

)8(12Set

)4(Insert

23

2

{-3}

{4,7}

{-8,-5,-12}

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

Pair Construction

• Builiding each pair takes amortized constant time.(One insertion into Iheap)

• !! But the old version disapears

• Solution: Partial Persistence.

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

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.

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

Before and After

2

3

22suf , 4H

0

11suf , 3H

3

-4

33suf , 8H

0

0

3

0

3

-4

{-3}

{4,7}

{-8,-5,-12}

1 3

2 4

3 8

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

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

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

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)

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

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)

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

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

kH k suf kH k 22suf

k best for 1- k k best for k+1-2kk best for 1-2

kH k 22suf kHk

2suf

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

Flash-Back

• For k=1 this algorithm and Kadanes algorithm from the introduction are the same.

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

Higher Dimensions

Can be reduced to 1D case.

……..

space )( time,)( 2 knOknmO

space )( time,)(1

12

21 knOknnO

d

ii

d

ii

• For an m x n matrix, we get

• In general we get

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