Docsity

Algorithm Analysis and Design Fundamentals, Study notes of Design and Analysis of Algorithms

Toggle sidebar
bg1
UNIT – 1
INTRODUCTION
What is an algorithm?
Definition: An algorithm is defined as finite sequence of unambiguous instructions followed
to accomplish a given task. It is also defined as unambiguous, step by step procedure
(instructions) to solve a given problem in finite number of steps by accepting a set of inputs
and producing the desired output. After producing the result, the algorithm should terminate.
The notion of an algorithm is pictorially represented as shown below:
Input
ProblemAlgorithmProgramComputerOutput
Observe the following activities to see how an algorithm is used to produce the desired
result:
The solution to a given problem is expressed in the form of an algorithm.
The algorithm is converted into a program.
The program when it is executed, accept the input and produces the desired output
The properties of an algorithm?” An algorithm must satisfy the
• Input: Each algorithm should have zero or more inputs. The range of inputs for which
algorithm works should be satisfied
• Output: The algorithm should produce correct results. At least one output has to be
produced.
• Definiteness: Each instruction should be clear and unambiguous.
• Effectiveness: The instructions should be simple and should transform the given input
to the desired output.
• Finiteness: The algorithm must terminate after a finite sequence of instructions.
Introduction: What is an Algorithm? Fundamentals of Algorithmic problem solving,
Fundamentals of the Analysis of Algorithm Efficiency, Analysis Framework, Measuring the
input size, Units for measuring Running time, Orders of Growth, Worst-case, Best case and
Average-case efficiencies.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b

Partial preview of the text

Download Algorithm Analysis and Design Fundamentals and more Study notes Design and Analysis of Algorithms in PDF only on Docsity!

UNIT – 1

INTRODUCTION

What is an algorithm? Definition: An algorithm is defined as finite sequence of unambiguous instructions followed to accomplish a given task. It is also defined as unambiguous, step by step procedure (instructions) to solve a given problem in finite number of steps by accepting a set of inputs and producing the desired output. After producing the result, the algorithm should terminate. The notion of an algorithm is pictorially represented as shown below: Input ProblemAlgorithmProgramComputerOutput Observe the following activities to see how an algorithm is used to produce the desired result:  The solution to a given problem is expressed in the form of an algorithm.  The algorithm is converted into a program.  The program when it is executed, accept the input and produces the desired output The properties of an algorithm?” An algorithm must satisfy the

  • Input: Each algorithm should have zero or more inputs. The range of inputs for which algorithm works should be satisfied
  • Output: The algorithm should produce correct results. At least one output has to be produced.
  • Definiteness: Each instruction should be clear and unambiguous.
  • Effectiveness : The instructions should be simple and should transform the given input to the desired output.
  • Finiteness: The algorithm must terminate after a finite sequence of instructions. Introduction: What is an Algorithm? Fundamentals of Algorithmic problem solving, Fundamentals of the Analysis of Algorithm Efficiency, Analysis Framework, Measuring the input size, Units for measuring Running time, Orders of Growth, Worst-case, Best case and Average-case efficiencies.

Note: By looking at the algorithm, the programmer can write the program in C or C++ or any of the programming language. Before writing any program, the solution has to be expressed in the form of algorithms.

Algorithm design and analysis process

A sequence of steps involved in designing and analyzing an algorithm is shown inthe

figure below. :

 Understand the problem

 Decide on Computational Device Exact Vs Approximate Algorithms  Algorithm Design Techniques  Design an algorithms  Prove Correctness  Analyze the Algorithm  Code the Algorithm Understanding the Problem: Begin by clearly understanding the problem you are trying to solve. Identify the input, output, and constraints. This step is crucial for defining the scope of the algorithm.

Computing GCD

