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

Module 1 ADA

Module 1 of BCS401 covers the fundamentals of algorithms, including definitions, characteristics, and various methods for computing the greatest common divisor (GCD). It outlines the steps involved in designing and analyzing algorithms, emphasizing the importance of understanding the problem, choosing appropriate techniques, and proving correctness. Additionally, it discusses efficiency analysis, including time and space complexity, and introduces asymptotic notations for comparing algorithm performance.

Uploaded by

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

Module 1 ADA

Module 1 of BCS401 covers the fundamentals of algorithms, including definitions, characteristics, and various methods for computing the greatest common divisor (GCD). It outlines the steps involved in designing and analyzing algorithms, emphasizing the importance of understanding the problem, choosing appropriate techniques, and proving correctness. Additionally, it discusses efficiency analysis, including time and space complexity, and introduces asymptotic notations for comparing algorithm performance.

Uploaded by

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

Analysis & Design of Algorithms Module 1 BCS401

MODULE-1

INTRODUCTION

What Is an Algorithm?
(Define algorithm and also discuss the characteristics of an algorithm 5M)
An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for
obtaining a required output for any legitimate input in a finite amount of time.
This definition can be illustrated by a simple diagram,

Characteristics of the Algorithm:


➢ The non-ambiguity requirement for each step of an algorithm cannot be compromised.
➢ The range of inputs for which an algorithm works has to be specified carefully.
➢ The same algorithm can be represented in several different ways.
➢ There may exist several algorithms for solving the same problem.
➢ Algorithms for the same problem can be based on very different ideas and can solve
the problem with dramatically different speeds.

Example:1
(Design Euclid’s algorithm for computing GCD (m,n). Find GCD (60, 24) using
Euclid’s Algorithm 10 M)
Let us see different methods to find greatest common divisor (GCD)
1. Euclid’s algorithm for computing gcd(m, n)
Step 1 If n = 0, return the value of m as the answer and stop; otherwise, proceed to Step 2.
Step 2 Divide m by n and assign the value of the remainder to r.
Step 3 Assign the value of n to m and the value of r to n. Go to Step 1.

Dept., of CSE Page 1


Analysis & Design of Algorithms Module 1 BCS401

ALGORITHM Euclid(m, n)
//Computes gcd(m, n) by Euclid’s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do
r ←m mod n m←n
n←r
return m

2. Consecutive integer checking algorithm for computing gcd(m, n)


Step 1 Assign the value of min{m, n} to t.
Step 2 Divide m by t. If the remainder of this division is 0, go to Step 3; otherwise, go to
step 4.
Step 3 Divide n by t. If the remainder of this division is 0, return the value of t as the
answer and stop; otherwise, proceed to Step 4.
Step 4 Decrease the value of t by 1. Go to Step 2.
Note: This algorithm does not work correctly when one of its input numbers is zero.

3. Middle-school procedure for computing gcd(m, n)

Step 1 Find the prime factors of m.


Step 2 Find the prime factors of n.
Step 3 Identify all the common factors in the two prime expansions found in Step 1 and
Step 2. (If p is a common factor occurring pm and pn times in m and n, respectively, it
should be repeated min{pm, pn} times.)
Step 4 Compute the product of all the common factors and return it as the greatest
common divisor of the numbers given.

Note: This algorithm doesn’t specify how to find prime factorization

Dept., of CSE Page 2


Analysis & Design of Algorithms Module 1 BCS401

Fundamentals of Algorithmic Problem Solving


(Briefly discuss the sequence of steps involved in designing and analyzing an
algorithm 10M)
We can consider algorithms to be procedural solutions to problems. These solutions are not
answers but specific instructions for getting answers.
Sequence of steps one typically goes through in designing and analyzing an
algorithm is shown below figure:

Dept., of CSE Page 3


Analysis & Design of Algorithms Module 1 BCS401

1. Understanding the Problem

Before designing an algorithm is to understand completely the problem given. Read the
problem’s description carefully and ask questions if you have any doubts about the problem,
do a few small examples by hand, think about special cases, and ask questions again if needed.
2. Ascertaining the Capabilities of the Computational Device
Once you completely understand a problem, you need to ascertain the capabilities of the
computational device the algorithm is intended for.

Random-access machine (RAM). Its central assumption is that instructions are executed one
after another, one operation at a time. Accordingly, algorithms designed to be executed on such
machines are called sequential algorithms.

