1. Trang chủ
  2. » Công Nghệ Thông Tin

Fenwick_tree_cay_chi_so_nhi_phan.pdf

162 3 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Fenwick Tree (Binary Indexed Tree)
Tác giả William Fiset
Thể loại essay
Định dạng
Số trang 162
Dung lượng 2,33 MB

Nội dung

cấu trúc cây chỉ số nhị phân, tài liệu về cây chỉ số nhị phân

Trang 1

Fenwick Tree

William Fiset

(Binary Indexed Tree)

Trang 2

• Discussion & Examples

• Data structure motivation

• What is a Fenwick tree?

Trang 3

Fenwick 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 4

Given an array of integer values compute

the range sum between index [i, j).

Trang 5

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 =

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 6

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 =

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 7

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 =

P =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 8

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 9

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 10

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 11

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 12

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 13

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 14

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 15

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 16

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 17

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 18

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 =

Let P be an array containing all

the prefix sums of A.

Fenwick Tree

Motivation

Trang 19

Given an array of integer values compute

the range sum between index [i, j).

Trang 20

Given 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 21

Given 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 22

Question: What about if we want to update

our initial array with some new value?

Trang 23

Question: What about if we want to update

our initial array with some new value?

Trang 24

Question: What about if we want to update

our initial array with some new value?

Trang 25

What 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 26

Complexity

Trang 27

Fenwick Tree Range Queries

William Fiset

Trang 28

Unlike a regular array, in

a Fenwick tree a specific cell is responsible for other cells as well.

Trang 29

Unlike a regular array, in

a Fenwick tree a specific cell is responsible for other cells as well.

The position of the least

Trang 30

Unlike 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 31

Index 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 32

Index 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 33

for 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 34

for that cell NOT value

Numbers with their least significant bit in the second position have a range of two.

Trang 35

for that cell NOT value

Numbers with their least significant bit in the third position have a range of four.

Trang 36

for that cell NOT value

Numbers with their least significant bit in the fourth position have a range of eight.

Trang 37

for that cell NOT value

Numbers with their least significant bit in the fifth position have a range

of sixteen.

Trang 39

Range 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 42

Range 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 43

Range 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 46

Range 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 47

Range 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 52

Range 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 57

Range 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 58

Range 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 59

Range 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 60

Hence, 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 61

Range 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 62

next video: Fenwick Tree point updates!

Implementation source code and tests can

all be found at the following link:

github.com/williamfiset/data-structures

Trang 63

Fenwick Tree Point Updates

William Fiset

Trang 64

Last Video: Fenwick Tree Range Queries

Trang 66

Point Updates

Recall that with range queries

we cascaded down from the current index by continuously

Trang 67

Recall that with range queries

we cascaded down from the current index by continuously

Trang 68

Recall that with range queries

we cascaded down from the current index by continuously

Trang 69

Recall that with range queries

we cascaded down from the current index by continuously

Trang 70

Point 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 71

Point 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 72

Point 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 73

Point 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 74

Point 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 75

Point 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 76

Point Updates

If we add x to position 6 in the Fenwick tree which cells

do we also need to modify?

Trang 77

do we also need to modify?

6 = 01102

Trang 78

do we also need to modify?

8 = 10002

6 = 01102, 01102+00102 = 10002

Trang 79

do we also need to modify?

8 = 10002, 10002+10002 = 100002

16 = 100002

6 = 01102, 01102+00102 = 10002

Trang 80

do we also need to modify?

8 = 10002, 10002+10002 = 100002

16 = 100002

6 = 01102, 01102+00102 = 10002

Trang 81

do we also need to modify?

Trang 82

Point 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 83

Fenwick 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 84

Fenwick Tree Construction

William Fiset

Trang 85

Naive 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 86

Input values we wish to turn into a legitimate

Trang 87

Idea: 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 88

Let 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 89

i = 1 = 00012

Trang 90

i = 1 = 00012

Trang 91

i = 2 = 00102

Trang 92

i = 2 = 00102

Trang 93

i = 3 = 00112

Trang 94

i = 3 = 00112

Trang 95

i = 4 = 01002

Trang 96

i = 4 = 01002

Trang 97

i = 5 = 01012

Trang 98

i = 5 = 01012

Trang 99

i = 6 = 01102

Trang 100

i = 6 = 01102

Trang 101

i = 7 = 01112

Trang 102

i = 7 = 01112

Trang 103

i = 8 = 10002

Ignore updating j if index is out of bounds

Trang 104

i = 9 = 10012

Trang 105

i = 9 = 10012

Trang 106

i = 10 = 10102

Trang 107

i = 10 = 10102

Trang 108

i = 11 = 10112

Trang 109

i = 11 = 10112

Trang 110

i = 12 = 11002

Ignore updating j if index is out of bounds

Trang 111

required.

Trang 113

Fenwick 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 114

Fenwick Tree Source Code

William Fiset

Trang 115

Source 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 117

Fenwick Tree Range Update Visualization

Trang 131

Idea: Place an edge between values

who only differ by their Least Significant Bit (LSB) in binary.

0

Least Significant Bit

Trang 132

Least significant bits

Least Significant Bit

Idea: Place an edge between values

who only differ by their Least Significant Bit (LSB) in binary.

Trang 133

In 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 134

Least 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 135

Least 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 136

Least 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 137

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 141

Fenwick Tree Visualization

Trang 142

Fenwick Tree Visualization

Trang 143

Fenwick Tree Visualization

Trang 145

16 8

12

14 15

Trang 146

16 8

12

14 15

13 11

Fenwick Tree Visualization

Trang 147

16 8

12

14 15

Fenwick Tree Visualization

Trang 148

16 8

12

14 15

Fenwick Tree Visualization

Trang 149

16 8

12

14 15

Fenwick Tree Visualization

Trang 150

16 8

12

14 15

13

10 11

9 7

Fenwick Tree Visualization

Trang 151

16 8

12

14 15

9 6

7

Fenwick Tree Visualization

Trang 152

16 8

12

14 15

4

6 7

Fenwick Tree Visualization

Trang 153

16 8

12

14 15

4

6 7

Fenwick Tree Visualization

Trang 154

16 8

12

14 15 13

10 11 9

4

6 7

Trang 155

16 8

12

14 15 13

10 11 9

4

6 7

3

Fenwick Tree Visualization

Trang 156

16 8

12

14 15 13

10 11 9

4

6 7

Fenwick Tree Visualization

Trang 157

16 8

12

14 15 13

10 11 9

4

6 7

Fenwick Tree Visualization

Trang 158

16 8

12

14 15 13

10 11 9

4

6 7

Fenwick Tree Visualization

Trang 159

16 8

12

14 15 13

10 11 9

4

6 7

Fenwick Tree Visualization

Trang 160

Although 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 161

The 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 162

Odd 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

Ngày đăng: 04/02/2024, 12:45

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN