Fenwick Trees Dr. Mattox Beckman University of Illinois at - - PowerPoint PPT Presentation

fenwick trees
SMART_READER_LITE
LIVE PREVIEW

Fenwick Trees Dr. Mattox Beckman University of Illinois at - - PowerPoint PPT Presentation

Introduction Navigation Fenwick Trees Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Introduction Navigation Objectives Your Objectives: Describe and implement a Fenwick Tree Compare a


slide-1
SLIDE 1

Introduction Navigation

Fenwick Trees

  • Dr. Mattox Beckman

University of Illinois at Urbana-Champaign Department of Computer Science

slide-2
SLIDE 2

Introduction Navigation

Objectives

Your Objectives: ◮ Describe and implement a Fenwick Tree ◮ Compare a Fenwick Tree to a Segment Tree

slide-3
SLIDE 3

Introduction Navigation

Motivating Example

Exam scores = {2, 4, 5, 5, 6, 6, 6, 7, 7, 8, 9}

slide-4
SLIDE 4

Introduction Navigation

Motivating Example

Exam scores = {2, 4, 5, 5, 6, 6, 6, 7, 7, 8, 9} index 1 2 3 4 5 6 7 8 9 10 value 1 1 2 3 2 1 1

slide-5
SLIDE 5

Introduction Navigation

Motivating Example

Exam scores = {2, 4, 5, 5, 6, 6, 6, 7, 7, 8, 9} index 1 2 3 4 5 6 7 8 9 10 value 1 1 2 3 2 1 1 cumulative 1 1 2 4 7 9 10 11 11

slide-6
SLIDE 6

Introduction Navigation

Motivating Example

Exam scores = {2, 3, 4, 5, 5, 6, 6, 6, 7, 7, 8, 9} index 1 2 3 4 5 6 7 8 9 10 value 1 1 1 2 3 2 1 1 cumulative 1 2 3 5 8 10 11 12 12

slide-7
SLIDE 7

Introduction Navigation

Fenwick Tree

value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10

slide-8
SLIDE 8

Introduction Navigation

Fenwick Tree

value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010

slide-9
SLIDE 9

Introduction Navigation

Fenwick Tree

ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010

slide-10
SLIDE 10

Introduction Navigation

Fenwick Tree

ft 2 1 5 1 ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010

slide-11
SLIDE 11

Introduction Navigation

Fenwick Tree

ft 3 2 ft 2 1 5 1 ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010

slide-12
SLIDE 12

Introduction Navigation

Fenwick Tree

ft 4 10 ft 3 2 ft 2 1 5 1 ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010

slide-13
SLIDE 13

Introduction Navigation

Queries

ft 4 10 ft 3 2 ft 2 1 5 1 ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 ◮ To Query sum for position n, fjrst read n. ◮ Then, subtract the lowest order bit, and repeat until n = 0. E.g.: 5 = 101 4 = 100

ft ft

E.g.: 10 = 1010 8 = 1000

ft ft

Update 5: Visit 5=101 6=110 8=1000 LSOne(n) = n & -n

slide-14
SLIDE 14

Introduction Navigation

Queries

ft 4 10 ft 3 2 ft 2 1 5 1 ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 ◮ To Query sum for position n, fjrst read n. ◮ Then, subtract the lowest order bit, and repeat until n = 0. ◮ E.g.: 5 = 101 → 4 = 100 → 0

◮ ft(5) + ft(4) = 2 + 2 = 4

E.g.: 10 = 1010 8 = 1000

ft ft

Update 5: Visit 5=101 6=110 8=1000 LSOne(n) = n & -n

slide-15
SLIDE 15

Introduction Navigation

Queries

ft 4 10 ft 3 2 ft 2 1 5 1 ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 ◮ To Query sum for position n, fjrst read n. ◮ Then, subtract the lowest order bit, and repeat until n = 0. ◮ E.g.: 5 = 101 → 4 = 100 → 0

◮ ft(5) + ft(4) = 2 + 2 = 4

E.g.: 10 = 1010 8 = 1000

ft ft

Update 5: Visit 5=101 6=110 8=1000 LSOne(n) = n & -n

slide-16
SLIDE 16

Introduction Navigation

Queries

ft 4 10 ft 3 2 ft 2 1 5 1 ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 ◮ To Query sum for position n, fjrst read n. ◮ Then, subtract the lowest order bit, and repeat until n = 0. ◮ E.g.: 5 = 101 → 4 = 100 → 0

◮ ft(5) + ft(4) = 2 + 2 = 4

◮ E.g.: 10 = 1010 → 8 = 1000 → 0

◮ ft(10) + ft(8) = 1 + 10 = 11

Update 5: Visit 5=101 6=110 8=1000 LSOne(n) = n & -n

slide-17
SLIDE 17

Introduction Navigation

Queries

ft 4 10 ft 3 2 ft 2 1 5 1 ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 ◮ To Query sum for position n, fjrst read n. ◮ Then, subtract the lowest order bit, and repeat until n = 0. ◮ E.g.: 5 = 101 → 4 = 100 → 0

◮ ft(5) + ft(4) = 2 + 2 = 4

◮ E.g.: 10 = 1010 → 8 = 1000 → 0

◮ ft(10) + ft(8) = 1 + 10 = 11

Update 5: Visit 5=101 6=110 8=1000 LSOne(n) = n & -n

slide-18
SLIDE 18

Introduction Navigation

Queries

ft 4 10 ft 3 2 ft 2 1 5 1 ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 ◮ To Query sum for position n, fjrst read n. ◮ Then, subtract the lowest order bit, and repeat until n = 0. ◮ E.g.: 5 = 101 → 4 = 100 → 0

◮ ft(5) + ft(4) = 2 + 2 = 4

◮ E.g.: 10 = 1010 → 8 = 1000 → 0

◮ ft(10) + ft(8) = 1 + 10 = 11

◮ Update 5: Visit 5=101 → 6=110 → 8=1000 LSOne(n) = n & -n

slide-19
SLIDE 19

Introduction Navigation

Queries

ft 4 10 ft 3 2 ft 2 1 5 1 ft 1 2 2 1 value 1 1 2 3 2 1 1 index 1 2 3 4 5 6 7 8 9 10 index 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 ◮ To Query sum for position n, fjrst read n. ◮ Then, subtract the lowest order bit, and repeat until n = 0. ◮ E.g.: 5 = 101 → 4 = 100 → 0

◮ ft(5) + ft(4) = 2 + 2 = 4

◮ E.g.: 10 = 1010 → 8 = 1000 → 0

◮ ft(10) + ft(8) = 1 + 10 = 11

◮ Update 5: Visit 5=101 → 6=110 → 8=1000 ◮ LSOne(n) = n & -n