SlideShare a Scribd company logo
Optimal Binary Search
DESIGN ANALYSIS AND ALGORITHM
CSE3004
What is the problem?
• Determining the cost and the structure of the Optimal Binary Search Tree
(OBST) for a set of 4 keys to find the smallest possible search time for a
given sequence of accesses.
INDEX 0 11 2 3
NODE 43 13 80 74
FREQUENCY 3 4 8 9
Algorithm
• Here, the Optimal Binary Search Tree Algorithm is presented.
• First, we build a BST from a set of provided n number of distinct keys < k1,
k2, k3, ... kn >.
• Here we assume, the probability of accessing a key Ki is pi.
• Some dummy keys (d0, d1, d2, ... dn) are added as some searches may be
performed for the values which are not present in the Key set K.
• We assume, for each dummy key di probability of access is qi.
Algorithm
int sum(int freq[], int i, int j);
int optCost(int freq[], int i, int j)
{
if (j < i) // no elements in this subarray
return 0;
if (j == i)
return freq[i];
int fsum = sum(freq, i, j);
int min = INT_MAX;
for (int r = i; r <= j; ++r)
{
int cost = optCost(freq, i, r - 1) +
optCost(freq, r + 1, j);
if (cost < min)
min = cost;
}
return min + fsum;
}
int optimalSearchTree(int keys[],
int freq[], int n) {
return optCost(freq, 0, n - 1);
}
int sum(int freq[], int i, int j) {
int s = 0;
for (int k = i; k <= j; k++)
s += freq[k];
return s;
}
int main() {
int keys[] = {10, 12, 20};
int freq[] = {34, 8, 50};
int n = sizeof(keys) / sizeof(keys[0]);
cout << "Cost of Optimal BST is "
<< optimalSearchTree(keys, freq, n);
return 0;
}
Solving the Problem
• First for l = 1
calculating costs of following
Cost[0,0] = 3
Cost[1,1] = 4
Cost[2,2] = 8
Cost[3,3] = 9
• Now for l = 2
calculating cost
Cost[0,1] = 10
Cost[1,2] = 16
Solving the Problem
• For l = 2
calculating cost
Cost[2,3] = 25
• Now for l = 3
calculating cost
Cost[0,2] = 25
Cost[1,3] = 34
• For l = 4
calculating cost
Cost[0,3] = 43 (Maximum frequency)
Calculating Minimum Frequency
From the matrix 43 is the maximum value
with node (2)
Now mention second node as a root
node, with all the other nodes as the child
nodes and the following optimal binary
search tree is formed
Checking frequency as per formed obst.
Total frequency = 8x1 + 3x2 + 9x2 + 4x3
= 9 + 16 + 18 + 12
= 43
Hence Verified.
THANK YOU
MADE BY –
A&C
Ad

More Related Content

What's hot (20)

Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
Jyothishmathi Institute of Technology and Science Karimnagar
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
Dr Shashikant Athawale
 
Binary search
Binary searchBinary search
Binary search
AparnaKumari31
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
Kumar
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
Animesh Chaturvedi
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
Sreedhar Chowdam
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
Kumar
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
Selection sort
Selection sortSelection sort
Selection sort
amna izzat
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
subhashchandra197
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
QAU ISLAMABAD,PAKISTAN
 
Binary Search
Binary SearchBinary Search
Binary Search
kunj desai
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
Al Amin
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
Muhammad Muzammal
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
Rajendran
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
Kumar
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
Animesh Chaturvedi
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
Sreedhar Chowdam
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
Kumar
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
Selection sort
Selection sortSelection sort
Selection sort
amna izzat
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
Al Amin
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
Rajendran
 

Similar to OPTIMAL BINARY SEARCH (20)

14_OptimalBinarySearchTree.ppt
14_OptimalBinarySearchTree.ppt14_OptimalBinarySearchTree.ppt
14_OptimalBinarySearchTree.ppt
riyiko
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
Saurabh Singh
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
Shakil Ahmed
 
Datastructure tree
Datastructure treeDatastructure tree
Datastructure tree
rantd
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
showkat27
 
Matrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmMatrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithm
RajKumar323561
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
Svet Ivantchev
 
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptxCh 2Algo Analysis.pptxCh 2Algo Analysis.pptx
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
abduulahikhmaies
 
OptimalBinarySearchTree.ppt
OptimalBinarySearchTree.pptOptimalBinarySearchTree.ppt
OptimalBinarySearchTree.ppt
Freeze16
 
Lecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & AlgorithmsLecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & Algorithms
haseebanjum2611
 
Yoyak ScalaDays 2015
Yoyak ScalaDays 2015Yoyak ScalaDays 2015
Yoyak ScalaDays 2015
ihji
 
