Design and Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Lower Bounds for Sorting, Searching and Selection
Advertisements

Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Lecture 3: Parallel Algorithm Design
Types of Algorithms.
Advanced Data Structures
Chapter 6: Transform and Conquer
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
Comp 122, Fall 2004 Dynamic Programming. dynprog - 2 Lin / Devi Comp 122, Spring 2004 Longest Common Subsequence  Problem: Given 2 sequences, X =  x.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
1 Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
MA/CSSE 473 Day 30 Optimal BSTs. MA/CSSE 473 Day 30 Student Questions Optimal Linked Lists Expected Lookup time in a Binary Tree Optimal Binary Tree (intro)
MA/CSSE 473 Days Optimal linked lists Optimal BSTs.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
CMPT 438 Algorithms.
Searching and Sorting Algorithms
Decision Trees DEFINITION: DECISION TREE A decision tree is a tree in which the internal nodes represent actions, the arcs represent outcomes of an action,
Part-D1 Binary Search Trees
BCA-II Data Structure Using C
Top 50 Data Structures Interview Questions
Binary Search Trees A binary search tree is a binary tree
Database Management System
Binary search tree. Removing a node
Advanced Algorithms Analysis and Design
Backtracking And Branch And Bound
Binary Search Tree (BST)
Chapter 6 Transform-and-Conquer
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
Matrix Chain Multiplication
Best-fit bin packing in O(n log n) time
Lecture 22 Binary Search Trees Chapter 10 of textbook
B+ Tree.
Data Structures and Algorithms
Types of Algorithms.
CS200: Algorithm Analysis
Chapter 8 Dynamic Programming.
CS 3343: Analysis of Algorithms
Chapter 6 Transform and Conquer.
The Complexity of Algorithms and the Lower Bounds of Problems
Part-D1 Priority Queues
Complexity Present sorting methods. Binary search. Other measures.
Multi-Way Search Trees
Unit-2 Divide and Conquer
Types of Algorithms.
Matrix Chain Multiplication
Data Structures and Algorithms
Chapter 6: Transform and Conquer
A Robust Data Structure
Advanced Algorithms Analysis and Design
Data Structure and Algorithms
Greedy Algorithms Alexandra Stefan.
Quicksort.
Lecture 9: Self Balancing Trees
Types of Algorithms.
Dynamic Programming II DP over Intervals
B+-Trees j a0 k1 a1 k2 a2 … kj aj j = number of keys in node.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Divide-and-conquer approach
Divide and Conquer Merge sort and quick sort Binary search
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Quicksort.
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Data Structures and Algorithms
Presentation transcript:

Design and Analysis of Algorithms Example of Dynamic Programming Algorithms Optimal Binary Search Tree

Key Points Dynamic Algorithms Solve small problems Store answers to these small problems Use the small problem results to answer larger problems Use space to obtain speed-up

Optimal Binary Search Trees Balanced trees Always the most efficient search trees?

Optimal Binary Search Trees Balanced trees Always the most efficient search trees? Yes, if every key is equally probable Spelling check dictionary Entry at root of a balanced tree ... miasma? Occurrence in ordinary text ... 0.01%, 0.0001%, .. ? 99.99+% of searches waste at least one comparison! Common words (‘a’, ‘and’, ‘the’, ...) in leaves? If key, k, has relative frequency, rk , then in an optimal tree, we minimise dkrk dk is the depth of key k

Optimal Binary Search Trees Finding the optimal tree Try each “candidate” key as the root Divides the keys into left and right groups Try each key in the left group as root of the left sub-tree ... Number of candidate keys: O(n) Number of candidates for roots of sub-trees: 2O(n) O(nn) algorithm

Optimal Binary Search Trees Lemma Sub-trees of optimal trees are themselves optimal trees Proof If a sub-tree of an optimal tree is not optimal, then a better search tree will be produced if the sub-tree is replaced by an optimal tree.

Optimal Binary Search Trees Key Table Keys (in order) + frequency Key Problem Which key should be placed at the root? If we can determine this, we can ask the same question for the left and right subtrees. A B C D E 23 10 8 12 30 F G H I J K L M N O P .. 5 14 18 20 2 4 11 7 22

Optimal Binary Search Tree Divide and conquer? Choose a key for the root n choices Repeat the process for the sub-trees 2 O(n) O(nn) Smaller problems are not small enough! One k, one n-k-1

Optimal Binary Search Tree Start with the small problems Look at pairs of adjacent keys Two possible arrangements A B C D E 23 10 8 12 30 F G H I J K L M N O P .. 5 14 18 20 2 4 11 7 22 C D Min D C Cost 8x1 + 12x2 = 32 8x2 + 12x1 = 28

Optimal Binary Search Tree - Cost matrix Initialise Diagonal C[j,j] Costs of one-element ‘trees’ Below diagonal C[j,k] Costs of best tree j to k Cjj Zero x Cost of best tree C-G

Optimal Binary Search Tree - Cost matrix Store the costs of the best two element trees Diagonal C[j,j] Costs of one-element ‘trees’ Below diagonal C[j-1,j] Costs of best 2-element trees j-1 to j Cj-1,j

Optimal Binary Search Tree - Root matrix Store the roots of the best two element trees Diagonal Roots of 1-element trees Below diagonal best[j-1,j] Root of best 2-element trees j-1 to j

Optimal Binary Search Tree - 3-element trees Now examine the 3-element trees Choose each in turn as the root B with (C,D) to the right C with B and D as children D with (B,C) to the left Find best, store cost in C[B,D] Store root in best[B,D] A B C D E 23 10 8 12 30 F G H I J K L M N O P .. 5 14 18 20 2 4 11 7 22 Next slide

Optimal Binary Search Tree - 3-element trees Find best, store cost in C[B,D] Store root in best[B,D] Root = B Root = C Root = D D C B We already know this is best for C,D and stored its cost D C B B D C Best B,C

Optimal Binary Search Tree - 3-element trees Similarly, update all C[j-2,j] and best[j-2,j] Costs Roots

Optimal Binary Search Trees - 4-trees Now the 4-element trees eg A-D Choose A as root Choose B as root Choose C as root Choose D as root Use 0 for left Best B-D is known A-A is in C[0,0] Best C-D is known A-B is in C[0,1] D is in C[3,3] A-C is in C[0,2] Use 0 in C[4,3] for right

Optimal Binary Search Trees Final cost will be in C[0,n-1] Final cost

Optimal Binary Search Trees Construct the search tree Root will be in best[0,n-1] If r0 = best[0,n-1], Left subtree root is best[0,r0-1], Right subtree root is best[r0+1,n-1] Root = ‘E’

Optimal Binary Search Trees Construct the search tree E B H A D G I C F J

Optimal Binary Search Trees - Analysis k -element trees require k operations One for each candidate root There are k of them O(k2) There are n levels Constructing the tree is O(n) Average ~ logn Total O(n3)  k2 = O(n3) k =1 n

Optimal Binary Search Trees - Notes A good example of a dynamic algorithm Solves all the small problems Builds solutions to larger problems from them Requires space to store small problem results