The notion of an algorithm can be explained by computing the GCD of two numbers. Now, let us see “What is GCD of two numbers?” Definition : The GCD (short form for Greatest Common Divisor) of two numbers m and n denoted by GCD(m, n) is defined as the largest integer that divides both m and n such that the remainder is zero. GCD of two numbers is defined only for positive integers but, not defined for negative integers and floating point numbers. For example, GCD(10, 30) can be obtained as shown below: Step 1: The numbers 1, 2, 5, 10 divide 10 Step 2: The numbers 1, 2, 5, 6, 10 and 30 divide 30 Step 3: Observe from step I and step 2 that the numbers 1, 2, 5 and 10 are common divisors of both 10 and 30 and 10 is the greatest number which is common and hence it is called Greatest Common Divisor So, GCD(10,30) = 10. Now, let us see “What are the different ways of computing GCD of two numbers?” The GCD of two numbers can be computed using various methods as shown below: Different ways of computing GCD

  • Euclid’s algorithm (Using modulus)
  • Repetitive subtraction (Euclid’s algorithm)
  • Consecutive inter checking algorithm
  • Middle school procedure using prime factors Step 2: [Eliminate the multiples of p between 2 to n] for p = 21 sqrt(n) if(a * [p]! = 0) // Is a[p] is prime? If so proceed i p* p // p is the next prime number obtained while (i<= n) // Obtain position of multiples of p a[i]  0 // Multiples of p may exist ii + p // Eliminate a[i] which is multiple of p End while // Obtain the position of next multiple of p End if End for Step 3: [Obtain the prime numbers by copying the non-zero elements]

j 0 for i 2 to n do if(a * [i]! = 0) b[j]  a[i] ; j j + 1 End if End for Step 4: [Output the prime numbers between 2 to n] for i0 to j– 1 Write b[i] End for Step 5: [Finished]Exit Units for measuring Running time The running time of algorithms is often measured using different units depending on the context and the granularity of analysis. Here are common units for measuring running time: Seconds (s):  The most straightforward unit, representing the actual time in seconds that an algorithm takes to run on a specific machine.  Suitable for measuring real-world performance but can be influenced by external factors like the machine's load. Milliseconds (ms):  A smaller unit than seconds, measuring time in thousandths of a second.  Useful for more precise measurement when algorithms have relatively fast execution times. Microseconds (μs):  An even smaller unit than milliseconds, measuring time in millionths of a second.  Appropriate for very fast algorithms or when extremely precise timing is required. Nanoseconds (ns):  A unit smaller than microseconds, measuring time in billionths of a second.

  1. Stack space  Program space: The space required for storing the machine program generated by the compiler or assembler is called program space.  Data space : The space required to store the constants, variables etc., is called data  Stack space: The space required to store the return address along with parameters that are passed to the function, local variables etc., is called stack space. Note: The new technological innovations have improved the computer’s speed and memory size by many orders of magnitude. Now a days, space requirement for an algorithm is not a concern and hence, we are not concentrating on space efficiency. Let us concentrate only on time efficiency.. “What is time efficiency?” Definition: The time efficiency of an algorithm is measured purely on how fast a given algorithm is executed. Since the efficiency of an algorithm is measured using time, the word time complexity is often associated with an algorithm. The time efficiency of the algorithm depends on various factors that are shown below: Components that affect time efficiency  Speed of the computer  Choice of the programming language  Compiler used  Choice of the algorithm  Number (Size) of inputs/Outputs  Since we do not have any control over speed of the computer, programming language and compiler, let us concentrate only on next two factors such as:n o Choice of an algorithm o Number (size) of inputs "What is basic operation?" Definition: The operation that contributes most towards the running time of the algorithm is called basic operation. A statement that executes maximum number of times in a function is also called basic operation. The number of times basic operation is executed depends on size of the input. The basic operation is the most time consuming operation in the algorithm. For example:  a statement present in the innermost loop in the algorithm  addition operation while adding two matrices, since it is present in innermost loop  multiplication operation in matrix multiplication since it is present in innermost loop

Now, let us see "How to compute the running time of an algorithm using basic operation or How time efficiency is analyzed?" The time efficiency is analyzed by determining the number of times the basic operation is executed. The running time T(n) is given by: T(n) ~ b*C(n)  T is the running time of the algorithm  n is the size of the input  b execution time for basic operation.  C represent number of times the basic operation is executed

Order of growth

