SlideShare a Scribd company logo
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
1
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
2
 After completing this chapter, the reader will be able to
understand the following:

 Basic tools needed to develop and analyze algorithms
 Methods to compute the efficiency of algorithms
 Ways to make a wise choice among many solutions for a
given problem
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
3
Algorithm Analysis
Asymptoti c Notations (W, p, O)
Big Omega (W)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
4
Algorithm Analysis
Asymptoti c Notations (W, p, O)
Big Omega (W)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
5
 The following steps elaborate the general structure of the
divide-and-conquer strategy
 If the data size n of problem P is fundamental,
calculate the result of P(n) and go to step 4
 If the data size n of problem P is not fundamental,
divide the problem P(n) into equivalent subproblems
P(n1), P(n2), … P(ni) such that i ≥ 1
 Apply divide-and-conquer recursively to each
individual subproblem P(n1), P(n2), …,P(ni)
 Combine the results of all subproblems P(n1), P(n2),…,
P(ni) to get the final solution of P(n)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
6
 At level one, only one call to a partition is made
with n elements; at level two, Atmost two calls are
made with elements (n - 1), and so on
C(n) = O(n2)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
7
 General
 Knapsack Problem
 Elements of Greedy Strategy
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
8
 To decide whether a problem can be solved using a
greedy strategy, the following elements should be
considered:
 Greedy-choice property
 Optimal substructure Greedy Method
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
9
 Greedy-choice property A problem exhibits
greedy-choice property if a globally optimal
solution can be arrived at by making a locally
optimal greedy choice
 That is, we make the choice that seems best at
that time without considering the results from
the sub problems
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
10
 The General Method
 Elements of Dynamic Programming
 Principle of Optimality
 Limitations of Dynamic Programming
 Knapsack Problem
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
11
 The General Method
 Elements of Dynamic Programming
 Principle of Optimality
 Limitations of Dynamic Programming
 Knapsack Problem
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
12
A dynamic programming solution has the following three
components:
 Formulate the answer as a recurrence relation or a
recursive algorithm
 Show that the number of different instances of your
recurrence is bounded by a polynomial
 Specify an order of evaluation for the recurrence
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
13
To decide whether a problem can be solved using the dynamic
programming method, the following three elements of dynamic
programming should be considered:

 Optimal substructure
 Overlapping subproblems
 Memorization
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
14
 A problem exhibits optimal substructure if an optimal
solution to the problem contains within it optimal
solutions to subproblems
 It also means that dynamic programming (and greedy
method) might apply
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
15
 When a recursive algorithm revisits the same problem
repeatedly, it is said that the optimization problem has
overlapping sub problems
 This is beneficial for dynamic programming
 It solves each subproblem once and stores the answer in a
table
 This answer can be searched in constant time when required.
This is contradictory to the divide-and-conquer strategy
where a new problem is generated at each step of recursion
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
16
 A problem exhibits optimal substructure if an optimal
solution to the problem contains within it optimal solutions
to subproblems
 It also means that dynamic programming (and greedy
method) might apply
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
17
 However, it uses the control structure similar to the
recursive algorithm
 In a memorized recursive algorithm, an entry is
maintained in a table for the solution to each subproblem
 Initially, all entries contain a special value, which
indicates that the entry is not yet used
 For each subproblem, which is encountered for the first
time, its solution is computed and stored in the table
 Next time, for that subproblem, its entry is searched and
the value is used
 This can be implemented using hashing
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
18
 The principle of optimality states that an optimal
sequence of decisions has the property that whatever
the initial state and decision are, the remaining
decisions must constitute an optimal decision
sequence with regard to the state resulting from the
first decision
Principle of Optimality
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
19
 Pattern matching is the process of finding the
presence of a particular string (pattern) in the given
string (text)
Pattern Matching
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
20
 Database search
 Search engine
 Text editors
 Intrusion detection
 Natural language processing
 Feature detection in digitized images
A few such applications are as follows:
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
21
The most popular are the following:
Brute-force approach
Boyer–Moore algorithm
Knuth–Morris–Pratt algorithm
Robin–Karp algorithm
Text partitioning algorithm
Semi-numerical algorithm
Popular techniques for
string pattern search
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
22
 String : A string is a finite sequence of symbols that are
chosen from a set or alphabet:
 Alphabet is a set of characters or symbols
 Substring: A substring or subsequence of a string is a
subset of the symbols in a string where the order of
elements is preserved
 Suffix: A suffix of S is a substring S[i, …, m − 1], where i
ranges between 0 and m − 1
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
23
 This is a simple straight forward approach based on
the comparison of a pattern character by character
with a string
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
24
 The steps involved in this approach are as
follows:
 Adjust the pattern P at beginning of the text
 Start moving from left to right and compare the
character of pattern to the corresponding character
in text
 Continue with step 2 until successful (all characters
of the pattern are matched) or unsuccessful (a
mismatch is detected)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
25
 Boyer and Moore have developed an efficient pattern
matching algorithm
 Instead of sliding by one character to the right at a time, in
Boyer–Moore approach, the sliding to the right is done in
longer steps
 The algorithm scans the character of pattern from right to
left beginning with the rightmost character
 If the text symbol compared with the rightmost
pattern symbol does not occur in the pattern at all, then the
pattern can be shifted by m positions (where m is length
of pattern)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
26
 The researchers Knuth, Morris, and Pattern
proposed a linear time algorithm for the string
matching problem
 In this approach, a matching time of O(n) is
achieved by avoiding comparisons with characters
of T that have previously been involved in
comparison with some element of the pattern P to
be matched so that backtracking is avoided
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
27
 The KMP matcher finds the occurrence of the
pattern P in text T and returns the number of shifts
of P, after which the occurrence is found taking T, P,
and prefix function p as inputs
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
28
 A compact data structure that represents a set of
strings (such as all the words in a text) known as tries
 A trie is a tree-based data structure for storing strings
to make pattern matching faster
 A trie helps in pattern matching in time that is
proportional to the length of the pattern
 Tries can be used to perform prefix query for
information retrieval
 Prefix query searches for the longest prefix of a given
string that matches a prefix of some string in the tries
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
29
 There are variants of tries, which are listed as
follows:
 Standard tries
 Compressed trie
 Suffix tries
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
30
 The standard trie for a set of strings S is an ordered tree
such that
 Each node but the root is labelled with a character;
 The children of a node are alphabetically ordered;
 The paths from the external nodes to the oot yield the
strings of S
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
31
 Similar to the standard trie, a compressed is a tree-
based data structure
 For storing strings in order to make pattern matching
much faster
 This is an optimized approach for pattern matching
specially suitable for applications where time is a
more crucial factor
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
32
Following are the unique characteristics of
compressed tire:
 A compressed trie (or Patricia trie) has internal
nodes of degree at least 2
 It is obtained from standard trie by compressing
chains of redundant nodes
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
33
b
id u
o
ok il sh y
s
ell to
ck p
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
34
m e g h a v i n d
0 1 2 3 4 5 6 7 8
d e me
ghavind
nd
ndghavind
nd
ghavind
vind
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
35
 A suffix trie is a compressed Trie for all the suffixes
of a text
 This is a compressed Trie, and hence, possesses all
features a compressed trie and makes it more
powerful for making a search faster as it includes
all suffixes of a text
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
36

More Related Content

What's hot (20)

PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
12. Heaps - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
14. Files - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
15. STL - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
widespreadpromotion
 
PPS
Data Structure
sheraz1
 
PPT
Stacks in algorithems & data structure
faran nawaz
 
PDF
Data Structures and Algorithm - Week 4 - Trees, Binary Trees
Ferdin Joe John Joseph PhD
 
PDF
Lecture-05-DSA
Haitham El-Ghareeb
 
PDF
Week 2 - Data Structures and Algorithms
Ferdin Joe John Joseph PhD
 
PDF
Data Structures and Algorithm - Week 9 - Search Algorithms
Ferdin Joe John Joseph PhD
 
PPT
List moderate
Rajendran
 
PDF
The D-basis Algorithm for Association Rules of High Confidence
ITIIIndustries
 
PDF
Mapping Domain Names to Categories
Gene Chuang
 
PDF
Week 1 - Data Structures and Algorithms
Ferdin Joe John Joseph PhD
 
PDF
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Ferdin Joe John Joseph PhD
 
PDF
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees
Ferdin Joe John Joseph PhD
 
PPTX
Datastructures using c++
Gopi Nath
 
PDF
Data structure-question-bank
Jagan Mohan Bishoyi
 
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
12. Heaps - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
14. Files - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
15. STL - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
widespreadpromotion
 
Data Structure
sheraz1
 
Stacks in algorithems & data structure
faran nawaz
 
Data Structures and Algorithm - Week 4 - Trees, Binary Trees
Ferdin Joe John Joseph PhD
 
Lecture-05-DSA
Haitham El-Ghareeb
 
Week 2 - Data Structures and Algorithms
Ferdin Joe John Joseph PhD
 
Data Structures and Algorithm - Week 9 - Search Algorithms
Ferdin Joe John Joseph PhD
 
List moderate
Rajendran
 
The D-basis Algorithm for Association Rules of High Confidence
ITIIIndustries
 
Mapping Domain Names to Categories
Gene Chuang
 
Week 1 - Data Structures and Algorithms
Ferdin Joe John Joseph PhD
 
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Ferdin Joe John Joseph PhD
 
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees
Ferdin Joe John Joseph PhD
 
Datastructures using c++
Gopi Nath
 
Data structure-question-bank
Jagan Mohan Bishoyi
 

Viewers also liked (20)

PDF
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
widespreadpromotion
 
PPTX
Pointers in c++
Vineeta Garg
 
PPT
5.3 dynamic programming
Krish_ver2
 
PPTX
Lists, queues and stacks
andreeamolnar
 
PPTX
Array,lists and hashes in perl
sana mateen
 
PPTX
Constructors and destructors
Vineeta Garg
 
PPTX
Classes and objects1
Vineeta Garg
 
DOCX
Advanced data structures using c++ 3
Shaili Choudhary
 
PPTX
Structured query language functions
Vineeta Garg
 
PPTX
Structured query language constraints
Vineeta Garg
 
PPS
Pp
sheraz1
 
DOC
Selection sort
asra khan
 
PPTX
Stacks in c++
Vineeta Garg
 
PDF
Working with Cookies in NodeJS
Jay Dihenkar
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PPT
C++ data types
pratikborsadiya
 
PPT
Dynamic pgmming
Dr. C.V. Suresh Babu
 
PPTX
Quick sort
Jehat Hassan
 
PPT
Dynamic programming
Shakil Ahmed
 
PPTX
Data file handling in c++
Vineeta Garg
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
widespreadpromotion
 
Pointers in c++
Vineeta Garg
 
5.3 dynamic programming
Krish_ver2
 
Lists, queues and stacks
andreeamolnar
 
Array,lists and hashes in perl
sana mateen
 
Constructors and destructors
Vineeta Garg
 
Classes and objects1
Vineeta Garg
 
Advanced data structures using c++ 3
Shaili Choudhary
 
Structured query language functions
Vineeta Garg
 
Structured query language constraints
Vineeta Garg
 
Selection sort
asra khan
 
Stacks in c++
Vineeta Garg
 
Working with Cookies in NodeJS
Jay Dihenkar
 
Data types in c++
Venkata.Manish Reddy
 
C++ data types
pratikborsadiya
 
Dynamic pgmming
Dr. C.V. Suresh Babu
 
