SlideShare a Scribd company logo
Analysis and Design of Algorithms
Patterns Algorithms
Analysis and Design of Algorithms
Pattern searching
Naive Pattern Searching
Regular Expression
Analysis and Design of Algorithms
Pattern searching is an important problem in
computer science. When we do search for a string in
notepad/word file or browser or database, pattern
searching algorithms are used to show the search
results.
Analysis and Design of Algorithms
Naive Pattern Searching
Analysis and Design of Algorithms
Slide the pattern over text one by one and
check for a match. If a match is found, then
slides by 1 again to check for subsequent
matches.
Analysis and Design of Algorithms
Example 1:
Input: txt[] = "THIS IS A TEST TEXT"
pat[] = "TEST"
Output: Pattern found at index 10
Analysis and Design of Algorithms
Example 2:
Input: txt[] = "AABAACAADAABAABA"
pat[] = "AABA"
Output: Pattern found at index 0
Pattern found at index 9
Pattern found at index 12
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A

j
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A

j
i i+j
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A

j
i i+j
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A

j
i i+j
Analysis and Design of Algorithms
 Pattern found at index 0
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Pattern found at index 9
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Pattern found at index 12
A A B A A C A A D A A B A A B A
A A B A
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A B
i
Analysis and Design of Algorithms
 Compare
A A B A A C A A D A A B A A B A
A A
i
Analysis and Design of Algorithms
Input: txt[] = "AABAACAADAABAABA"
pat[] = "AABA"
Output: Pattern found at index 0
Pattern found at index 9
Pattern found at index 12
Analysis and Design of Algorithms
 Python Code:
Analysis and Design of Algorithms
Analysis and Design of Algorithms
What is the best case?
The best case occurs when the first character of the pattern
is not present in text at all.
txt[] = "AABCCAADDEE"
pat[] = "FAA"
Analysis and Design of Algorithms
What is the worst case ?
1) When all characters of the text and pattern are same.
txt[] = "AAAAAAAAAAAAAAAAAA"
pat[] = "AAAAA"
Analysis and Design of Algorithms
2) Worst case also occurs when only the last character is
different.
txt[] = "AAAAAAAAAAAAAAAAAB"
pat[] = "AAAAB"
Analysis and Design of Algorithms
The worst case is O(m*(n-m+1))
Analysis and Design of Algorithms
Regular Expression
Analysis and Design of Algorithms
Regular expressions are a
powerful language for matching
text patterns.
Analysis and Design of Algorithms
re.match() checks for a match only at the beginning of
the string.
re.search() checks for a match anywhere in the string.
Analysis and Design of Algorithms
Import Regular Expression (Python)
Analysis and Design of Algorithms
In Python a regular expression search is
typically written as:
 match = re.search(pat, str)
Analysis and Design of Algorithms
Simple match
Analysis and Design of Algorithms
w (lowercase w): matches a "word" character:
a letter or digit or underbar [a-z A-Z 0-9 _].
Analysis and Design of Algorithms
W (upper case W): matches any non-word
character.
Analysis and Design of Algorithms
d (lowercase d): decimal digit [0-9]
Analysis and Design of Algorithms
s (lowercase s): matches a single whitespace
character -- space, newline, return, tab, form [
nrtf].
Analysis and Design of Algorithms
 . (dot) : matches any single character except
newline 'n'
Analysis and Design of Algorithms
 ^ = start : match the start
Match is empty
Analysis and Design of Algorithms
 $ = end : match the end of the string
Match is empty
Analysis and Design of Algorithms
 [ ] : a range of characters can be
indicated by giving two characters and
separating them by a “-”.
Analysis and Design of Algorithms
 [ ]
Analysis and Design of Algorithms
 [^ ] : will match any character except this.
Analysis and Design of Algorithms
 {n} : match exactly with any number of n.
Analysis and Design of Algorithms
 {n,m} : match exactly with any number of n to m.
Analysis and Design of Algorithms
Syntax Description Equivalent
d Matches any decimal digit [0-9]
D Matches any non-digit character [^0-9]
s Matches any whitespace character [ tnrfv]
S Matches any non-whitespace character [^ tnrfv]
w Matches any alphanumeric character [a-zA-Z0-9_]
W Matches any non-alphanumeric character [^a-zA-Z0-9_]
Analysis and Design of Algorithms
+ : 1 or more occurrences of the pattern
Analysis and Design of Algorithms
* : 0 or more occurrences of the pattern
Analysis and Design of Algorithms
? : match 0 or 1 occurrences of the pattern
Analysis and Design of Algorithms
 Emails:
Analysis and Design of Algorithms
 Emails:
 Square brackets can be used to indicate a set of chars, so [abc]
matches 'a' or 'b' or 'c'.
Analysis and Design of Algorithms
 Emails:
 findall() finds all the matches and returns them as a list of strings.
Analysis and Design of Algorithms
( ): match group of pattern
Analysis and Design of Algorithms
Operators Description
. Matches with any single character except newline ‘n’
? Match 0 or 1 occurrence of the pattern to its left
+ Match 1 or more occurrences of the pattern to its left
* Match 0 or more occurrences of the pattern to its left
w Matches with a alphanumeric character
W Matches non alphanumeric character
d Matches with digits [0-9]
D Matches with non-digits
Analysis and Design of Algorithms
Operators Description
s Matches with a single white space character (space, newline, tab)
S Matches any non-white space character
[..] Matches any single character in a square bracket
[^..] Matches any single character not in square bracket
 It is used for special meaning characters
^ and $ ^ and $ match the start or end of the string respectively
{n,m} Matches at least n and at most m occurrences of expression
a| b Matches either a or b
Analysis and Design of Algorithms
Operators Description
( ) Groups regular expressions and returns matched text
t, n, r Matches tab, newline, return
Analysis and Design of Algorithms
Validate a phone number (phone number must be of
10 digits and starts with 8 or 9)
Analysis and Design of Algorithms
 Solution
Analysis and Design of Algorithms
Return date from given string
str= Amit 34-3456 12-05-2007, XYZ 56-4532 11-
11-2011
Analysis and Design of Algorithms
 Solution
Analysis and Design of Algorithms
facebook.com/mloey
mohamedloey@gmail.com
twitter.com/mloey
linkedin.com/in/mloey
mloey@fci.bu.edu.eg
mloey.github.io
Analysis and Design of Algorithms
www.YourCompany.com
© 2020 Companyname PowerPoint Business Theme. All Rights Reserved.
THANKS FOR
YOUR TIME

More Related Content

What's hot (20)

Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
subhashchandra197
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Stuart russell and peter norvig artificial intelligence - a modern approach...
Stuart russell and peter norvig   artificial intelligence - a modern approach...Stuart russell and peter norvig   artificial intelligence - a modern approach...
Stuart russell and peter norvig artificial intelligence - a modern approach...
Lê Anh Đạt
 
Heaps
HeapsHeaps
Heaps
Hafiz Atif Amin
 
K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)
Abdullah al Mamun
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Time complexity
Time complexityTime complexity
Time complexity
Katang Isip
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
shashidharPapishetty
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentation
Ratul Hasan
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
Dr Shashikant Athawale
 
Brute force-algorithm
Brute force-algorithmBrute force-algorithm
Brute force-algorithm
9854098540
 
b+ tree
b+ treeb+ tree
b+ tree
bitistu
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
Rajendran
 
Natural Language Processing (NLP) - Introduction
Natural Language Processing (NLP) - IntroductionNatural Language Processing (NLP) - Introduction
Natural Language Processing (NLP) - Introduction
Aritra Mukherjee
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMS
koolkampus
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Stuart russell and peter norvig artificial intelligence - a modern approach...
Stuart russell and peter norvig   artificial intelligence - a modern approach...Stuart russell and peter norvig   artificial intelligence - a modern approach...
Stuart russell and peter norvig artificial intelligence - a modern approach...
Lê Anh Đạt
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentation
Ratul Hasan
 
Brute force-algorithm
Brute force-algorithmBrute force-algorithm
Brute force-algorithm
9854098540
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
Rajendran
 
Natural Language Processing (NLP) - Introduction
Natural Language Processing (NLP) - IntroductionNatural Language Processing (NLP) - Introduction
Natural Language Processing (NLP) - Introduction
Aritra Mukherjee
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMS
koolkampus
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 

Similar to Algorithms Lecture 8: Pattern Algorithms (20)

Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
Chirag Shetty
 
Project
ProjectProject
Project
Guilherme Torres
 
String
StringString
String
Dr. Abhineet Anand
 
Quick start reg ex
Quick start reg exQuick start reg ex
Quick start reg ex
V krishnamoorthy
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
Mukesh Tekwani
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Eran Zimbler
 
Array and string
Array and stringArray and string
Array and string
prashant chelani
 
regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdf
DarellMuchoko
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
mussawir20
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
Terry Yoast
 
Ch09
Ch09Ch09
Ch09
Arriz San Juan
 
string in C
string in Cstring in C
string in C
CGC Technical campus,Mohali
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
ssusere19c741
 
Matlab: Procedures And Functions
Matlab: Procedures And FunctionsMatlab: Procedures And Functions
Matlab: Procedures And Functions
matlab Content
 
Procedures And Functions in Matlab
Procedures And Functions in MatlabProcedures And Functions in Matlab
Procedures And Functions in Matlab
DataminingTools Inc
 
Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5
bilcorry
 
C-Pubb-TestingExperience_Issue_08_2008
C-Pubb-TestingExperience_Issue_08_2008C-Pubb-TestingExperience_Issue_08_2008
C-Pubb-TestingExperience_Issue_08_2008
Berta Danilo
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Expresiones regulares, sintaxis y programación en JAVA
Expresiones regulares, sintaxis y programación en JAVAExpresiones regulares, sintaxis y programación en JAVA
Expresiones regulares, sintaxis y programación en JAVA
Oscar743056
 
Exploratory data analysis project
Exploratory data analysis project Exploratory data analysis project
Exploratory data analysis project
BabatundeSogunro
 
Ad

More from Mohamed Loey (20)

Lecture 6: Deep Learning Applications
Lecture 6: Deep Learning ApplicationsLecture 6: Deep Learning Applications
Lecture 6: Deep Learning Applications
Mohamed Loey
 
Lecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network ModelsLecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network Models
Mohamed Loey
 
Lecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksLecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning Frameworks
Mohamed Loey
 
Lecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural NetworksLecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural Networks
Mohamed Loey
 
Lecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural NetworksLecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural Networks
Mohamed Loey
 
Lecture 2: Artificial Neural Network
Lecture 2: Artificial Neural NetworkLecture 2: Artificial Neural Network
Lecture 2: Artificial Neural Network
Mohamed Loey
 
Lecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer VisionLecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer Vision
Mohamed Loey
 
Design of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer DiseasesDesign of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer Diseases
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1
Mohamed Loey
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Convolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep LearningConvolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep Learning
Mohamed Loey
 
Deep Learning - Overview of my work II
Deep Learning - Overview of my work IIDeep Learning - Overview of my work II
Deep Learning - Overview of my work II
Mohamed Loey
 
Computer Security Lecture 7: RSA
Computer Security Lecture 7: RSAComputer Security Lecture 7: RSA
Computer Security Lecture 7: RSA
Mohamed Loey
 
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption StandardComputer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Mohamed Loey
 
Computer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary MaterialComputer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary Material
Mohamed Loey
 
PMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration ManagementPMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration Management
Mohamed Loey
 
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption StandardComputer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Mohamed Loey
 
Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2
Mohamed Loey
 
Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 2: Classical Encryption Techniques 1Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 2: Classical Encryption Techniques 1
Mohamed Loey
 
Lecture 6: Deep Learning Applications
Lecture 6: Deep Learning ApplicationsLecture 6: Deep Learning Applications
Lecture 6: Deep Learning Applications
Mohamed Loey
 
Lecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network ModelsLecture 5: Convolutional Neural Network Models
Lecture 5: Convolutional Neural Network Models
Mohamed Loey
 
Lecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksLecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning Frameworks
Mohamed Loey
 
Lecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural NetworksLecture 4: How it Works: Convolutional Neural Networks
Lecture 4: How it Works: Convolutional Neural Networks
Mohamed Loey
 
Lecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural NetworksLecture 3: Convolutional Neural Networks
Lecture 3: Convolutional Neural Networks
Mohamed Loey
 
Lecture 2: Artificial Neural Network
Lecture 2: Artificial Neural NetworkLecture 2: Artificial Neural Network
Lecture 2: Artificial Neural Network
Mohamed Loey
 
Lecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer VisionLecture 1: Deep Learning for Computer Vision
Lecture 1: Deep Learning for Computer Vision
Mohamed Loey
 
