Considering input series: { 4 , -10 , 3 , 100 , -20 , 1 }
Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y }
A node contains-
[ START & END is a node's segment limit ]
Prefix is the maximum sum starting at START, end can be anywhere. There are two possibilities of the maximum. One, node's leftChild's prefix or two, adding leftChild's sum + rightChild's prefix. (which will make the prefix contiguous)
Suffix is the maximum sum ending at END, start can be anywhere.
There's two possibility of the maximum. One, node's rightChild's already calculated suffix or two, add rightChild's sum + leftChild's suffix (which will make the suffix contiguous).
Sum: leftChild's sum + rightChild's sum.
MAX Maximum of -
Notice that, in last point, we can't add leftChild's Max + rightChild's Max because in some cases that would result in a non-contiguous MAX.
Considering sub interval 4 -10 3
[ START & END is a node's segment limit ]
Prefix is the maximum sum starting at START, end can be anywhere. There are two possibilities of the maximum. One, node's leftChild's prefix or two, adding leftChild's sum + rightChild's prefix. (which will make the prefix contiguous)
Suffix is the maximum sum ending at END, start can be anywhere.
There's two possibility of the maximum. One, node's rightChild's already calculated suffix or two, add rightChild's sum + leftChild's suffix (which will make the suffix contiguous).
Sum: leftChild's sum + rightChild's sum.
MAX Maximum of -
- prefix (result is in the node, starts from START but doesn't end in END )
- suffix (result is in the node, doesn't start from START but surely ends in END )
- leftChild's max ( result is in left child completely )
- rightChild's max (result is in right child completely )
- leftChild's suffix + rightChild's prefix ( result is in between leftChild & rightChild )
Notice that, in last point, we can't add leftChild's Max + rightChild's Max because in some cases that would result in a non-contiguous MAX.
Considering sub interval 4 -10 3
- prefix = 4
- suffix = 3
- sum = -3
- best = 4
- prefix =100 leftChild's prefix
- suffix = 81 rightChild's sum + leftChild's suiffx
- sum =81
- best = 100 prefix from above
finally, Query(x,y) = result.MAX ;
you saved my day with clear explanation of prefix and suffix. _/\_
ReplyDeletethanks buddy!
ReplyDeleteyou have made everything clear just in one post which I wasn't unable to find in about 10 different posts.
:D
There is an error I guess,
ReplyDeletein third line of image
tree[node].suf = max(tree[RIGHT].suf , tree[LEFT].suf+tree[RIGHT].sum)
yes you are right
DeleteIn the picture , in node 1-2 , shouldn't the value be 4,-6,-6,4 ?
ReplyDeleteyes you are right
DeleteThis comment has been removed by the author.
Deletetis is good stuff
ReplyDeleteCan we do this using Binary Indexed Trees as well?
ReplyDeleteAmazing blog on this topic...
ReplyDeleteThanks man
In Image 1, Node 1 should store sum of range [1,6] which should be 78, But it is written -78.
ReplyDeleteIn Image 1, Node 1 should store prefix sum of range [1,6] which should be 97, But it is written 78.
ReplyDeletePlease correct me if I am wrong.
Thanks for this
ReplyDeletethanks bro
ReplyDelete