Honors Track: Competitive Programming & Problem Solving Binary Indexed Trees Christiaan Dirkx.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Fenwick Tree (binary indexed tree).
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
Data Structure (III) GagGuy.
Trees By Charl du Plessis. Contents Basic Terminology Basic Terminology Binary Search Trees Binary Search Trees Interval Trees Interval Trees Binary Indexed.
Data Structure Introduction.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Interval Trees Marco Gallotta. Problem ● Given a collection of items i, each with value V i ● Want to answer many queries of the form: How many items.
Introduction toData structures and Algorithms
School of Computing Clemson University Fall, 2012
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Analysis of Recursive Algorithms
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
Two-Dimensional Arrays
Now with a speaking professor! (hopefully...)
Searching – Linear and Binary Searches
Priority Queues An abstract data type (ADT) Similar to a queue
COSC160: Data Structures Binary Trees
Heapsort CSE 373 Data Structures.
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Spatial Indexing I Point Access Methods.
Priority Queues Chuan-Ming Liu
RECITATION 1 ANALYSIS OF ALGORITHMS
CSCE 3100 Data Structures and Algorithm Analysis
Functions Dr. Sajib Datta
Heaps, Priority Queues, Compression
Top Ten Words that Almost Rhyme with “Peas”
Data Structures: Segment Trees, Fenwick Trees
Fundamentals of Python: From First Programs Through Data Structures
Randomized Algorithms CS648
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
Priority Queues.
CMSC 341 Lecture 14 Priority Queues & Heaps
Priority Queues.
© 2013 Goodrich, Tamassia, Goldwasser
EE 422C Sets.
HKOI 2005 Intermediate Training
Locators 3 a 1 g 4 e 12/29/2018 7:56 AM Locators Locators
Unit-4: Dynamic Programming
Multidimensional Arrays
CSE 373 Data Structures and Algorithms
Multidimensional array
Collections Framework
Priority Queues An abstract data type (ADT) Similar to a queue
Heapsort CSE 373 Data Structures.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Data Structures Lecture 29 Sohail Aslam.
Abstract Data Types, Elementary Data Structures and Arrays
Fundamentals of Python: From First Programs Through Data Structures
Priority Queues.
Hash Tables Buckets/Chaining
Amortized Analysis and Heaps Intro
Priority Queues CSE 373 Data Structures.
Priority Queues & Heaps
Analysis of Recursive Algorithms
Chapter 8 Multidimensional Arrays
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Arrays Imran Rashid CTO at ManiWeber Technologies.
Priority Queue and Heap
Sorting We have actually seen already two efficient ways to sort:
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
Heaps Section 6.4, Pg. 309 (Section 9.1).
Analysis of Recursive Algorithms
Abstract Data Types (ADTs)
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

Honors Track: Competitive Programming & Problem Solving Binary Indexed Trees Christiaan Dirkx

Contents Problem Introduction Formalization Naive Implementation Binary Indexed Trees Binary Intermezzo Operations Extenstions Problems

Why Binary Indexed Trees? Consider the following problem: A list of values A 1 3 -1 4 Sum of all elements in A? Sum of the first 3 elements in A? Sum of element 2 through 7?

Formalisation Abstact Data Type We want to store a collection C of elements, each with an associated index (integer value). Operations Get(S, i): gets the value of element in S with index i Sum(S, i): gets the sum of all elements in S with index ≤ i Update(S, i, v): updates the value of element in S with index i to value v

Formalisation Abstact Data Type We want to store a collection C of elements, each with an associated index (integer value). Operations Get(S, i): gets the value of element in S with index i Sum(S, i): gets the sum of all elements in S with index ≤ i Update(S, i, v): updates the value of element in S with index i to value v Sum(S, a, b)?: gets the sum of all elements in S with index i, a ≤ i ≤ b

Formalisation Abstact Data Type We want to store a collection C of elements, each with an associated index (integer value). Operations Get(S, i): gets the value of element in S with index i Sum(S, i): gets the sum of all elements in S with index ≤ i Update(S, i, v): updates the value of element in S with index i to value v Sum(S, a, b): Sum(S, b) – Sum(S, a - 1)

