Algorithms
  • Introduction
  • Algorithms
    • Insertion Sort
    • Shell Sort
    • Selection Sort
    • Heap Sort
    • Merge Sort
    • Quick Sort
    • Dual Pivot QuickSort
    • Radix Exchange Sort
  • Data Structures
    • Basic Datastructures
      • Table or Array
      • List
      • Linked List
      • Stack
      • Queue
      • Deque
      • Dictionary
      • HashTables
    • Trees
      • Tree
      • Binary Search Tree
      • Heap
      • Red-Black Trees
      • Splay Trees
      • Randomized Search Trees
    • Dynamic Programming
      • Fibonacci
      • Knapsack
      • Optimal Binary Search Trees
      • Longest Common Sub sequence
    • External Data Structures
      • B-Trees
      • Variants
    • Multi-dimensional Data Structures
      • Point-Region Quadtree
      • Point-Quadtrees
    • Priority Queues
      • Binomial Queues
  • Graphs
    • Basics
    • Depth First Search
    • Applications of Depth First Search
    • Breadth First Search
    • Shortest Paths
    • Transitive Closure
    • Flow Networks
    • Matching
  • Strings
    • Data Structures for Strings
      • Tries
        • Binary Tries
        • Multiway Trie
        • Patricia Tries
      • Variable Length-code
        • Elias Code
        • Gamma Code
        • Fibonacci Code
      • Huffmancode
      • Ternary Search Tree
    • Searching in Strings
      • Prefix Function
      • Knuth-Morris-Pratt
      • Boyer-Moore
      • Monte-Carlo Algorithms
      • Karp-Rabin
      • Automatons
      • Shift-AND
    • Indexing Text
      • Suffix Trees
      • Suffix Arrays
      • Search Engines & Inverted Files
  • P and NP
    • Introduction
    • SAT and 3-SAT
    • Graph Problems
    • Problems with Collections
    • Networks
    • Data storage
  • Metaheuristics
    • Trajectory Methods
      • Local Search
      • Simulated Annealing
      • Tabu Search
    • Population Based
      • Ant Colony
      • Evolutionary Computation
  • Sources
Powered by GitBook
On this page
  • Example
  • i = 1
  • i = 2
  • i = 3
  • i = 4

Was this helpful?

  1. Data Structures
  2. Dynamic Programming

Optimal Binary Search Trees

As we saw in red-black tree and splay trees, we want to minimze the time that is needed to access a binary tree. Therefor we want to find a solution that calculates the most efficient binary tree structure, which can be done through dynamic programming.

Lets say that we have our keys that we want to get inserted in this tree in ascending order k1,k2,k3,k4,k5,...k_1, k_2, k_3, k_4, k_5, ...k1​,k2​,k3​,k4​,k5​,..., and also their priorities p1,p2,p3,p4,p5,...p_1, p_2, p_3, p_4, p_5, ...p1​,p2​,p3​,p4​,p5​,... , now how can we create a BST with the lowest possible access time? Well first we have to define that the cost for finding an element i in a BST on depth d = d + 1 is equal to the number of comparisons that we have to do to find that element.

Now to solve this with dynamic programming, we follow these steps:

  • Take 2 tables (1 with the sorted keys, and 1 with the priorities)

  • For every index i (where i is the number of nodes that we will combine)

    • Take every combination of elements that we can combine together. (example, is we have 1, 2, 3 as elements and i = 2, then we can take 1,2 together and 2,3 together).

      • Now take the sum of the probabilities of the combined elements

        • the minimum of the longest combination possible if we take every index seperately as a root element.

Note that this algorithm gives O(n3)O(n^3)O(n3) as performance.

Example

0

1

2

3

V

10

12

16

21

P

4

2

6

3

i = 1

For i = 1, we see every element seperately, and thus we can write the probabilities as the diagonal of their indexes.

0

1

2

3

0

4

-

-

-

1

-

2

-

-

2

-

-

6

-

3

-

-

-

3

i = 2

For i = 2 it gets more complicated, this means that we take 2 nodes together. Let's take [0,1] together. This sum becomes: 4 + 2 = 6 and now we find the minimum if we take them bother seperately as root.

0 as root gives 2 1 as root gives 6

so the minimum is 2, add this together to our 6, becomes 8.

0

1

2

3

0

4

8

-

-

1

-

2

10

-

2

-

-

6

12

3

-

-

-

3

i = 3

Now we take 3 elements together, so we have the following combinations:

  • [0, 2]: Sum probabilities = 4 + 2 + 6 = 12

    • root = 0 ==> [1, 2] = 10

    • root = 1 ==> [0, 0] + [2, 2] = 10

    • root = 2 ==> [0, 1] = 8 (smallest)

    • ==> Solution: [0, 2] = 12 + 8 = 20

  • [1, 3]: Sum probabilities = 2 + 6 + 3 = 11

    • root = 1 ==> [2, 3] = 12

    • root = 2 ==> [1, 1] + [3, 3] = 2 + 3 = 5 (smallest)

    • root = 3 ==> [1, 2] = 10

    • ==> Solution: [1, 3] = 11 + 5 = 16

0

1

2

3

0

4

8

20

-

1

-

2

10

16

2

-

-

6

12

3

-

-

-

3

i = 4

0

1

2

3

0

4

8

20

26

1

-

2

10

16

2

-

-

6

12

3

-

-

-

3

PreviousKnapsackNextLongest Common Sub sequence

Last updated 6 years ago

Was this helpful?