ByteCode 2012 Talk: Quantitative analysis of Java/.Net like programs to under...
ByteCode 2012 Talk: Quantitative analysis of Java/.Net like programs to under...ByteCode 2012 Talk: Quantitative analysis of Java/.Net like programs to under...
ByteCode 2012 Talk: Quantitative analysis of Java/.Net like programs to under...
garbervetsky
 
Codes and Isogenies
Codes and IsogeniesCodes and Isogenies
Codes and Isogenies
Priyanka Aash
 
Lec39
Lec39Lec39
Lec39
Nikhil Chilwant
 
Clustering com numpy e cython
Clustering com numpy e cythonClustering com numpy e cython
Clustering com numpy e cython
Anderson Dantas
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
PyData
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
luzenith_g
 
14_OptimalBinarySearchTree.ppt
14_OptimalBinarySearchTree.ppt14_OptimalBinarySearchTree.ppt
14_OptimalBinarySearchTree.ppt
riyiko
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
Saurabh Singh
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
Shakil Ahmed
 
Datastructure tree
Datastructure treeDatastructure tree
Datastructure tree
rantd
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
showkat27
 
Matrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmMatrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithm
RajKumar323561
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
Svet Ivantchev
 
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptxCh 2Algo Analysis.pptxCh 2Algo Analysis.pptx
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
abduulahikhmaies
 
OptimalBinarySearchTree.ppt
OptimalBinarySearchTree.pptOptimalBinarySearchTree.ppt
OptimalBinarySearchTree.ppt
Freeze16
 
Lecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & AlgorithmsLecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & Algorithms
haseebanjum2611
 
Yoyak ScalaDays 2015
Yoyak ScalaDays 2015Yoyak ScalaDays 2015
Yoyak ScalaDays 2015
ihji
 
ByteCode 2012 Talk: Quantitative analysis of Java/.Net like programs to under...
ByteCode 2012 Talk: Quantitative analysis of Java/.Net like programs to under...ByteCode 2012 Talk: Quantitative analysis of Java/.Net like programs to under...
ByteCode 2012 Talk: Quantitative analysis of Java/.Net like programs to under...
garbervetsky
 
Clustering com numpy e cython
Clustering com numpy e cythonClustering com numpy e cython
Clustering com numpy e cython
Anderson Dantas
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
PyData
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
luzenith_g
 
Ad

More from Cool Guy (8)

Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and Algorithms
Cool Guy
 
Modified Distribution Method (MODI)
Modified Distribution Method (MODI)Modified Distribution Method (MODI)
Modified Distribution Method (MODI)
Cool Guy
 
Class 12 Maths Board Syllabus CBSE 2018
Class 12 Maths Board Syllabus CBSE 2018Class 12 Maths Board Syllabus CBSE 2018
Class 12 Maths Board Syllabus CBSE 2018
Cool Guy
 
Criminal Record System
Criminal Record SystemCriminal Record System
Criminal Record System
Cool Guy
 
Infrared Security System
Infrared Security SystemInfrared Security System
Infrared Security System
Cool Guy
 
The Preparation Of Potash Alum
The Preparation Of Potash AlumThe Preparation Of Potash Alum
The Preparation Of Potash Alum
Cool Guy
 
Soil pollution
Soil pollutionSoil pollution
Soil pollution
Cool Guy
 
Software concepts
Software conceptsSoftware concepts
Software concepts
Cool Guy
 
Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and Algorithms
Cool Guy
 
Modified Distribution Method (MODI)
Modified Distribution Method (MODI)Modified Distribution Method (MODI)
Modified Distribution Method (MODI)
Cool Guy
 
Class 12 Maths Board Syllabus CBSE 2018
Class 12 Maths Board Syllabus CBSE 2018Class 12 Maths Board Syllabus CBSE 2018
Class 12 Maths Board Syllabus CBSE 2018
Cool Guy
 
Criminal Record System
Criminal Record SystemCriminal Record System
Criminal Record System
Cool Guy
 
Infrared Security System
Infrared Security SystemInfrared Security System
Infrared Security System
Cool Guy
 
The Preparation Of Potash Alum
The Preparation Of Potash AlumThe Preparation Of Potash Alum
The Preparation Of Potash Alum
Cool Guy
 
Soil pollution
Soil pollutionSoil pollution
Soil pollution
Cool Guy
 
Software concepts
Software conceptsSoftware concepts
Software concepts
Cool Guy
 
Ad

Recently uploaded (20)

Lesson 6-Interviewing in SHRM_updated.pdf
Lesson 6-Interviewing in SHRM_updated.pdfLesson 6-Interviewing in SHRM_updated.pdf
Lesson 6-Interviewing in SHRM_updated.pdf
hemelali11
 
Snowflake training | Snowflake online course
Snowflake training | Snowflake online courseSnowflake training | Snowflake online course
Snowflake training | Snowflake online course
Accentfuture
 
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
Taqyea
 
national income & related aggregates (1)(1).pptx
national income & related aggregates (1)(1).pptxnational income & related aggregates (1)(1).pptx
national income & related aggregates (1)(1).pptx
j2492618
 
Dynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics DynamicsDynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics Dynamics
heyoubro69
 
Feature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record SystemsFeature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record Systems
Process mining Evangelist
 
Introduction to Python_for_machine_learning.pdf
Introduction to Python_for_machine_learning.pdfIntroduction to Python_for_machine_learning.pdf
Introduction to Python_for_machine_learning.pdf
goldenflower34
 
Digital Disruption Use Case_Music Industry_for students.pdf
Digital Disruption Use Case_Music Industry_for students.pdfDigital Disruption Use Case_Music Industry_for students.pdf
Digital Disruption Use Case_Music Industry_for students.pdf
ProsenjitMitra9
 
Responsible Data Science for Process Miners
Responsible Data Science for Process MinersResponsible Data Science for Process Miners
Responsible Data Science for Process Miners
Process mining Evangelist
 
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial IntelligenceDr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug
 
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdfTOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
NhiV747372
 
Red Hat Openshift Training - openshift (1).pptx
Red Hat Openshift Training - openshift (1).pptxRed Hat Openshift Training - openshift (1).pptx
Red Hat Openshift Training - openshift (1).pptx
ssuserf60686
 
From Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
From Data to Insight: How News Aggregator APIs Deliver Contextual IntelligenceFrom Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
From Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
Contify
 
MLOps_with_SageMaker_Template_EN idioma inglés
MLOps_with_SageMaker_Template_EN idioma inglésMLOps_with_SageMaker_Template_EN idioma inglés
MLOps_with_SageMaker_Template_EN idioma inglés
FabianPierrePeaJacob
 
The challenges of using process mining in internal audit
The challenges of using process mining in internal auditThe challenges of using process mining in internal audit
The challenges of using process mining in internal audit
Process mining Evangelist
 
Important JavaScript Concepts Every Developer Must Know
Important JavaScript Concepts Every Developer Must KnowImportant JavaScript Concepts Every Developer Must Know
Important JavaScript Concepts Every Developer Must Know
yashikanigam1
 
TYPES OF SOFTWARE_ A Visual Guide.pdf CA SUVIDHA CHAPLOT
TYPES OF SOFTWARE_ A Visual Guide.pdf CA SUVIDHA CHAPLOTTYPES OF SOFTWARE_ A Visual Guide.pdf CA SUVIDHA CHAPLOT
TYPES OF SOFTWARE_ A Visual Guide.pdf CA SUVIDHA CHAPLOT
CA Suvidha Chaplot
 
CS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docxCS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docx
nidarizvitit
 
web-roadmap developer file information..
web-roadmap developer file information..web-roadmap developer file information..
web-roadmap developer file information..
pandeyarush01
 
report (maam dona subject).pptxhsgwiswhs
report (maam dona subject).pptxhsgwiswhsreport (maam dona subject).pptxhsgwiswhs
report (maam dona subject).pptxhsgwiswhs
AngelPinedaTaguinod
 
Lesson 6-Interviewing in SHRM_updated.pdf
Lesson 6-Interviewing in SHRM_updated.pdfLesson 6-Interviewing in SHRM_updated.pdf
Lesson 6-Interviewing in SHRM_updated.pdf
hemelali11
 
Snowflake training | Snowflake online course
Snowflake training | Snowflake online courseSnowflake training | Snowflake online course
Snowflake training | Snowflake online course
Accentfuture
 
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
Taqyea
 
national income & related aggregates (1)(1).pptx
national income & related aggregates (1)(1).pptxnational income & related aggregates (1)(1).pptx
national income & related aggregates (1)(1).pptx
j2492618
 
Dynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics DynamicsDynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics Dynamics
heyoubro69
 
Feature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record SystemsFeature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record Systems
Process mining Evangelist
 
Introduction to Python_for_machine_learning.pdf
Introduction to Python_for_machine_learning.pdfIntroduction to Python_for_machine_learning.pdf
Introduction to Python_for_machine_learning.pdf
goldenflower34
 
Digital Disruption Use Case_Music Industry_for students.pdf
Digital Disruption Use Case_Music Industry_for students.pdfDigital Disruption Use Case_Music Industry_for students.pdf
Digital Disruption Use Case_Music Industry_for students.pdf
ProsenjitMitra9
 
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial IntelligenceDr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug
 
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdfTOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
NhiV747372
 
