Docsity

Introduction to Algorithms: Design, Analysis, and Common Strategies, Lecture notes of Data Structures and Algorithms

Toggle sidebar
bg1
What is an Algorithm?
•The steps that can be used by a Computer for the solution
of a problem. It is different from words like Process,
Technique or Method
•An algorithm is a finite set of instructions that if followed,
accomplishes a particular task. In addition all algorithms
must satisfy following criteria (characteristics)
1. Input : Zero or more quantities are externally supplied
2. Output : At least one quantity is produced
3. Definiteness : Each instruction is clear &
unambiguous
4. Finiteness : For all input cases it terminates after finite
number of steps
5. Effectiveness : Every instruction must be very basic
so that it can be carried out, in principle, by a person
using only pencil and paper (Feasible Instructions)
Unit I : Introduction to Algorithms
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

Partial preview of the text

Download Introduction to Algorithms: Design, Analysis, and Common Strategies and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

What is an Algorithm?

  • (^) The steps that can be used by a Computer for the solution of a problem. It is different from words like Process, Technique or Method
  • (^) An algorithm is a finite set of instructions that if followed, accomplishes a particular task. In addition all algorithms must satisfy following criteria (characteristics) 1. Input : Zero or more quantities are externally supplied 2. Output : At least one quantity is produced 3. Definiteness : Each instruction is clear & unambiguous 4. Finiteness : For all input cases it terminates after finite number of steps 5. Effectiveness : Every instruction must be very basic so that it can be carried out, in principle, by a person using only pencil and paper (Feasible Instructions)

Four Research Areas of study in Algorithms :

1. Design of Algorithms : Algorithmic strategies ( D&C, Greedy, Dynamic Programming, Backtracking, Branch & Bound, Approximation Algorithms, Randomized algorithms, Parallel Algorithms etc). 2. Validation of Algorithms : Once an algorithm is devised, it is necessary to show that it computes the correct answer for all possible legal inputs. Proof techniques (Contradiction, Mathematical Induction etc.) 3. Analysis of Algorithms : Computing Time and Storage requirements for an algorithm 4. Testing of Programs : Software Testing, Test Cases Design and Debugging

Algorithm (or Program) Specification : Example

  1. Selection sort algorithm (Pseudo code) : for (i = 1; i < n ; i++) { examine a[ i ] to a[ n ] for the smallest element a[ j ]; interchange a[ i ] and a[ j ]; }
  2. // C Program to Sort the array a[1:n] into non-decreasing order. typedef int type; int j, k; type t; void SelectionSort(type a[], int n) { for (i = 1; i <n; i++) { j = i;

Performance Analysis of an Algorithm : There are many criteria upon which we can judge an algorithm. For example :

  1. Does it do what we want it to do? Results
  2. Does it work correctly according to original specifications of the task? Quality
  3. Is there documentation that describes how to use it and how it works? Documentation
  4. Are procedures created in such a way that they perform logical sub-functions? Modularity
  5. Is the code readable? Readability These criteria are all important when it comes to writing software for large systems. *** There are other criteria for judging algorithms that have a more direct relationship to performance. These have to do with

Performance Evaluation : Asymptotic analysis Priori Estimates : For any instruction in a program: Total Computation Time = Time required to execute the instruction * Number of times the instruction is executed in a program (i.e. frequency count ). Time required to execute the instruction depends upon:

  • Speed of processor (Clock frequency)
  • Instruction set of the machine language
  • Time required (processor cycles) to execute an instruction
  • Compiler used to translate high level instructions to m/c language

All above criteria differ from installation to installation. Therefore for priori estimates frequency count of each statement is most important factor while analyzing an algorithm or program. Frequency count : It is the number of times an instruction is executed in the execution of a program. Example

Performance Evaluation : Asymptotic analysis Priori Estimates : Example contd… Frequency Count of each statement: Statement # 1 : n + 1 Statement # 2 : n2 /2 + 3n/ Statement # 3 : n3/6 + n2 + 5n/ Statement # 3 : n3/6 + n2/2+ n/ Total Frequency Count = (n 3 +6n2 +11n +3)/3 = O(n 3 )