Design of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer DiseasesDesign of an Intelligent System for Improving Classification of Cancer Diseases
Design of an Intelligent System for Improving Classification of Cancer Diseases
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 2
Mohamed Loey
 
Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1Computer Security - CCNA Security - Lecture 1
Computer Security - CCNA Security - Lecture 1
Mohamed Loey
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Convolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep LearningConvolutional Neural Network Models - Deep Learning
Convolutional Neural Network Models - Deep Learning
Mohamed Loey
 
Deep Learning - Overview of my work II
Deep Learning - Overview of my work IIDeep Learning - Overview of my work II
Deep Learning - Overview of my work II
Mohamed Loey
 
Computer Security Lecture 7: RSA
Computer Security Lecture 7: RSAComputer Security Lecture 7: RSA
Computer Security Lecture 7: RSA
Mohamed Loey
 
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption StandardComputer Security Lecture 5: Simplified Advanced Encryption Standard
Computer Security Lecture 5: Simplified Advanced Encryption Standard
Mohamed Loey
 
Computer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary MaterialComputer Security Lecture 4.1: DES Supplementary Material
Computer Security Lecture 4.1: DES Supplementary Material
Mohamed Loey
 
PMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration ManagementPMP Lecture 4: Project Integration Management
PMP Lecture 4: Project Integration Management
Mohamed Loey
 
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption StandardComputer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Mohamed Loey
 
Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 3: Classical Encryption Techniques 2
Mohamed Loey
 
Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 2: Classical Encryption Techniques 1Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 2: Classical Encryption Techniques 1
Mohamed Loey
 
Ad

Recently uploaded (20)

IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 

Algorithms Lecture 8: Pattern Algorithms

  • 1. Analysis and Design of Algorithms Patterns Algorithms
  • 2. Analysis and Design of Algorithms Pattern searching Naive Pattern Searching Regular Expression
  • 3. Analysis and Design of Algorithms Pattern searching is an important problem in computer science. When we do search for a string in notepad/word file or browser or database, pattern searching algorithms are used to show the search results.
  • 4. Analysis and Design of Algorithms Naive Pattern Searching
  • 5. Analysis and Design of Algorithms Slide the pattern over text one by one and check for a match. If a match is found, then slides by 1 again to check for subsequent matches.
  • 6. Analysis and Design of Algorithms Example 1: Input: txt[] = "THIS IS A TEST TEXT" pat[] = "TEST" Output: Pattern found at index 10
  • 7. Analysis and Design of Algorithms Example 2: Input: txt[] = "AABAACAADAABAABA" pat[] = "AABA" Output: Pattern found at index 0 Pattern found at index 9 Pattern found at index 12
  • 8. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A  j i
  • 9. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A  j i i+j
  • 10. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A  j i i+j
  • 11. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A  j i i+j
  • 12. Analysis and Design of Algorithms  Pattern found at index 0 A A B A A C A A D A A B A A B A A A B A i
  • 13. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A i
  • 14. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A i
  • 15. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A i
  • 16. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A i
  • 17. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A i
  • 18. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A i
  • 19. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A i
  • 20. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A i
  • 21. Analysis and Design of Algorithms  Pattern found at index 9 A A B A A C A A D A A B A A B A A A B A i
  • 22. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A i
  • 23. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B A i
  • 24. Analysis and Design of Algorithms  Pattern found at index 12 A A B A A C A A D A A B A A B A A A B A i
  • 25. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A B i
  • 26. Analysis and Design of Algorithms  Compare A A B A A C A A D A A B A A B A A A i
  • 27. Analysis and Design of Algorithms Input: txt[] = "AABAACAADAABAABA" pat[] = "AABA" Output: Pattern found at index 0 Pattern found at index 9 Pattern found at index 12
  • 28. Analysis and Design of Algorithms  Python Code:
  • 29. Analysis and Design of Algorithms
  • 30. Analysis and Design of Algorithms What is the best case? The best case occurs when the first character of the pattern is not present in text at all. txt[] = "AABCCAADDEE" pat[] = "FAA"
  • 31. Analysis and Design of Algorithms What is the worst case ? 1) When all characters of the text and pattern are same. txt[] = "AAAAAAAAAAAAAAAAAA" pat[] = "AAAAA"
  • 32. Analysis and Design of Algorithms 2) Worst case also occurs when only the last character is different. txt[] = "AAAAAAAAAAAAAAAAAB" pat[] = "AAAAB"
  • 33. Analysis and Design of Algorithms The worst case is O(m*(n-m+1))
  • 34. Analysis and Design of Algorithms Regular Expression
  • 35. Analysis and Design of Algorithms Regular expressions are a powerful language for matching text patterns.
  • 36. Analysis and Design of Algorithms re.match() checks for a match only at the beginning of the string. re.search() checks for a match anywhere in the string.
  • 37. Analysis and Design of Algorithms Import Regular Expression (Python)
  • 38. Analysis and Design of Algorithms In Python a regular expression search is typically written as:  match = re.search(pat, str)
  • 39. Analysis and Design of Algorithms Simple match
  • 40. Analysis and Design of Algorithms w (lowercase w): matches a "word" character: a letter or digit or underbar [a-z A-Z 0-9 _].
  • 41. Analysis and Design of Algorithms W (upper case W): matches any non-word character.
  • 42. Analysis and Design of Algorithms d (lowercase d): decimal digit [0-9]
  • 43. Analysis and Design of Algorithms s (lowercase s): matches a single whitespace character -- space, newline, return, tab, form [ nrtf].
  • 44. Analysis and Design of Algorithms  . (dot) : matches any single character except newline 'n'
  • 45. Analysis and Design of Algorithms  ^ = start : match the start Match is empty
  • 46. Analysis and Design of Algorithms  $ = end : match the end of the string Match is empty
  • 47. Analysis and Design of Algorithms  [ ] : a range of characters can be indicated by giving two characters and separating them by a “-”.
  • 48. Analysis and Design of Algorithms  [ ]
  • 49. Analysis and Design of Algorithms  [^ ] : will match any character except this.
  • 50. Analysis and Design of Algorithms  {n} : match exactly with any number of n.
  • 51. Analysis and Design of Algorithms  {n,m} : match exactly with any number of n to m.
  • 52. Analysis and Design of Algorithms Syntax Description Equivalent d Matches any decimal digit [0-9] D Matches any non-digit character [^0-9] s Matches any whitespace character [ tnrfv] S Matches any non-whitespace character [^ tnrfv] w Matches any alphanumeric character [a-zA-Z0-9_] W Matches any non-alphanumeric character [^a-zA-Z0-9_]
  • 53. Analysis and Design of Algorithms + : 1 or more occurrences of the pattern
  • 54. Analysis and Design of Algorithms * : 0 or more occurrences of the pattern
  • 55. Analysis and Design of Algorithms ? : match 0 or 1 occurrences of the pattern
  • 56. Analysis and Design of Algorithms  Emails:
  • 57. Analysis and Design of Algorithms  Emails:  Square brackets can be used to indicate a set of chars, so [abc] matches 'a' or 'b' or 'c'.
  • 58. Analysis and Design of Algorithms  Emails:  findall() finds all the matches and returns them as a list of strings.
  • 59. Analysis and Design of Algorithms ( ): match group of pattern
  • 60. Analysis and Design of Algorithms Operators Description . Matches with any single character except newline ‘n’ ? Match 0 or 1 occurrence of the pattern to its left + Match 1 or more occurrences of the pattern to its left * Match 0 or more occurrences of the pattern to its left w Matches with a alphanumeric character W Matches non alphanumeric character d Matches with digits [0-9] D Matches with non-digits
  • 61. Analysis and Design of Algorithms Operators Description s Matches with a single white space character (space, newline, tab) S Matches any non-white space character [..] Matches any single character in a square bracket [^..] Matches any single character not in square bracket It is used for special meaning characters ^ and $ ^ and $ match the start or end of the string respectively {n,m} Matches at least n and at most m occurrences of expression a| b Matches either a or b
  • 62. Analysis and Design of Algorithms Operators Description ( ) Groups regular expressions and returns matched text t, n, r Matches tab, newline, return
  • 63. Analysis and Design of Algorithms Validate a phone number (phone number must be of 10 digits and starts with 8 or 9)
  • 64. Analysis and Design of Algorithms  Solution
  • 65. Analysis and Design of Algorithms Return date from given string str= Amit 34-3456 12-05-2007, XYZ 56-4532 11- 11-2011
  • 66. Analysis and Design of Algorithms  Solution
  • 67. Analysis and Design of Algorithms facebook.com/mloey [email protected] twitter.com/mloey linkedin.com/in/mloey [email protected] mloey.github.io
  • 68. Analysis and Design of Algorithms www.YourCompany.com © 2020 Companyname PowerPoint Business Theme. All Rights Reserved. THANKS FOR YOUR TIME