Implementation How can we implement this ADT? Heaps! (Not quite)

Implementation A: 1 3 -1 4 1 4 3 3 7 8 9 Aggregation array

Implementation A: Can we improve? Aggregation array 1 3 -1 4 1 4 3 3 7 1 3 -1 4 1 4 3 3 7 8 9 Aggregation array Complexity? Get(S, i): O(1) Can we improve? Sum(S, i): O(1) Update(S, i, v): O(n)

Implementation A: Can we improve? Aggregation array 1 3 -1 4 1 4 3 3 7 1 3 -1 4 1 4 3 3 7 8 9 Aggregation array Complexity? Get(S, i): O(log n) Can we improve? Sum(S, i): O(log n) Update(S, i): O(log n)

Binary Indexed Tree ? Change the recurrence relation The key idea: Aggregation array: Sum(S, 0): Get(S, 0) Sum(S, i): Sum(S, i-1, i) + Sum(S, i-1)

Binary Indexed Tree ? ? Change the recurrence relation The key idea: Aggregation array: Sum(S, 0): Get(S, 0) Sum(S, i): Get(S, i) + Sum(S, i-1) ? Binary indexed tree: O(log n)

Binary recurrence Sum(S, 13): Sum(S, 12, 13) + Sum(S, 12) Consider the number 13 Sum(S, 13): Sum(S, 12, 13) + Sum(S, 12) Sum(S, 12): Sum(S, 8, 12) + Sum(S, 8) Sum(S, 8): Sum(S, 0, 8) + Sum(S, 0) Why? In binary: 13 1101 12 1100 8 1000

Binary recurrence Sum(S, 1101): Sum(S, 1100, 1101) + Sum(S, 1100) Consider the number 13 Sum(S, 1101): Sum(S, 1100, 1101) + Sum(S, 1100) Sum(S, 1100): Sum(S, 1000, 1100) + Sum(S, 1000) Sum(S, 1000): Sum(S, 0000, 1000) + Sum(S, 0000) O(log n)

Visualization 0000 0001 0010 0100 1000 0011 0101 0110 0111 A: 1 3 -1 4

Visualization 1 3 9 3 4 1 A: 1 3 -1 4 1 4 3 3 7 8 9

Visualization 1 3 9 3 4 1 A: 1 3 -1 4 1 4 3 3 7 8 9

Visualization T: A: But how should we encode the index? 1 3 3 4 1 9 1 1 3 3 4 1 9 But how should we encode the index? A: 1 3 -1 4 1 4 3 3 7 8 9

We want to iteratively get the least significant bit Binary Intermezzo Binary number: 1 0 1 0 1 1 0 1 most significant bit least significant bit 13 1 1 0 1 12 1 1 0 0 8 1 0 0 0 We want to iteratively get the least significant bit

Binary Intermezzo i : 0 1 0 0 1 1 0 0 ~i : 1 0 1 1 0 0 1 1 -i : Next index: i : 0 1 0 0 1 1 0 0 ~i : 1 0 1 1 0 0 1 1 -i : 1 0 1 1 0 1 0 0 i & -i : 0 0 0 0 0 1 0 0 i - (i & -i) : 0 1 0 0 1 0 0 0