Performance Evaluation : Asymptotic analysis Asymptotic Notations : To enable us to make meaningful (but inexact) statements about the time and space complexities of an algorithm , asymptotic notations (O, o, Ω, ω, θ) are used. Big “Oh” : The function f(n) = O(g(n)), to be read as “ f of n is Big Oh of g of n”, if and only if there exist positive constants c and n0 such that, f(n) ≤ c*g(n) for all n≥n0, for example, i. f(n) = 3n + 2 ≤ 4n for all n ≥ 2. Here c = 4, g(n) = n and n0 = 2. ii. f(n) = (n3 + 6n2 + 11n +3) ≤ 2n3 for all n ≥ 8. Here c = 2, g(n) = n3 and n0 = 8. Thus f(n) = O(g(n)) states that g(n) is an upper bound on the value of f(n) for all n≥n0.

Performance Evaluation : Asymptotic analysis Theta ( θ ): The function f(n) = θ(g(n)), to be read as “ f of n is theta of g of n, if and only if there exist positive constants c1 , c2 and n0 such that, c1g(n) ≤ f(n) ≤ c2*g(n) for all n≥n0, for example, i. f(n) = 3n + 2 then 3n ≤ 3n + 2 ≤ 4n for all n≥ Here c1 = 3, c2 = 4, g(n) = n and n0 = 2. ii. f(n) = (n3 + 6n2 + 11n +3) then n3 ≤ (n3 + 6n2 + 11n +3) ≤ 2n3 for all n ≥ 8. Here c1 = 1, c2 = 2, g(n) = n3 and n0 = 8. Thus f(n) = θ(g(n)) states that g(n) is both upper & lower bound on the value of f(n) for all n≥n0.

Performance Evaluation : Asymptotic analysis Little “oh” : The function f(n) = o(g(n)), to be read as “ f of n is little oh of g of n, if and only if Lim f(n) / g(n) = 0 n → ∞ i. f(n) = 3n + 2 = o(n2) or o(nlogn) ii. f(n) = (n3 + 6n2 + 11n +3) = o(n4) or o(n3logn) Little omega : The function f(n) = ω(g(n)), to be read as “ f of n is little omega of g of n, if and only if Lim g(n) / f(n) = 0 n → ∞ i. f(n) = 3n + 2 = ω(1) ii. f(n) = (n3 + 6n2 + 11n +3) = ω(n2)

Amortized Analysis : Worst case analysis of an algorithm is sometimes pessimistic. For example let us consider the following procedure for adding 1 to counter. Let array C[1:m] represent binary number = Σ 2m-j C[j] for example [0,1,1,0,1,1] = 27 1≤ j ≤ m Algorithm count (C[1…m]) // assumption is m≥1 & C[j] = {0, 1} for 1≤j≤m { j = m + 1; repeat { j = j – 1; C[ j ] = 1 - C[ j ]; } until (C[ j ] = 1) or ( j = 1); }

Amortized Analysis : In worst case a barometric instruction j = j – 1 will be executed ‘m’ times and if we call the procedure ‘n’ times ( i.e. add 1 to count ‘n’ times) it will be executed nlog n times. Thus the worst case time complexity for ‘n’ calls = O(nlogn) but we can prove that it is O(n). 00 number of calls 01 1 10 2 11 1 100 3 Total 7 For n = 4, total calls = 7 = 24 - 1 For n = 8, total calls = 15 = 2*8 - For n = n, total calls = 2n – 1 Time Complexity = O(n)

Performance Evaluation : Performance Measurement : (Posteriori Testing) : Performance measurement is concerned with obtaining the space and time requirements for a particular algorithm. Purpose of posteriori testing may be to work out some empirical formula by executing the program for different sizes of n, which can be used to estimate the runtime for a given problem size, or the purpose may be to compare the two algorithms for their runtime. For example, for sequential search the runtime may be given by the empirical formula t = 0.002 + 0.0003067n, where t is the time in milliseconds and n is size of array. Time and Space tradeoffs in algorithms : In general, while solving the problem, for any algorithm, computation time required will be more when space required is less and vice-a-versa. Example

Best, Worst and Average case analysis of an algorithm: Best Case analysis : Best case is that input to the algorithm which takes minimum time for execution of it. Best case analysis of an algorithm is the asymptotic analysis of an algorithm for best case input. Examples :

  • (^) Binary search algorithm : Best case is to search the element positioned at the middle of the sorted array and Asymptotic time (Time complexity) required is O(1)
  • (^) Insertion sort algorithm : Best case is sorted input in the required order and Asymptotic time (Time complexity) required is O(n)