cấu trúc cây chỉ số nhị phân, tài liệu về cây chỉ số nhị phân
Trang 1Fenwick Tree
William Fiset
(Binary Indexed Tree)
Trang 2• Discussion & Examples
• Data structure motivation
• What is a Fenwick tree?
Trang 3Fenwick Tree
Motivation
Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Trang 4Given an array of integer values compute
the range sum between index [i, j).
Trang 5Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Sum of A from [2,7) = 6 + 1 + 0 + -4 + 11 = 14 Sum of A from [0,4) = 5 + -3 + 6 + 1 = 9
Fenwick Tree
Motivation
Trang 6Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Sum of A from [2,7) = 6 + 1 + 0 + -4 + 11 = 14 Sum of A from [0,4) = 5 + -3 + 6 + 1 = 9
Sum of A from [7,8) = 6 = 6
Fenwick Tree
Motivation
Trang 7Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
P =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 8Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 9Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 10Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 11Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 12Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 13Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 14Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 15Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 16Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 17Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 18Given an array of integer values compute
the range sum between index [i, j).
5 -3 6 1 0 -4 11 6 2 7
A =
Let P be an array containing all
the prefix sums of A.
Fenwick Tree
Motivation
Trang 19Given an array of integer values compute
the range sum between index [i, j).
Trang 20Given an array of integer values compute
the range sum between index [i, j).
0 5 2 8 9 9 5 16 22 24 31
Fenwick Tree
Motivation
Trang 21Given an array of integer values compute
the range sum between index [i, j).
Sum of A from [7,8) = P[8] - P[7] = 22 - 16 = 6
Fenwick Tree
Motivation
Trang 22Question: What about if we want to update
our initial array with some new value?
Trang 23Question: What about if we want to update
our initial array with some new value?
Trang 24Question: What about if we want to update
our initial array with some new value?
Trang 25What is a Fenwick Tree?
A Fenwick Tree (also called Binary
Indexed Tree) is a data structure that supports sum range queries as well as setting values in a static array and getting the value of the prefix sum up
some index efficiently.
Trang 26Complexity
Trang 27Fenwick Tree Range Queries
William Fiset
Trang 28Unlike a regular array, in
a Fenwick tree a specific cell is responsible for other cells as well.
Trang 29Unlike a regular array, in
a Fenwick tree a specific cell is responsible for other cells as well.
The position of the least
Trang 30Unlike a regular array, in
a Fenwick tree a specific cell is responsible for other cells as well.
Index 12 in binary is: 1 1 002
LSB is at position 3, so this index is responsible for 23-1 = 4
cells below itself.
The position of the least
Trang 31Index 10 in binary is: 10 1 02
LSB is at position 2, so this index is responsible for 22-1 = 2
cells below itself.
Unlike a regular array, in
a Fenwick tree a specific cell is responsible for other cells as well.
The position of the least
Trang 32Index 11 in binary is: 101 12
LSB is at position 1, so this index is responsible for 21-1 = 1
cell (itself).
Unlike a regular array, in
a Fenwick tree a specific cell is responsible for other cells as well.
The position of the least
Trang 33for that cell NOT value
All odd numbers have a their first least significant bit set
in the ones position, so they are only responsible for themselves.
Trang 34for that cell NOT value
Numbers with their least significant bit in the second position have a range of two.
Trang 35for that cell NOT value
Numbers with their least significant bit in the third position have a range of four.
Trang 36for that cell NOT value
Numbers with their least significant bit in the fourth position have a range of eight.
Trang 37for that cell NOT value
Numbers with their least significant bit in the fifth position have a range
of sixteen.
Trang 39Range Queries
Idea: Suppose you want to find
the prefix sum of [1, i], then
you start at i and cascade downwards until you reach zero adding the value at each of the indices you encounter.
In a Fenwick tree we may compute the prefix sum up to
a certain index, which ultimately lets us perform
range sum queries
Trang 42Range Queries
sum = A[7] + A[6]
In a Fenwick tree we may compute the prefix sum up to
a certain index, which ultimately lets us perform
range sum queries
Trang 43Range Queries
sum = A[7] + A[6] + A[4]
In a Fenwick tree we may compute the prefix sum up to
a certain index, which ultimately lets us perform
range sum queries
Trang 46Range Queries
sum = A[11] + A[10]
In a Fenwick tree we may compute the prefix sum up to
a certain index, which ultimately lets us perform
range sum queries
Trang 47Range Queries
sum = A[11] + A[10] + A[8]
In a Fenwick tree we may compute the prefix sum up to
a certain index, which ultimately lets us perform
range sum queries
Trang 52Range Queries
First we compute the prefix sum of [1, 15], then we will compute the prefix sum of [1,11) and get the difference.
Not inclusive! We want the value at position 11.
Trang 57Range Queries
First we compute the prefix sum of [1, 15], then we will compute the prefix sum of [1,11) and get the difference.
Compute the interval sum
between [11, 15].
Sum of [1,15] = A[15]+A[14]+A[12]+A[8]Sum of [1,11) = A[10]
Trang 58Range Queries
Sum of [1,15] = A[15]+A[14]+A[12]+A[8]Sum of [1,11) = A[10]+A[8]
Compute the interval sum
between [11, 15].
Trang 59Range Queries
Sum of [1,15] = A[15]+A[14]+A[12]+A[8]Sum of [1,11) = A[10]+A[8]
Compute the interval sum
between [11, 15].
Trang 60Hence, it’s easy to see that
in the worst case a range query might make us have to
do two queries that cost
log2(n) operations.
Trang 61Range query algorithm
function prefixSum(i):
sum := 0 while i != 0:
sum = sum + tree[i]
i = i - LSB (i) return sum
To do a range query from [i,j] both
inclusive a Fenwick tree of size N:
Trang 62next video: Fenwick Tree point updates!
Implementation source code and tests can
all be found at the following link:
github.com/williamfiset/data-structures
Trang 63Fenwick Tree Point Updates
William Fiset
Trang 64Last Video: Fenwick Tree Range Queries
Trang 66Point Updates
Recall that with range queries
we cascaded down from the current index by continuously
Trang 67Recall that with range queries
we cascaded down from the current index by continuously
Trang 68Recall that with range queries
we cascaded down from the current index by continuously
Trang 69Recall that with range queries
we cascaded down from the current index by continuously
Trang 70Point Updates
Point updates are the opposite of this, we want to
add the LSB to propagate the
value up to the cells responsible for us.
Trang 71Point Updates
Point updates are the opposite of this, we want to
add the LSB to propagate the
value up to the cells responsible for us.
Trang 72Point Updates
Point updates are the opposite of this, we want to
add the LSB to propagate the
value up to the cells responsible for us.
Trang 73Point Updates
Point updates are the opposite of this, we want to
add the LSB to propagate the
value up to the cells responsible for us.
Trang 74Point Updates
Point updates are the opposite of this, we want to
add the LSB to propagate the
value up to the cells responsible for us.
Trang 75Point Updates
Point updates are the opposite of this, we want to
add the LSB to propagate the
value up to the cells responsible for us.
Trang 76Point Updates
If we add x to position 6 in the Fenwick tree which cells
do we also need to modify?
Trang 77do we also need to modify?
6 = 01102
Trang 78do we also need to modify?
8 = 10002
6 = 01102, 01102+00102 = 10002
Trang 79do we also need to modify?
8 = 10002, 10002+10002 = 100002
16 = 100002
6 = 01102, 01102+00102 = 10002
Trang 80do we also need to modify?
8 = 10002, 10002+10002 = 100002
16 = 100002
6 = 01102, 01102+00102 = 10002
Trang 81do we also need to modify?
Trang 82Point update algorithm
function add(i, x):
while i < N:
tree[i] = tree[i] + x
i = i + LSB (i)
To update the cell at index i in
the a Fenwick tree of size N:
Where LSB returns the value of the
least significant bit For example:
LSB (12) = 4 because 1210 = 11002 and the least significant bit of 11002 is 1002,
or 4 in base ten
Trang 83Fenwick Tree construction
follows in next video
Implementation source code and tests can
all be found at the following link:
github.com/williamfiset/data-structures
Trang 84Fenwick Tree Construction
William Fiset
Trang 85Naive Construction
Let A be an array of values For each
element in A at index i do a point update
on the Fenwick tree with a value of A[i]
There are n elements and each point
update takes O(log(n)) for a total of
O(nlog(n)) , can we do better?
Trang 86Input values we wish to turn into a legitimate
Trang 87Idea: Add the value in
the current cell to the
immediate cell that is responsible for us This resembles what we did for point updates but only one cell at a time.
This will make the
‘cascading’ effect in range queries possible
by propagating the value
in each cell throughout
the tree.
Trang 88Let i be the current index
The immediate cell above us
is at position j given by:
j := i + LSB(i)
Where LSB is the Least Significant Bit of i
Trang 89i = 1 = 00012
Trang 90i = 1 = 00012
Trang 91i = 2 = 00102
Trang 92i = 2 = 00102
Trang 93i = 3 = 00112
Trang 94i = 3 = 00112
Trang 95i = 4 = 01002
Trang 96i = 4 = 01002
Trang 97i = 5 = 01012
Trang 98i = 5 = 01012
Trang 99i = 6 = 01102
Trang 100i = 6 = 01102
Trang 101i = 7 = 01112
Trang 102i = 7 = 01112
Trang 103i = 8 = 10002
Ignore updating j if index is out of bounds
Trang 104i = 9 = 10012
Trang 105i = 9 = 10012
Trang 106i = 10 = 10102
Trang 107i = 10 = 10102
Trang 108i = 11 = 10112
Trang 109i = 11 = 10112
Trang 110i = 12 = 11002
Ignore updating j if index is out of bounds
Trang 111required.
Trang 113Fenwick Tree source code
follows in next video!
Implementation source code and tests can
all be found at the following link:
github.com/williamfiset/data-structures
Trang 114Fenwick Tree Source Code
William Fiset
Trang 115Source Code Link
NOTE: Make sure you have understood the previous video sections explaining how a Fenwick Tree works before continuing!
Implementation source code and tests can all be found
at the following link:
github.com/williamfiset/data-structures
Trang 117Fenwick Tree Range Update Visualization
Trang 131Idea: Place an edge between values
who only differ by their Least Significant Bit (LSB) in binary.
0
Least Significant Bit
Trang 132Least significant bits
Least Significant Bit
Idea: Place an edge between values
who only differ by their Least Significant Bit (LSB) in binary.
Trang 133In other words: Place an
edge between nodes i and i
without its LSB.
Least Significant Bit
Idea: Place an edge between values
who only differ by their Least Significant Bit (LSB) in binary.
Trang 134Least Significant Bit
Idea: Place an edge between values
who only differ by their Least Significant Bit (LSB) in binary.
In other words: Place an
edge between nodes i and i
without its LSB.
Trang 135Least Significant Bit
Idea: Place an edge between values
who only differ by their Least Significant Bit (LSB) in binary.
In other words: Place an
edge between nodes i and i
without its LSB.
Trang 136Least Significant Bit
Idea: Place an edge between values
who only differ by their Least Significant Bit (LSB) in binary.
In other words: Place an
edge between nodes i and i
without its LSB.
Trang 137Idea: Place an edge between values
who only differ by their Least Significant Bit (LSB) in binary.
In other words: Place an
edge between nodes i and i
without its LSB.
Trang 141Fenwick Tree Visualization
Trang 142Fenwick Tree Visualization
Trang 143Fenwick Tree Visualization
Trang 14516 8
12
14 15
Trang 14616 8
12
14 15
13 11
Fenwick Tree Visualization
Trang 14716 8
12
14 15
Fenwick Tree Visualization
Trang 14816 8
12
14 15
Fenwick Tree Visualization
Trang 14916 8
12
14 15
Fenwick Tree Visualization
Trang 15016 8
12
14 15
13
10 11
9 7
Fenwick Tree Visualization
Trang 15116 8
12
14 15
9 6
7
Fenwick Tree Visualization
Trang 15216 8
12
14 15
4
6 7
Fenwick Tree Visualization
Trang 15316 8
12
14 15
4
6 7
Fenwick Tree Visualization
Trang 15416 8
12
14 15 13
10 11 9
4
6 7
Trang 15516 8
12
14 15 13
10 11 9
4
6 7
3
Fenwick Tree Visualization
Trang 15616 8
12
14 15 13
10 11 9
4
6 7
Fenwick Tree Visualization
Trang 15716 8
12
14 15 13
10 11 9
4
6 7
Fenwick Tree Visualization
Trang 15816 8
12
14 15 13
10 11 9
4
6 7
Fenwick Tree Visualization
Trang 15916 8
12
14 15 13
10 11 9
4
6 7
Fenwick Tree Visualization
Trang 160Although the values contained by different Fenwick trees may differ, the actual structure of the tree does not depend on the values it is holding.
Fenwick Tree Analysis
Trang 161The furthest node from the root will
always be at most log2(n) nodes deep This happens when all the trailing bits are 1’s These numbers are of the form
Trang 162Odd nodes are always leaves in our Fenwick Tree
because their LSB is always a 1.
0
16 8
12
14 15 13
10 11 9
4
6 7 5
2 3 1
Fenwick Tree Analysis