Sum(S, i) A: T: Sum(S, i) Sum(S, 7) 1 3 -1 4 1 1 1 3 3 4 1 9 sum: 0 1 3 -1 4 1 1 T: 1 3 3 4 1 9 Sum(S, i) Sum(S, 7) int sum(int idx) { int sum = 0; while (idx > 0) { sum += T[idx]; idx -= (idx & -idx); } return sum; sum: 0 idx: 7 (0111)

Sum(S, i) A: T: Sum(S, i) Sum(S, 7) 1 3 -1 4 1 1 1 3 3 4 1 9 sum: 1 1 3 -1 4 1 1 T: 1 3 3 4 1 9 Sum(S, i) Sum(S, 7) int sum(int idx) { int sum = 0; while (idx > 0) { sum += T[idx]; idx -= (idx & -idx); } return sum; sum: 1 idx: 7 (0111)

Sum(S, i) A: T: Sum(S, i) Sum(S, 7) 1 3 -1 4 1 1 1 3 3 4 1 9 sum: 5 1 3 -1 4 1 1 T: 1 3 3 4 1 9 Sum(S, i) Sum(S, 7) int sum(int idx) { int sum = 0; while (idx > 0) { sum += T[idx]; idx -= (idx & -idx); } return sum; sum: 5 idx: 6 (0110)

Sum(S, i) A: T: Sum(S, i) Sum(S, 7) 1 3 -1 4 1 1 1 3 3 4 1 9 sum: 8 1 3 -1 4 1 1 T: 1 3 3 4 1 9 Sum(S, i) Sum(S, 7) int sum(int idx) { int sum = 0; while (idx > 0) { sum += T[idx]; idx -= (idx & -idx); } return sum; sum: 8 idx: 4 (0100)

Sum(S, i) A: T: Sum(S, i) Sum(S, 7) O(log n) 1 3 -1 4 1 1 1 3 3 4 1 9 1 3 -1 4 1 1 T: 1 3 3 4 1 9 Sum(S, i) Sum(S, 7) int sum(int idx) { int sum = 0; while (idx > 0) { sum += T[idx]; idx -= (idx & -idx); } return sum; sum: 8 idx: 0 (0000) O(log n)

Get(S, i) Get(S, i): Sum(S, i) - Sum(S, i-1) Keep a reference to A int get(int idx) { int sum = T[idx]; if (idx > 0) { int z = idx - (idx & -idx); idx--; while (idx != z) { sum -= tree[idx]; idx -= (idx & -idx); } return sum; O(log n)

Update(S, i,v) 1 3 9 3 4 1 A: 1 3 -1 4 1 4 3 3 7 8 9

Update(S, i,v) 1 3 9 3 4 1 A: 1 3 -1 2 4 1 4 3 5 9 10 11

Update(S, i,v) 1 3 9 3 2 4 1 A: 1 3 -1 2 4 1 4 3 5 9 10 11

Update(S, i,v) 1 3 9 3 2 6 1 A: 1 3 -1 2 4 1 4 3 5 9 10 11

Update(S, i,v) A: But how should we encode the index? 1 3 -1 2 4 1 4 3 1 3 11 3 2 6 But how should we encode the index? 1 A: 1 3 -1 2 4 1 4 3 5 9 10 11

Update(S, i, v) And that’s all! Update(S, i, v) O(log n) void update(int idx ,int val) { while (idx < |T|) { T[idx] += val; idx += (idx & -idx); } O(log n) And that’s all!

Extensions O( 2 𝑑 log 𝑑 𝑛 ) Operations for scaling all values Operations for finding the index of a particular sum Multidimensional Binary Indexed Trees O( 2 𝑑 log 𝑑 𝑛 ) void update(int idx ,int val) { while (idx <= MAX_VAL) { T[idx] += val; idx += (idx & -idx); }

Extensions O( 2 𝑑 log 𝑑 𝑛 ) Operations for scaling all values Operations for finding the index of a particular sum Multidimensional Binary Indexed Trees O( 2 𝑑 log 𝑑 𝑛 ) void updatey(int x, int y, int val) { while (y <= MAX_Y) { T[x][y] += val; y += (y& -y); } void update(int x, int y, int val) { while (x <= MAX_X) { updatey(x,y,val); x += (x & -x); }

Problems EAPC10I – Imagine BAPC14 – Excellent Engineers …

EAPC10I A square board of 1024x1024 square cm Stickers of 1x1 of party A and B in alternating pattern A B A B B A B A B Query: how many stickers of each party in (x1,y1,x2,y2)? Update: a sticker for party A or B was placed at (x,y)

Solution Using a 2-dimensional Binary Indexed Tree How do we store stickers? We keep track of the sum of stickers for A Stickers for B = total - #A How do we query a rectangle (x1,y1,x2,y2)? Sum(x1,y1,x2,y2) = Sum(x2, y2) - Sum(x1-1, y2) - Sum(x2, y -1) + Sum(x1-1, y1-1)

Questions?