The central assumption of the RAM model does not hold for some newer computers that can
execute operations concurrently, i.e., in parallel. Algorithms that take advantage of this
capability are called parallel algorithms.
3. Choosing between Exact and Approximate Problem Solving

The next principal decision is to choose between solving the problem exactly or solving it
approximately.

In the former case, an algorithm is called an exact algorithm; in the latter case, an algorithm is
called an approximation algorithm.

Exact algorithms give precise answers, while approximation algorithms offer close but not
perfect solutions.
Ex: People might go for approximation algorithms because some problems are just too hard to
solve exactly, like finding square roots or solving complex equations.
Problems like sorting, searching need exact solutions.

4. Algorithm Design Techniques


An algorithm design technique (or “strategy” or “paradigm”) is a general approach to
solving problems algorithmically that is applicable to a variety of problems from different
areas of computing. Ex: Brute force, Divide-and- Conquer etc.

5. Designing an Algorithm and Data Structures


• The process of designing algorithms for specific problems can be challenging, as some
design techniques may not be applicable or may require ingenuity to implement
effectively.
Dept., of CSE Page 4
Analysis & Design of Algorithms Module 1 BCS401

• Combining multiple techniques and choosing appropriate data structures are


crucial steps in algorithm design, as they significantly impact the efficiency of the
solution. Attention to selecting suitable data structures is paramount, as improper choices
can lead to inefficiencies in algorithm performance.

6. Methods of Specifying an Algorithm


The two primary methods for specifying algorithms are natural language and pseudocode.

• Natural language appeals to simplicity, its inherent ambiguity can make describing
algorithms difficult.
• Pseudocode, blending natural language with programming-like constructs, offers more
precision and conciseness in algorithm descriptions. Although various pseudocode
dialects exist, they are generally easy to understand for those familiar with modern
programming languages. Despite advancements, algorithms described in natural
language or pseudocode must ultimately be translated into computer programs for
execution

7. Proving an Algorithm’s Correctness

Once an algorithm has been specified, you have to prove its correctness. That is, you have to
prove that the algorithm yields a required result for every legitimate input in a finite amount of
time.

For some algorithms, a proof of correctness is quite easy; for others, it can be quite complex.
A common technique for proving correctness is to use mathematical induction because an
algorithm’s iterations provide a natural sequence of steps needed for such proofs.

But in order to show that an algorithm is incorrect, you need just one instance of its input for
which the algorithm fails.

8. Analyzing an Algorithm
We usually want our algorithms to possess several qualities. After correctness, by far the most
important is efficiency. In fact, there are two kinds of algorithm efficiency: time efficiency,
indicating how fast the algorithm runs, and space efficiency, indicating how much extra
memory it uses.
Other desirable characteristic of an algorithm is simplicity- simple algorithms are easier to
understand and program, and generality- of the problem and range of inputs.
Dept., of CSE Page 5
Analysis & Design of Algorithms Module 1 BCS401

9. Coding an Algorithm

Most algorithms are destined to be ultimately implemented as computer programs.

Programming an algorithm presents both a peril and an opportunity.

As a practical matter, the validity of programs is still established by testing. Testing of


computer programs is an art rather than a science

Dept., of CSE Page 6


Analysis & Design of Algorithms Module 1 BCS401

Fundamentals of the Analysis of Algorithm Efficiency

The Analysis Framework


There are two kinds of efficiency: time efficiency and space efficiency.
• Time efficiency, also called time complexity, indicates how fast an algorithm in question
runs.
• Space efficiency, also called space complexity, refers to the amount of memory units
required by the algorithm in addition to the space needed for its input and output.

In the early days of electronic computing, both resources—time and space— were at a
premium. But the technological innovations have improved the computer’s speed and
memory size. Now space efficiency is not much concerned compared to speed.

We primarily concentrate on time efficiency,

➢ Measuring an Input’s Size


➢ Units for Measuring Running Time
➢ Orders of Growth

Measuring an Input’s Size


Almost all algorithms run longer on larger inputs. For example, it takes longer to sort larger
arrays, multiply larger matrices, and so on. Therefore, it is logical to investigate an algorithm’s
efficiency as a function of some parameter n indicating the algorithm’s input size.

Ex: For searching, finding the list’s smallest element, and most other problems dealing with
lists input size is size of the list.
In certain scenarios, the choice input size significantly impacts the assessment of algorithm
efficiency. One such example is the computation of the product of two n × n matrices. There are
two common measures of input size for this problem: Matrix Order & Total Number of
elements.

Dept., of CSE Page 7