"What is order of growth? For what values of n we find the order of growth?" Definition: We expect the algorithms to work faster for all values of n. Some algorithms execute faster for smaller values of n. But, as the value of n increases, they tend to be very slow. So, the behaviour of some algorithm changes with increase in value of n. This change in behaviour of the algorithm and algorithm's efficiency can be analyzed byconsidering the highest order of n. The order of growth is normally determined for largervalues of n for the following reasons:  The behavior of algorithm changes as the value of n increases  In real time applications we normally encounter large values of n For example, the order of growth with respect to two running times is shown below:  Suppose T(n) approx c* C(n). Observe that T(n) varies linearly with increase or decrease in the value of n. In this case, the order of growth is linear.  Suppose T(n) approx c* C(n ^ 2). In this context, the order of growth is quadratic. Note: If the order of growth of one algorithm is linear and the order of growth of second algorithm to solve the same problem is quadratic, then it clearly indicates that running time of first algorithm is less and it is more efficient. So, while analyzing the time efficiency of an algorithm, the order of growth of n is important. Let us discuss orders of growth in the next section.

2 N: Indicates that running time of an algorithm is exponential. The tower of Hanoiproblem and algorithms that generate subsets of a given set will have this runningtime N! : Indicates that running time of an algorithm is factorial. The algongenerate all permutations of set will have this running time. Note: All the above functions can be ordered according to their order of growth (fromlowest to highest) as shown below: 1 < log(n) < n < n * log(n) < n^2 < n^3 < 2n^ < n! Note: Even though running times of 2N^ and N! are different, normally both classes are considered as exponential. The algorithms with this running time can be solved practically for smaller values of n. Note: For a very large value of n, the exponential function 2" and factorial function n!generate a very high value such that even the fastest computer can take years to executean algorithm. For example, a computer which executes 10^12 instructions per second takes 4*10^10 years to execute 2^100 operations. The factorial function n! is much more than the value specified for 2 n.

Worst-case, Best-case and average case efficiencies Now the question is "Will the algorithm efficiency depends on the size of algorithm's input alone?" For some of the problems, the time complexity will not depend on the number of inputs alone. For example, while searching for a specific item in an array of n elements using linear search, we have following three situations:  An item we are searching for may be present in the very first location itself. In this case only one item is compared and this is the best case.  The item may be present somewhere in the middle which definitely takes some time. Running time is more when compared to the previous case for the same value of n. Since we do not know where the item is we have to consider the average number of cases and hence this situation is an average case.  The item we are searching for may not be present in the array requiring n number of comparisons and running time is more than the previous two cases. This may be considered as the worst case. So, knowing n alone itself is not enough to estimate the run time of an algorithm or a function. In such cases, we may have to find the worst-case efficiency, best case efficiency and average case efficiency. Now, let us see "What is worst case efficiency?" "What is worst case efficiency?" Definition: The efficiency of an algorithm for the input of size n for which the algorithm takes longest time to execute among all possible inputs is called worst case efficiency For example, if we use linear search and the item to be searched is no present in the array, then it is an example of worst case efficiency. In the worst case, the algorithm runs for the longest duration among all possible inputs for a given size. Here, maximum number of steps are executed for that input. "What is best case efficiency?" Definition: The efficiency of an algorithm for the input of size n for which the algorithm takes least time during execution among all possible inputs of that size is called best case efficiency. In the best case, the algorithm runs fastest for the input specified. For example, in linear search, if the item to be searched is present in the beginning, then it is an example of the best case efficiency. Note: In the average case efficiency, the average number of basic operations executed will be considered. This is required only for the randomized input.

Average-case efficiency:  This is the scenario where the algorithm takes an average amount of time to complete, considering all possible inputs.  The average-case time complexity is often more challenging to analyze and may involve statistical analysis.  In our example, if the target element is equally likely to be anywhere in the array, the average-case time complexity is O(n/2), which simplifies to O(n).

Average-case

scenarioarr = [1, 2, 3, 4, 5] target = 3 result = search_element(arr, target)

On average, the algorithm might need to check half of the array element

Design And Analysis of Algorithm NEP - 2020 Arvind G |Dept of Computer Application | MIT FGC

UNIT 2

Asymptotic Notations and Basic Efficiency classes, Informal Introduction, O-notation, Ω- notation, θ-notation, mathematical analysis of non-recursive algorithms, mathematical analysis of recursive algorithms. Asymptotic notations The efficiency of the algorithm is normally expressed using asymptotic notations. The order of growth can be expressed using two methods:  Order of growth using asymptotic notations  Order of growth using limits. Before proceeding further, let us see “What do you mean by asymptotic behavior of a function?” Definition: The value of the function may increase or decrease as the value of n increases. Based on the order of growth of n, the behavior of the function varies. Asymptotic notations are the notations using which two algorithms can be compared with respect to efficiency based on the order of growth of an algorithm’s basic operation. Now, let us see “What are the different types of asymptotic notations?” The different types of asymptotic notations are shown below: Asymptotic notations  O(Big Oh)  Ω(Big Omega)  ΘBigTheta Now, let us see the informal definition and formal definition of above asymptotic notations Informal Definitions of Asymptotic notations In this section, let us “Give informal definitions of asymptotic notations” Informal Definitions of Big-Oh(0) Definition: Assuming n indicates the size of input and g(n) is a function, informally O(g(n)) is defined as set of functions with a small or same order of growth as g(n)) as n goes to infinity.

