0% found this document useful (0 votes)
10 views

EI-331 - Design and Analysis of Algorithms - String Matching

The document discusses string matching algorithms, highlighting their importance in various fields and detailing two specific algorithms: the Naive String Matching Algorithm and the Knuth-Morris-Pratt (KMP) Algorithm, with their respective complexities. It also covers the basic properties of graphs, including vertices, edges, weights, and paths, emphasizing their applications in computer science and other domains. Additionally, it touches on graph representation and the differences between trees and graphs.

Uploaded by

abbastayyab574
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

EI-331 - Design and Analysis of Algorithms - String Matching

The document discusses string matching algorithms, highlighting their importance in various fields and detailing two specific algorithms: the Naive String Matching Algorithm and the Knuth-Morris-Pratt (KMP) Algorithm, with their respective complexities. It also covers the basic properties of graphs, including vertices, edges, weights, and paths, emphasizing their applications in computer science and other domains. Additionally, it touches on graph representation and the differences between trees and graphs.

Uploaded by

abbastayyab574
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

EI-331

Design and Analysis of


Algorithms
EI-331 Design & Analysis of
Algorithms
String Matching
 String matching is a fundamental problem in computer science, with
applications in various fields including text processing, bioinformatics,
and data retrieval.
 It involves finding occurrences of a substring (pattern) within a larger
string (text).
 String matching algorithms aim to efficiently locate these occurrences.
EI-331 Design & Analysis of
Algorithms
String Matching
 Text: The larger string in which we want to find occurrences of a smaller
string (pattern).
 Pattern: The smaller string we are searching for within the text.
 Occurrence: A position in the text where an exact match of the pattern
is found.
EI-331 Design & Analysis of
Algorithms
String Matching
 Naive String Matching Algorithm – Brute-Force Algorithm
 The simplest approach to string matching.
 Iterate through the text one character at a time and compare it with the
pattern.
 If a match is found, check if the subsequent characters also match the
pattern.
 Complexity: O((n-m+1)*m) where n is the length of the text and m is the
length of the pattern.
EI-331 Design & Analysis of Algorithms
String Matching (Brute – Force Algorithm)
EI-331 Design & Analysis of
Algorithms
String Matching
 Knuth-Morris-Pratt (KMP) Algorithm
 Efficient string matching algorithm based on the concept of "failure
function" or "prefix function".
 Preprocess the pattern to determine the longest proper prefix of the
pattern that is also a suffix.
 Use this information to avoid unnecessary comparisons in the text.
 Complexity: O(n + m), where n is the length of the text and m is the
length of the pattern.
EI-331 Design & Analysis of Algorithms
String Matching
(Knuth-Morris-Pratt (KMP) Algorithm)

a b a c a a b a c c a b a c a b a a b b
1 2 3 4 5 6
a b a c a b
7
a b a c a b
8 9 10 11 12
a b a c a b
13
j 0 1 2 3 4 5 a b a c a b
P[j] a b a c a b 14 15 16 17 18 19
F(j) 0 0 1 0 1 2 a b a c a b
EI-331 Design & Analysis of
Algorithms
String Matching
 Knuth-Morris-Pratt (KMP) Algorithm
 Preprocess the pattern to compute the failure function.
 Use the failure function to guide the search in the text.
 Achieves linear time complexity.
EI-331 Design & Analysis of
Algorithms
Data Structure Revision
EI-331 Design & Analysis of
Algorithms
Graphs
 A graph is a data structure composed of nodes (vertices) and edges
that connect pairs of nodes.
 Nodes represent entities, and edges represent relationships between
these entities.
 Graphs are versatile and widely used in various fields, including
computer science, social networks, transportation systems, and more.
EI-331 Design & Analysis of
Algorithms
The basic properties of a graph include:
Graphs
Vertices (nodes): The points where edges meet in a graph are known as vertices or nodes. A
vertex can represent a physical object, concept, or abstract entity.
Edges: The connections between vertices are known as edges. They can be undirected
(bidirectional) or directed (unidirectional).
Weight: A weight can be assigned to an edge, representing the cost or distance between two
vertices. A weighted graph is a graph where the edges have weights.
Degree: The degree of a vertex is the number of edges that connect to it. In a directed graph,
the in-degree of a vertex is the number of edges that point to it, and the out-degree is the
number of edges that start from it.
Path: A path is a sequence of vertices that are connected by edges. A simple path does not
contain any repeated vertices or edges.
Cycle: A cycle is a path that starts and ends at the same vertex. A simple cycle does not
contain any repeated vertices or edges.
Connectedness: A graph is said to be connected if there is a path between any two vertices. A
disconnected graph is a graph that is not connected.
EI-331 Design & Analysis of
Algorithms
Graphs
EI-331 Design & Analysis of
Algorithms
Graphs
EI-331 Design & Analysis of
Algorithms
Graphs Representation
EI-331 Design & Analysis of
Algorithms
Graphs Representation
EI-331 Design & Analysis of
Algorithms
Tree vs Graphs
EI-331 Design & Analysis of
Algorithms
Graphs Search
EI-331 Design & Analysis of
Algorithms
Graphs Search

You might also like