Analysis & Design of Algorithms Module 1 BCS401

Units for Measuring Running Time


We can simply use some standard unit of time measurement—a second, or millisecond, and so
on—to measure the running time of a program implementing the algorithm. There are obvious
drawbacks such as:

• Dependence on the speed of a particular computer,


• Dependence on the quality of a program implementing the algorithm and
• The compiler used in generating the machine code, and
• The difficulty of clocking the actual running time of the program. So, we need
a metric that does not depend on these extraneous factors.
One possible approach is to identify the most important operation of the algorithm, called the
basic operation, the operation contributing the most to the total running time, and compute the
number of times the basic operation is executed.
Ex: In searching operation, basic operation is comparison.
(Explain the basic operation with formula 4M)

Let Cop be the execution time of an algorithm’s basic operation on a particular computer, and let
C(n) be the number of times this operation needs to be executed for this algorithm. Then we can
estimate the running time T (n) of a program implementing this algorithm on that computer
by the formula,
T (n) ≈ Cop .C(n)

Example:
Let C(n)=1/2n(n-1). How much longer will the algorithm run if we double its input size?
The Answer is 4 because,

Dept., of CSE Page 8


Analysis & Design of Algorithms Module 1 BCS401

Orders of Growth

We expect the algorithms to work faster for all values of n. But some algorithm executes faster
for smaller values of n, but as the value of n increases, they tend to be very slow.
“This change in behaviour as the value of n increases is called order of growth”

Ex: If the running time T(n) of an algorithm varies linearly with increase or decrease in the
value of n, the order of growth is called as linear.

The concept of order of growth can be clearly understood by considering the common
computing time functions as shown in the table below:

From the table we can observe that log2n function grows very slowly compare to 2n (which is
exponential). All the above functions can be ordered according to their order of growth (from
lowest to highest) as shown below:

log n< n< n log n<n2<n3<2n<n!

Basic Efficiency Classes


Even though the efficiency analysis framework puts together all the functions whose orders of
growth differ by a constant multiple, there are still infinitely many such classes.

Dept., of CSE Page 9


Analysis & Design of Algorithms Module 1 BCS401

Dept., of CSE Page 10


Analysis & Design of Algorithms Module 1 BCS401

Worst-Case, Best-Case, and Average-Case Efficiencies


(Develop an algorithm to search an element in an array using sequential search.
Calculate the best case, worst case and average case efficiency of this algorithm
10M)
But there are many algorithms for which running time depends not only on an input size but
also on the specifics of a particular input.

Consider, as an example, sequential search. This is a straightforward algorithm that searches


for a given item (some search key K) in a list of n elements by checking successive elements
of the list until either a match with the search key is found or the list is exhausted.

Worst-case efficiency:
Definition: The worst-case efficiency of an algorithm is its efficiency for the worst-case input
of size n, which is an input (or inputs) of size n for which the algorithm runs the longest among
all possible inputs of that size.
Ex: In linear or sequential search operation, the worst case is, when there are no matching
elements or the first matching element happens to be the last one on the list, the algorithm makes
the largest number of key comparisons among all possible inputs of size n:
Therefore Cworst(n) = n.

Best-case efficiency:
Definition: The best-case efficiency of an algorithm is its efficiency for the best- case input of
size n, which is an input (or inputs) of size n for which the algorithm runs the fastest among all
possible inputs of that size.

For example, the best-case inputs for sequential search are lists of size n with their first element
equal to a search key; accordingly,
Cbest(n) = 1

Average-case efficiency:
Some cases neither the worst-case analysis nor its best-case counterpart yields the necessary

Dept., of CSE Page 11


Analysis & Design of Algorithms Module 1 BCS401

information about an algorithm’s behavior on a “typical” or “random” input. This is the


information that the average-case efficiency seeks to provide. To analyze the algorithm’s
average-case efficiency, we must make some assumptions about possible inputs of size n.
Ex: In sequential search, the standard assumptions are:
(a) the probability of a successful search is equal to p (0 ≤ p ≤ 1) and
(b) the probability of the first match occurring in the ith position of the list is the same for
every i.
In the case of a successful search, the probability of the first match occurring in the ith position
of the list is p/n for every i, and the number of comparisons made by the algorithm in such a
situation is obviously i. In the case of an unsuccessful search, the number of comparisons will be
n with the probability of such a search being (1− p). Therefore,

Asymptotic Notations and Basic Efficiency Classes