Design And Analysis of Algorithm NEP - 2020 Arvind G |Dept of Computer Application | MIT FGC Note: Big-O is the formal method of expressing the upper bound of an algorithm’s running time. It is a measure of the longest amount of time it could possibly take for theAlgorithm to complete. Note: The function g(n) is normally expressed using higher order terms of f(n) This is achieved using the following steps:  Take the lower order term of f(n) replace the constant with next higher order variable. Repeat this step, till we get the higher order term and call it as c .g(n)  Once the constraint “ f(n) <= c.g(n) for n >= n 0 is obtained we say f(n) ∈ O(g(n)) Example : Let f(n) = 100n + 5 Express f(n) using big-oh Solution: It is given that f(n) = 100n + 5 Replacing 5 with n (so that next higher order term is obtained), we get 100n + n and call it c.g(n) i.e..c. g(n) = 100n + n for n = 5 = 101nforn =5; Now, the following constrain is satisfied: f(n) <=c * g(n) for n ≥ n 0 I .e.,100n+5<=101 nfor n >= 5 It is clear from the above relations that c = 101 g(n) = n and n 0 =5. So , by definition f(n) ∈ O(g(n)) * I .e.., f(n) ∈ O(n) Ω(Big-Omega) Definition: Let f(n) be the time complexity of an algorithm. The function f(n) is said tobe Ω(g(n)) [read as big-omega of g(n)] which is denoted by f(n) = Ωg((n)) or f(n) or = Ω(a(n)) If and only if there exists a positive constant c and non-negative integer no satisfying the constraint

Design And Analysis of Algorithm NEP - 2020 Arvind G |Dept of Computer Application | MIT FGC f(n)>=cg(n) for all n≥ n 0. So, if we draw the graph f(n) and cg(n) verses n, the graph of f(n) lies above the graph of g(n) for sufficiently large value of n as shown below: This notation gives the lower bound on a function f(n) within a constant factor. The lower bound on f(n) indicates that function f(n) will consume at least the specified time cg(n)i.e., the algorithm has a running time that is always greater than cg(n). In general, the lower bound implies that below this time the algorithm cannot perform better. Note: f(n) ≥ cg(n) indicates that g(n) is a lower bound and the running time of an algorithm is always greater than g(n). So, big-omega notation is used for finding best case time efficiency. Example: Let f(n) = 100n+ 5. Express f(n) using big-omega Solution: The constrain to be satisfied is f(n) >=c g(n) for n >= n 0 i.e., 100n+5 ≥ 100 n for n>= It is clear from the above relations that c = 100. g(n) = n and n 0 = 0. So, by definition f(n) = Ω(g(n))i.e.,f(n) ∈ Ω(n)

Design And Analysis of Algorithm NEP - 2020 Arvind G |Dept of Computer Application | MIT FGC This notation is used to denote both lower bound and upper bound on a function f(n) within a constant factor. The upper bound on f(n) indicates that function f(n) will not consume more than the specified time cog(n).The lower bound on f(n) indicates that function f(n) in the best case will consume at least the specified time c₁*g(n).

Design And Analysis of Algorithm NEP - 2020 Arvind G |Dept of Computer Application | MIT FGC

Mathematical analysis of non - recursive algorithms

The general plan of energy non recursive algorithm is shown below:  Based on the size of input data in the number of parameters to be considered.  Identify the basic operation in the algorithm  Check whether the number of times the basic operation is executed depends only on the size of the input, if the basic operation to be executed depends on the some other conditions, then it is necessary to obtain the worst case, best case and average case separately.  Open the total number of times a basic operation is executed  Simplify using standard formula and obtain the order of growth. Now let us consider some of the algorithms and see how to analyze these algorithms.