Quick sort
Jehat Hassan
 
Dynamic programming
Shakil Ahmed
 
Data file handling in c++
Vineeta Garg
 
Ad

Similar to 16. Algo analysis & Design - Data Structures using C++ by Varsha Patil (20)

PPTX
EE-232-LEC-01 Data_structures.pptx
iamultapromax
 
PPT
OR Slide
Shreesha Shetty
 
PDF
A Review of Constraint Programming
Editor IJCATR
 
DOC
Discovering Novel Information with sentence Level clustering From Multi-docu...
irjes
 
PDF
G04124041046
IOSR-JEN
 
PDF
01VD062009003760042.pdf
SunilMatsagar1
 
PDF
Algorithm Specification and Data Abstraction
Ashutosh Satapathy
 
PDF
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
Dr Arash Najmaei ( Phd., MBA, BSc)
 
PPTX
Regression with Microsoft Azure & Ms Excel
Dr. Abdul Ahad Abro
 
PDF
Semantic Annotation of Documents
subash chandra
 
PDF
Cuckoo Search: Recent Advances and Applications
Xin-She Yang
 
PDF
Ankit presentation
ANKIT AGRAWAL
 
PDF
Algorithm of Dynamic Programming for Paper-Reviewer Assignment Problem
IRJET Journal
 
PPTX
A Fairness-aware Machine Learning Interface for End-to-end Discrimination Dis...
wajrcs
 
PPT
Experiments on Design Pattern Discovery
Tim Menzies
 
PDF
Sample prac exam2013
hccit
 
PDF
Analyzing the solutions of DEA through information visualization and data min...
Gurdal Ertek
 
PDF
LNCS 5050 - Bilevel Optimization and Machine Learning
butest
 
PDF
EXPERT OPINION AND COHERENCE BASED TOPIC MODELING
ijnlc
 
EE-232-LEC-01 Data_structures.pptx
iamultapromax
 
OR Slide
Shreesha Shetty
 
A Review of Constraint Programming
Editor IJCATR
 
Discovering Novel Information with sentence Level clustering From Multi-docu...
irjes
 
G04124041046
IOSR-JEN
 
01VD062009003760042.pdf
SunilMatsagar1
 
Algorithm Specification and Data Abstraction
Ashutosh Satapathy
 
6 Tips for Interpretable Topic Models _ by Nicha Ruchirawat _ Towards Data Sc...
Dr Arash Najmaei ( Phd., MBA, BSc)
 
Regression with Microsoft Azure & Ms Excel
Dr. Abdul Ahad Abro
 
Semantic Annotation of Documents
subash chandra
 
Cuckoo Search: Recent Advances and Applications
Xin-She Yang
 
Ankit presentation
ANKIT AGRAWAL
 
Algorithm of Dynamic Programming for Paper-Reviewer Assignment Problem
IRJET Journal
 
A Fairness-aware Machine Learning Interface for End-to-end Discrimination Dis...
wajrcs
 
Experiments on Design Pattern Discovery
Tim Menzies
 
Sample prac exam2013
hccit
 
Analyzing the solutions of DEA through information visualization and data min...
Gurdal Ertek
 
LNCS 5050 - Bilevel Optimization and Machine Learning
butest
 
EXPERT OPINION AND COHERENCE BASED TOPIC MODELING
ijnlc
 
Ad

Recently uploaded (20)

PPTX
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
PDF
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
PDF
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
PDF
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
PPTX
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
PDF
Development and validation of the Japanese version of the Organizational Matt...
Yoga Tokuyoshi
 
PDF
Data Retrieval and Preparation Business Analytics.pdf
kayserrakib80
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PPTX
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
PPTX
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
PDF
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
PPTX
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
PPTX
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
PDF
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PDF
apidays Singapore 2025 - The API Playbook for AI by Shin Wee Chuang (PAND AI)
apidays
 
PDF
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
PPT
tuberculosiship-2106031cyyfuftufufufivifviviv
AkshaiRam
 
PDF
Simplifying Document Processing with Docling for AI Applications.pdf
Tamanna
 
PDF
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
Development and validation of the Japanese version of the Organizational Matt...
Yoga Tokuyoshi
 
Data Retrieval and Preparation Business Analytics.pdf
kayserrakib80
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
apidays Singapore 2025 - The API Playbook for AI by Shin Wee Chuang (PAND AI)
apidays
 
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
tuberculosiship-2106031cyyfuftufufufivifviviv
AkshaiRam
 
Simplifying Document Processing with Docling for AI Applications.pdf
Tamanna
 
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 

16. Algo analysis & Design - Data Structures using C++ by Varsha Patil

  • 1. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 1
  • 2. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 2  After completing this chapter, the reader will be able to understand the following:   Basic tools needed to develop and analyze algorithms  Methods to compute the efficiency of algorithms  Ways to make a wise choice among many solutions for a given problem
  • 3. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 3 Algorithm Analysis Asymptoti c Notations (W, p, O) Big Omega (W)
  • 4. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 4 Algorithm Analysis Asymptoti c Notations (W, p, O) Big Omega (W)
  • 5. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 5  The following steps elaborate the general structure of the divide-and-conquer strategy  If the data size n of problem P is fundamental, calculate the result of P(n) and go to step 4  If the data size n of problem P is not fundamental, divide the problem P(n) into equivalent subproblems P(n1), P(n2), … P(ni) such that i ≥ 1  Apply divide-and-conquer recursively to each individual subproblem P(n1), P(n2), …,P(ni)  Combine the results of all subproblems P(n1), P(n2),…, P(ni) to get the final solution of P(n)
  • 6. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 6  At level one, only one call to a partition is made with n elements; at level two, Atmost two calls are made with elements (n - 1), and so on C(n) = O(n2)
  • 7. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 7  General  Knapsack Problem  Elements of Greedy Strategy
  • 8. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 8  To decide whether a problem can be solved using a greedy strategy, the following elements should be considered:  Greedy-choice property  Optimal substructure Greedy Method
  • 9. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 9  Greedy-choice property A problem exhibits greedy-choice property if a globally optimal solution can be arrived at by making a locally optimal greedy choice  That is, we make the choice that seems best at that time without considering the results from the sub problems
  • 10. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 10  The General Method  Elements of Dynamic Programming  Principle of Optimality  Limitations of Dynamic Programming  Knapsack Problem
  • 11. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 11  The General Method  Elements of Dynamic Programming  Principle of Optimality  Limitations of Dynamic Programming  Knapsack Problem
  • 12. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 12 A dynamic programming solution has the following three components:  Formulate the answer as a recurrence relation or a recursive algorithm  Show that the number of different instances of your recurrence is bounded by a polynomial  Specify an order of evaluation for the recurrence
  • 13. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 13 To decide whether a problem can be solved using the dynamic programming method, the following three elements of dynamic programming should be considered:   Optimal substructure  Overlapping subproblems  Memorization
  • 14. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 14  A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems  It also means that dynamic programming (and greedy method) might apply
  • 15. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 15  When a recursive algorithm revisits the same problem repeatedly, it is said that the optimization problem has overlapping sub problems  This is beneficial for dynamic programming  It solves each subproblem once and stores the answer in a table  This answer can be searched in constant time when required. This is contradictory to the divide-and-conquer strategy where a new problem is generated at each step of recursion
  • 16. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 16  A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems  It also means that dynamic programming (and greedy method) might apply
  • 17. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 17  However, it uses the control structure similar to the recursive algorithm  In a memorized recursive algorithm, an entry is maintained in a table for the solution to each subproblem  Initially, all entries contain a special value, which indicates that the entry is not yet used  For each subproblem, which is encountered for the first time, its solution is computed and stored in the table  Next time, for that subproblem, its entry is searched and the value is used  This can be implemented using hashing
  • 18. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 18  The principle of optimality states that an optimal sequence of decisions has the property that whatever the initial state and decision are, the remaining decisions must constitute an optimal decision sequence with regard to the state resulting from the first decision Principle of Optimality
  • 19. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 19  Pattern matching is the process of finding the presence of a particular string (pattern) in the given string (text) Pattern Matching
  • 20. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 20  Database search  Search engine  Text editors  Intrusion detection  Natural language processing  Feature detection in digitized images A few such applications are as follows:
  • 21. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 21 The most popular are the following: Brute-force approach Boyer–Moore algorithm Knuth–Morris–Pratt algorithm Robin–Karp algorithm Text partitioning algorithm Semi-numerical algorithm Popular techniques for string pattern search
  • 22. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 22  String : A string is a finite sequence of symbols that are chosen from a set or alphabet:  Alphabet is a set of characters or symbols  Substring: A substring or subsequence of a string is a subset of the symbols in a string where the order of elements is preserved  Suffix: A suffix of S is a substring S[i, …, m − 1], where i ranges between 0 and m − 1
  • 23. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 23  This is a simple straight forward approach based on the comparison of a pattern character by character with a string
  • 24. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 24  The steps involved in this approach are as follows:  Adjust the pattern P at beginning of the text  Start moving from left to right and compare the character of pattern to the corresponding character in text  Continue with step 2 until successful (all characters of the pattern are matched) or unsuccessful (a mismatch is detected)
  • 25. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 25  Boyer and Moore have developed an efficient pattern matching algorithm  Instead of sliding by one character to the right at a time, in Boyer–Moore approach, the sliding to the right is done in longer steps  The algorithm scans the character of pattern from right to left beginning with the rightmost character  If the text symbol compared with the rightmost pattern symbol does not occur in the pattern at all, then the pattern can be shifted by m positions (where m is length of pattern)
  • 26. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 26  The researchers Knuth, Morris, and Pattern proposed a linear time algorithm for the string matching problem  In this approach, a matching time of O(n) is achieved by avoiding comparisons with characters of T that have previously been involved in comparison with some element of the pattern P to be matched so that backtracking is avoided
  • 27. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 27  The KMP matcher finds the occurrence of the pattern P in text T and returns the number of shifts of P, after which the occurrence is found taking T, P, and prefix function p as inputs
  • 28. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 28  A compact data structure that represents a set of strings (such as all the words in a text) known as tries  A trie is a tree-based data structure for storing strings to make pattern matching faster  A trie helps in pattern matching in time that is proportional to the length of the pattern  Tries can be used to perform prefix query for information retrieval  Prefix query searches for the longest prefix of a given string that matches a prefix of some string in the tries
  • 29. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 29  There are variants of tries, which are listed as follows:  Standard tries  Compressed trie  Suffix tries
  • 30. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 30  The standard trie for a set of strings S is an ordered tree such that  Each node but the root is labelled with a character;  The children of a node are alphabetically ordered;  The paths from the external nodes to the oot yield the strings of S
  • 31. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 31  Similar to the standard trie, a compressed is a tree- based data structure  For storing strings in order to make pattern matching much faster  This is an optimized approach for pattern matching specially suitable for applications where time is a more crucial factor
  • 32. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 32 Following are the unique characteristics of compressed tire:  A compressed trie (or Patricia trie) has internal nodes of degree at least 2  It is obtained from standard trie by compressing chains of redundant nodes
  • 33. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 33 b id u o ok il sh y s ell to ck p
  • 34. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 34 m e g h a v i n d 0 1 2 3 4 5 6 7 8 d e me ghavind nd ndghavind nd ghavind vind
  • 35. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 35  A suffix trie is a compressed Trie for all the suffixes of a text  This is a compressed Trie, and hence, possesses all features a compressed trie and makes it more powerful for making a search faster as it includes all suffixes of a text
  • 36. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 36