Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Grokking TechTalk #27: Optimal Binary Search Tree

177 views

Published on

Cây nhị phân tìm kiếm là 1 cấu trúc dữ liệu quen thuộc với chúng ta. Có rất nhiều nghiên cứu và các thuật toán xoay quanh cấu trúc dữ liệu này. Trong talk này, xin giới thiệu một kỹ thuật giúp tối ưu cây nhị phân tìm kiếm dựa trên tần suất tìm kiếm, qua đó giúp giảm chi phí tìm kiếm xuống mức thấp nhất.

- Speaker: Phong Vu - Software Engineer

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Grokking TechTalk #27: Optimal Binary Search Tree

  1. 1. Optimal Binary Search Tree Le Phong Vu
  2. 2. About me ▶ Le Phong Vu ▶ Member of Grokking ▶ Data structure and caching system ▶ Email: lephongvu90@gmail.com 2
  3. 3. Agenda ▶ Problem: Build a dictionary online with fast lookup ▶ Approach to apply OBST in dictionary application ▶ Use Dynamic programming to find OBST ▶ Optimize solution to reduce time create OBST ▶ Application of OBST 3
  4. 4. Problem: Build a dictionary ▶ Given an English world, you want to search its meaning quickly 4
  5. 5. Build a dictionary with BST apple hatch mango pizzajump Balanced binary search tree Worst-case O(log n) time per query Total cost: 2000100 Word Total search Depth of Node Cost apple 1.000.000 2 1000000 * 2 hatch 100 1 100 * 1 jump, mango, pizza 0 * 0 5
  6. 6. Re-structure BST apple hatch … Re-structure BST, move “apple” node to root Total cost: 1000200 Word Total search Depth of Node Cost apple 1.000.000 1 1.000.000 * 1 hatch 100 2 100 * 2 jump, mango, pizza 0 * 0 OBTIMAL BINARY SEARCH TREE 6
  7. 7. Approach 7 Build a balanced BST Collect search information Calculate probability search Calculate a Optimal BST Change current BST by new Optimal BST
  8. 8. Key and probability ▶   word key probability apple hatch jump mango 8
  9. 9. Dummy Key Meaning Symbol d0 d1 d2 di d0 d1 d2 d3 d5d4 apple hatch jump mango pizza< apple (apple, hatch) 9
  10. 10. Example 10 word key total search probability < apple 5 apple 10 (apple, hatch) … … hatch … … jump … … mango … … Total search = 100
  11. 11. Search time ▶   11
  12. 12. Optimal Binary Search Tree ▶ Given the set probabilities p and q, can we construct a binary search tree whose expected search time is minimized? ▶ This tree is called an Optimal Binary Search Tree 12
  13. 13. Dynamic Programming ▶ Solves problems by combining the solutions to sub-problems. 13 Figure 1: Dynamic Programming https://itnext.io/dynamic-programming-vs-divid e-and-conquer-2fea680becbe
  14. 14. Step 1: The structure of OBST ▶ If an OBST has a sub-tree T, then this sub-tree T must be optimal BST. 14 Optimal BST Optimal BST Optimal BST
  15. 15. Step 2: Deciding root of OBST ▶   15
  16. 16. Step 2: Deciding Root of OBST ▶   16 kr Contribute e [i, r-1] Contribute e [i, r-1] Contribute W[i,j]
  17. 17. Step 3: Bottom-up approach ▶   17
  18. 18.   18
  19. 19. Optimize solution ▶   19
  20. 20. Application ▶ Huffman Tree ▶ Optimal routing lookup 20
  21. 21. Reference ▶ Le Phong Vu, “Dùng kỹ thuật quy hoạch động giải bài toán tối ưu cây nhị phân tìm kiếm” https://engineering.grokking.org/optimal-binary-search-tree/ ▶ D. Knuth, “Optimum binary search trees”, Acta Informatica www.inrg.csie.ntu.edu.tw/algorithm2014/presentation/Knuth71.pdf ▶ Thomas H. Cormen, “Introduction to algorithm”, MIT Press, 3rd Edition ▶ D. Knuth, “The art of computer programming”, volume 3 ▶ Kurt Mehlhorn, “Nearly Optimal Binary Search Tree” https://people.mpi-inf.mpg.de/~mehlhorn/ftp/mehlhorn3.pdf 21
  22. 22. Question 22
  23. 23. Example 23 An example from book: Introduction to Algorithm, 3rd edition
  24. 24. Example 24
  25. 25. Pseudocode 25

×