🌳

Algorithms Live: Episode 0 - Fenwick Trees

Jul 22, 2024

Algorithms Live - Episode 0: Fenwick Trees

Host and Guest

  • Host: Matt Fontaine
  • Guest: Travis Me, PhD candidate in Hardware Security and ICPC teammate (2012 World Finals)

Bookkeeping

  • Broadcast time may vary each week
  • Check blog weekly for updates
  • Recordings available for later viewing

Introduction to Fenwick Trees

  • Alternative names: Binary Indexed Trees (BITs)
  • Commonly confused terms: switch between names can be used interchangeably

Problem Introduction

  • Goal: Solve range sum queries efficiently
  • Example: Given sequence [1, 4, 2, 1, 3, 5, 1, 2, 3], find sum of a range e.g., sum([1,4]) = 11
  • Naive Approach: Use for-loop. Worst case: O(n) per query
  • Pre-computation Strategy: Compute prefix sums, allows O(1) query time but O(n) time to update entire array

Motivation for Fenwick Trees

  • Dynamic Arrays: Need efficient update operations
  • Data Storage: Store partial prefix sums - allows range queries through prefix methods
  • Structuring Fenwick Trees: Binary Representation: Each cell responsible for its values plus values determined by the lowest 1-bit in its index

Fenwick Trees - Structure and Sum Queries

Structure Example

  • Indexing: Binary (1 to 16)
  • Range Responsibility: Determined by lowest 1-bit for each index
  • Redundancy: Multiple indices may have overlapping coverage

Sum Queries

  • Calculate prefix sums by removing the lowest 1-bit successively
  • Time Complexity: O(log n)

Fenwick Trees - Update Operations

Update Example

  • Propagate Changes: Update all indices responsible for a given value
  • Calculate: Find all cells by adding the lowest 1-bit
  • Isolate Lowest 1-bit: Use bitwise AND with two’s complement (-i)

Update Implementations

  • Code Example: Demonstrates updating and querying operations using simple loops

Example Problems on Fenwick Trees

Counting Inversions

  • Goal: Count inversions in a sequence
  • Methodology: Store counts of elements, traverse sequence to count inversions efficiently
  • Runtime: O(n log n)

Kth Tallest Person Problem

  • Heights range from 1 to 1000000
  • Two Queries: Update heights, find kth tallest person
  • Binary Search Approach: Combined with Fenwick Tree for improved efficiency
  • Optimized Runtime: O(log n)

Range Updates and Queries

  • Goal: Perform bulk updates and query individual points
  • Method: Use difference arrays combined with Fenwick Tree

Practice Problems

  1. Finding the Median: USACO 2011, November contest, Above the Median
  2. Movie Collection: Open Kattis, simplifies k-select problem solving

Next Week's Preview

  • Christmas Tree Lecture: Inspired by Donald Knuth’s lectures
  • Topics: Properties of trees, algorithms, and using these for complex problem-solving
  • Preparation: Review proof by contradiction, graph theory

Q&A Highlights

  • Coding Language: Preference for Java; robust libraries; less prone to bugs compared to C++
  • Software Used: Microsoft OneNote and YouTube Live
  • Future Topics: Multi-dimensional Fenwick Trees, high potential for future lectures

Viewer Preferences

  • Black background preferred by viewers for ease on eyes