(1. Explain asymptotic notations Big Oh, Big Omega and Big Theta notations
8M)
(2. What are the various basic efficiency classes? Explain Big oh, Big Omega,
Big Theta notations with example 10M)
(3. Explain Asymptotic notations with example 10M)

The efficiency analysis framework concentrates on the order of growth of an algorithm’s


basic operation count as the principal indicator of the algorithm’s efficiency.
To compare and rank such orders of growth, computer scientists use three notations:

➢ (big oh),
➢ Ω (big omega), and
➢ Θ (big theta).

O (big oh)

Dept., of CSE Page 12


Analysis & Design of Algorithms Module 1 BCS401

Ω -notation

Dept., of CSE Page 13


Analysis & Design of Algorithms Module 1 BCS401

Θ (big theta).

Dept., of CSE Page 14


Analysis & Design of Algorithms Module 1 BCS401

Useful Property Involving the Asymptotic Notations


(If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then show that t1(n) + t2(n) ∈
O(max{g1(n), g2(n)}). 4M)

Using the formal definitions of the asymptotic notations, we can prove their general properties. The
following property, in particular, is useful in analyzing algorithms that comprise two consecutively
executed parts.

Dept., of CSE Page 15


Analysis & Design of Algorithms Module 1 BCS401

Using Limits for Comparing Orders of Growth


Though the formal definitions of O, Ω, and Θ are indispensable for proving their abstract
properties, they are rarely used for comparing the orders of growth of two specific functions.

A much more convenient method for doing so is based on computing the limit of the ratio of
two functions in question.

Three principal cases may arise:

Note that the first two cases mean that t (n) ∈ O(g(n)), the last two mean that t (n) ∈ Ω(g(n)),
and the second case means that t (n) ∈ Θ(g(n)). The limit-based approach is often more
convenient than the one based on the definitions because it can take advantage of the powerful
calculus techniques developed for computing limits, such as L’Hospital’s rule

Dept., of CSE Page 16


Analysis & Design of Algorithms Module 1 BCS401

Mathematical Analysis of Nonrecursive Algorithms


(Explain the general plan for analyzing the efficiency of a non-recursive
algorithm. Suggest a non-recursive algorithm to find maximum element in the
list of n numbers. Derive its efficiency 8M)

General Plan for Analyzing the Time Efficiency of Nonrecursive Algorithms

1. Decide on a parameter (or parameters) indicating an input’s size.


2. Identify the algorithm’s basic operation. (As a rule, it is located in the innermost loop.)
3. Check whether the number of times the basic operation is executed depends only on
the size of an input. If it also depends on some additional property, the worst-case,
average-case, and, if necessary, best-case efficiencies have to be investigated separately.
4. Set up a sum expressing the number of times the algorithm’s basic operation is
executed.
5. Using standard formulas and rules of sum manipulation, either find a closed form
formula for the count or, at the very least, establish its order of growth.

Dept., of CSE Page 17


Analysis & Design of Algorithms Module 1 BCS401

EXAMPLE 1 Consider the problem of finding the value of the largest element in a list of n
numbers. For simplicity, we assume that the list is implemented as an array.
(Write an Algorithm to find maximum of n elements and obtain its time
complexity 8M)

The following is pseudocode of a standard algorithm for solving the problem.

Analysis (Time Complexity):

Input Size: The number of elements in the array, i.e., n.

Basic Operation: the comparison A[i]> maxval

Count of Basic Operation,


C(n):

EXAMPLE 2 Consider the element uniqueness problem: check whether all the elements in
a given array of n elements are distinct.
Dept., of CSE Page 18
Analysis & Design of Algorithms Module 1 BCS401

(Write an algorithm to find the uniqueness of elements in an array and give the
mathematical analysis of this non recursive algorithm with steps? M)

Analysis (Time Complexity):

Input Size: The number of elements in the array, i.e., n.

Basic Operation: the comparison A[i]> A[j]

Count of Basic Operation,


C(n):

Dept., of CSE Page 19


Analysis & Design of Algorithms Module 1 BCS401

EXAMPLE 3 Given two n × n matrices A and B, find the time efficiency of the definition-
based algorithm for computing their product C = AB. By definition, C is an n × n matrix whose
elements are computed as the scalar (dot) products of the rows of matrix A and the columns of
matrix B:

(Give the mathematical analysis of non-recursive matrix multiplication


algorithm 05M)

Analysis (Time Complexity):

Input Size: Matrix order n.

Basic Operation: There are two arithmetical operations in the innermost loop
here—multiplication and addition—that, in principle, can
compete for designation as the algorithm’s basic operation.

Actually, we do not have to choose between them, because


on each repetition of the innermost loop each of the two is
executed exactly once. So by counting one we automatically
count the other. Still, following a well- established tradition,

we consider multiplication as the basic operation

Count of Basic Operation,


C(n):

Dept., of CSE Page 20


Analysis & Design of Algorithms Module 1 BCS401

EXAMPLE 4 The following algorithm finds the number of binary digits in the binary
representation of a positive decimal integer.

Analysis (Time Complexity):

Input Size: Here, the input is just one number, and it is this number’s
magnitude that determines the input size.

In such situations, it is preferable to measure size by the


number b of bits in the n’s binary representation:

Basic Operation: First, notice that the most frequently executed operation
here is not inside the while loop but rather the comparison
n>1

Count of Basic Operation, A more significant feature of this example is the fact that the
C(n): loop variable takes on only a few values between its lower
and upper limits;

therefore, we have to use an alternative way of computing


the number of times the loop is executed.

Since the value of n is about halved on each repetition of the


loop,

Dept., of CSE Page 21


Analysis & Design of Algorithms Module 1 BCS401

Mathematical Analysis of Recursive Algorithms


(1. Explain the general plan for analyzing the efficiency of a recursive
algorithm. Suggest a recursive algorithm to find factorial of number. Derive
its efficiency 8M)
(2. Give the general plan for analyzing the efficiency of the recursive algorithm.
Develop recursive algorithm for computing factorial of a positive number.
Calculate the efficiency in terms of order of growth. 10M)
(3. Explain general plan of mathematical analysis of recursive algorithms with
example 8M)
(4. The factorial function n! has value 1 when n<=1 and value n*(n-1)! When
n>1. Write both a recursive and an iterative algorithm to compute n! 6M)

General Plan for Analyzing the Time Efficiency of Recursive Algorithms

1. Decide on a parameter (or parameters) indicating an input’s size.


2. Identify the algorithm’s basic operation.
3. Check whether the number of times the basic operation is executed can vary on different
inputs of the same size; if it can, the worst-case, average-case, and best-case efficiencies
must be investigated separately.
4. Set up a recurrence relation, with an appropriate initial condition, for the number of
times the basic operation is executed.
5. Solve the recurrence or, at least, ascertain the order of growth of its solution.

EXAMPLE 1 Compute the factorial function F(n) = n! for an arbitrary nonnegative integer
n. Since

n!= 1 . . . . . (n − 1) . n = (n − 1)! . n for n ≥ 1

and 0!= 1 by definition, we can compute F(n) = F(n − 1) . n with the following recursive
algorithm.

Dept., of CSE Page 22


Analysis & Design of Algorithms Module 1 BCS401

Analysis (Time Complexity)

we consider n itself as an indicator of this algorithm’s input size

The basic operation of the algorithm is multiplication, whose number of executions we


denote M(n).

Since the function F(n) is computed according to the formula

The number of multiplications M(n) needed to compute it must satisfy the equality

Thus, we succeeded in setting up the recurrence relation and initial condition for the
algorithm’s number of multiplications M(n):

Method of backward substitutions is used for solving recurrence equation.

Dept., of CSE Page 23


Analysis & Design of Algorithms Module 1 BCS401

EXAMPLE 2 As our next example, we consider another educational workhorse of recursive


algorithms:

(1. Write the tower of Hanoi algorithm and steps for analysis of recursive
algorithm. Show the analysis of the above algorithm 8M)
(2. Give the general Plan for analyzing Time efficiency of recursive
algorithms and also Analyze the tower of Hanoi recursive algorithm
10M)
(3. Write the recursive algorithm for tower of Hanoi. Prove that the time
complexity is exponential. 08M)

The Tower of Hanoi puzzle. In this puzzle, we have n disks of different sizes that can slide
onto any of three pegs. Initially, all the disks are on the first peg in order of size, the largest on
the bottom and the smallest on top.

The goal is to move all the disks to the third peg, using the second one as an auxiliary, if
necessary. We can move only one disk at a time, and it is forbidden to place a larger disk on
top of a smaller one.

Dept., of CSE Page 24


Analysis & Design of Algorithms Module 1 BCS401

Analysis (Time Complexity)

Dept., of CSE Page 25


Analysis & Design of Algorithms Module 1 BCS401

EXAMPLE 3 The number of binary digits in n’s binary representation

Dept., of CSE Page 26


Analysis & Design of Algorithms Module 1 BCS401

Brute Force Approaches


Brute force is a straightforward approach to solving a problem, usually directly based on the
problem statement and definitions of the concepts involved.

Selection Sort

consider the application of the brute-force approach to the problem of sorting: given a list of n
orderable items (e.g., numbers, characters from some alphabet, character strings), rearrange
them in nondecreasing order.

We start selection sort by scanning the entire given list to find its smallest element and
exchange it with the first element, putting the smallest element in its final position in the sorted
list.

Then we scan the list, starting with the second element, to find the smallest among the last n
− 1 elements and exchange it with the second element, putting the second smallest element in
its final position.

Generally, on the ith pass through the list, which we number from 0 to n − 2, the algorithm
searches for the smallest item among the last n − i elements and swaps it with Ai :

Dept., of CSE Page 27


Analysis & Design of Algorithms Module 1 BCS401

As an example, the action of the algorithm on the list 89, 45, 68, 90, 29, 34, 17 is illustrated
in Figure

Analysis (Time Complexity):

Input Size: The number of elements in the array, i.e., n.

Basic Operation: The key comparison A[j]> A[min]

Count of Basic Operation,


C(n):

Dept., of CSE Page 28


Analysis & Design of Algorithms Module 1 BCS401

Bubble Sort
(1.With the algorithm derive the worst case efficiency for Bubble sort 4M)
(2. With the algorithm derive the worst case efficiency for bubble sort 4M)

Another brute-force application to the sorting problem is to compare adjacent elements of the
list and exchange them if they are out of order. By doing it repeatedly, we end up ―bubbling
up‖ the largest element to the last position on the list.

The next pass bubbles up the second largest element, and so on, until after n − 1 passes the
list is sorted. Pass i (0 ≤ i ≤ n − 2) of bubble sort can be represented by the following diagram:

The action of the algorithm on the list 89, 45, 68, 90, 29, 34, 17 is illustrated as an example in
Figure

Dept., of CSE Page 29


Analysis & Design of Algorithms Module 1 BCS401

Analysis (Time Complexity):

Input Size: The number of elements in the array, i.e., n.

Basic Operation: The key comparison A[j+1]> A[j]

Count of Basic Operation,


C(n):

Dept., of CSE Page 30


Analysis & Design of Algorithms Module 1 BCS401

Sequential Search
(Design an Algorithm for performing sequential search and compute best case,
worst case and average case efficiency 10M)

The algorithm simply compares successive elements of a given list with a given search key
until either a match is encountered (successful search) or the list is exhausted without finding
a match (unsuccessful search).

A simple extra trick is often employed in implementing sequential search: if we append the
search key to the end of the list, the search for the key will have to be successful, and therefore
we can eliminate the end of list check altogether.

Analysis (Time Complexity):

Input Size: The number of elements in the array, i.e., n.

Basic Operation: The key comparison A[i] ≠ K

Count of Basic Operation, CWorst(n)=n;


C(n): CBest(n)=1;

CAvg(n)=n/2;

Dept., of CSE Page 31


Analysis & Design of Algorithms Module 1 BCS401

Brute-Force String Matching

Given a string of n characters called the text and a string of m characters (m ≤ n) called the
pattern, find a substring of the text that matches the pattern.

To put it more precisely, we want to find i—the index of the leftmost character of the first
matching substring in the text—such that

If matches other than the first one need to be found, a string-matching algorithm can simply
continue working until the entire text is exhausted.

Align the pattern against the first m characters of the text and start matching the corresponding
pairs of characters from left to right until either all the m pairs of the characters match (then
the algorithm can stop) or a mismatching pair is encountered.

In the latter case, shift the pattern one position to the right and resume the character
comparisons, starting again with the first character of the pattern and its counterpart in the text.

Note that the last position in the text that can still be a beginning of a matching substring is n
– m (provided the text positions are indexed from 0 to n − 1).

Beyond that position, there are not enough characters to match the entire pattern; hence, the
algorithm need not make any comparisons there.

Dept., of CSE Page 32


Analysis & Design of Algorithms Module 1 BCS401

Analysis (Time Complexity):

Thus, in the worst case, the algorithm makes m (n − m + 1) character comparisons, which
puts it in the O(nm) class.

Therefore, the average-case efficiency should be considerably better than the worst-case
efficiency.

Indeed, it is: for searching in random texts, it has been shown to be linear, i.e., Θ(n).

Dept., of CSE Page 33

You might also like