Red Hat Openshift Training - openshift (1).pptx
Red Hat Openshift Training - openshift (1).pptxRed Hat Openshift Training - openshift (1).pptx
Red Hat Openshift Training - openshift (1).pptx
ssuserf60686
 
From Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
From Data to Insight: How News Aggregator APIs Deliver Contextual IntelligenceFrom Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
From Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
Contify
 
MLOps_with_SageMaker_Template_EN idioma inglés
MLOps_with_SageMaker_Template_EN idioma inglésMLOps_with_SageMaker_Template_EN idioma inglés
MLOps_with_SageMaker_Template_EN idioma inglés
FabianPierrePeaJacob
 
The challenges of using process mining in internal audit
The challenges of using process mining in internal auditThe challenges of using process mining in internal audit
The challenges of using process mining in internal audit
Process mining Evangelist
 
Important JavaScript Concepts Every Developer Must Know
Important JavaScript Concepts Every Developer Must KnowImportant JavaScript Concepts Every Developer Must Know
Important JavaScript Concepts Every Developer Must Know
yashikanigam1
 
TYPES OF SOFTWARE_ A Visual Guide.pdf CA SUVIDHA CHAPLOT
TYPES OF SOFTWARE_ A Visual Guide.pdf CA SUVIDHA CHAPLOTTYPES OF SOFTWARE_ A Visual Guide.pdf CA SUVIDHA CHAPLOT
TYPES OF SOFTWARE_ A Visual Guide.pdf CA SUVIDHA CHAPLOT
CA Suvidha Chaplot
 
CS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docxCS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docx
nidarizvitit
 
web-roadmap developer file information..
web-roadmap developer file information..web-roadmap developer file information..
web-roadmap developer file information..
pandeyarush01
 
report (maam dona subject).pptxhsgwiswhs
report (maam dona subject).pptxhsgwiswhsreport (maam dona subject).pptxhsgwiswhs
report (maam dona subject).pptxhsgwiswhs
AngelPinedaTaguinod
 

OPTIMAL BINARY SEARCH

  • 1. Optimal Binary Search DESIGN ANALYSIS AND ALGORITHM CSE3004
  • 2. What is the problem? • Determining the cost and the structure of the Optimal Binary Search Tree (OBST) for a set of 4 keys to find the smallest possible search time for a given sequence of accesses. INDEX 0 11 2 3 NODE 43 13 80 74 FREQUENCY 3 4 8 9
  • 3. Algorithm • Here, the Optimal Binary Search Tree Algorithm is presented. • First, we build a BST from a set of provided n number of distinct keys < k1, k2, k3, ... kn >. • Here we assume, the probability of accessing a key Ki is pi. • Some dummy keys (d0, d1, d2, ... dn) are added as some searches may be performed for the values which are not present in the Key set K. • We assume, for each dummy key di probability of access is qi.
  • 4. Algorithm int sum(int freq[], int i, int j); int optCost(int freq[], int i, int j) { if (j < i) // no elements in this subarray return 0; if (j == i) return freq[i]; int fsum = sum(freq, i, j); int min = INT_MAX; for (int r = i; r <= j; ++r) { int cost = optCost(freq, i, r - 1) + optCost(freq, r + 1, j); if (cost < min) min = cost; } return min + fsum; } int optimalSearchTree(int keys[], int freq[], int n) { return optCost(freq, 0, n - 1); } int sum(int freq[], int i, int j) { int s = 0; for (int k = i; k <= j; k++) s += freq[k]; return s; } int main() { int keys[] = {10, 12, 20}; int freq[] = {34, 8, 50}; int n = sizeof(keys) / sizeof(keys[0]); cout << "Cost of Optimal BST is " << optimalSearchTree(keys, freq, n); return 0; }
  • 5. Solving the Problem • First for l = 1 calculating costs of following Cost[0,0] = 3 Cost[1,1] = 4 Cost[2,2] = 8 Cost[3,3] = 9 • Now for l = 2 calculating cost Cost[0,1] = 10 Cost[1,2] = 16
  • 6. Solving the Problem • For l = 2 calculating cost Cost[2,3] = 25 • Now for l = 3 calculating cost Cost[0,2] = 25 Cost[1,3] = 34 • For l = 4 calculating cost Cost[0,3] = 43 (Maximum frequency)
  • 7. Calculating Minimum Frequency From the matrix 43 is the maximum value with node (2) Now mention second node as a root node, with all the other nodes as the child nodes and the following optimal binary search tree is formed Checking frequency as per formed obst. Total frequency = 8x1 + 3x2 + 9x2 + 4x3 = 9 + 16 + 18 + 12 = 43 Hence Verified.
  翻译: