Neeldhara
  • About
  • Research
    • Overview
    • People
    • Publications
    • Surveys
  • Teaching
    • Courses
    • Materials
  • Lists
    • Puzzles
    • Bookmarks
  • Exposition
    • Talks
    • Videos
  • Events
  • Blog

4. Optimal BSTs

  • Advanced Algorithms
    • Flows
    • Intractability
    • Approximation Algorithms
    • Randomized Algorithms
    • Greedy Algorithms
      • 1. Storing Files on a Tape
      • 2. A Scheduling Problem
      • 3. Stable Matchings
    • Matroids
      • 1. The Motivation
      • 2. The Definition
      • 3. Greedy Works!
      • 4. Scheduling with Deadlines
    • Dynamic Programming
      • 1. Longest Increasing Subsequence
      • 2. Subset Sum
      • 3. Set Cover
      • 4. Optimal BSTs
      • 5. Maximum Independent Set on Trees

On this page

  • The Problem
  • The Solution

4. Optimal BSTs

algonotes
lecturenotes
Published

May 16, 2023

Acknowledgements

Some of this material is borrowed from Jeff Erickson’s chapters on Recursion, Backtracking, and Dynamic Programming. Check them out for a more detailed comparison between recursive and memoized implementations.

The Problem

  • The input is a sorted array A[1,…,n]A[1, \ldots, n]A[1,…,n] of search keys and an array f[1,…,n]f[1, \ldots, n]f[1,…,n] of frequency counts, where f[i]f[i]f[i] is the number of times we will search for A[i]A[i]A[i].
  • Our task is to construct a binary search tree for that set such that the total cost of all the searches is as small as possible, where the cost of a search for a key is the the number of anscestors1 that the key has multiplied by its frequency.

This can be thought of as a non-linear version of the file storage problem. Food for thought: will a greedy strategy (insert in descending order of frequencies of access) work?

Heads up: Note that the optimal solution may not be balanced at all.

The Solution

This section is coming soon.

What are the fragments (AKA, what do we want to store)?
Are the fragments going to be useful (AKA, where is the final answer)?
Do we have a kickstart (AKA, what are the base cases)?
How do the fragments come together (AKA, how do we compute the values that we have agreed to store)?
Can we put the pieces together without getting stuck (AKA, are the dependencies in step #4 acyclic)?

Footnotes

  1. The root is the only anscestor of itself, so the cost of access is just one.↩︎


© 2022 • Neeldhara Misra • Credits •

 

Corrections? Please leave a comment here or a PR in this repository, thanks!

I’d rather be a failure at something I love than a success at something I hate.

George Burns

You live and you learn — at any rate, you live.

Douglas Adams

A problem worthy of attack proves its worth by fighting back.

Paul Erdos

×