Professional Documents
Culture Documents
SL 1
SL 1
Books
Introduction to Algorithms
Coremen, Leiserson
ALGORITHM
A finite set of instructions which if followed accomplish a particular task. In addition every algorithm must satisfy following criteria:
1. Input: zero or more quantities externally supplied 2. Output: at least one quantity is produced 3. Definiteness: Each instruction must be clear and unambiguous. 4. Finiteness: In all cases algorithm must terminate after finite number of steps. 5. Effectiveness: each instruction must be sufficiently basic.
4
A1
For n = 106 Time taken by Super Computer = 2.(106)2/ 108 = 20,000 sec Time taken by P .C. = 50 . 106 lg 106 / 106 = 1,000 sec
Thus by using a fast algorithm , the personal computer gives results 20 times faster than the result given by super computer using a slow algorithm. Thus a good algorithm is like a sharp knife, it does exactly what it is supposed to do with a minimum amount of effort.
Pseudocode
Pseudocode is an English language like representation of the code required for an algorithm. It is partly English, partly structured code. The English part provides a relaxed syntax that is easy to read. The code part consists of an extended version of the basic algorithmic constructs-sequence, 8 selection and iteration.
The typical selection statement is the two way selection if (condition) action 1 else action 2. The part of the loop are identified by indentation. Loop iterates a block of code. It closely resembles the while loop. It is a pretest loop.
10
1 2
3 4 5 6
7 8
i= 0 Loop(all data are read) 1 i=i+1 2 read numbers into array[i] 3 sum = sum + number Average = sum / I Print (average) J=0 Loop (j < i) 1 j = j+ 1 2 dev = array[j] average 3 print (array [ j] . Dev) Return End deviation
11
Linear loop
1 2 l=1 Loop(I <= 1000)
1 2 application code I=I + 1
The body of the loop is repeated 1000 times. 1 2 I=1 Loop (I <= n) 1 Application code 2 I=I+2 For this the code time is proportional to n
Logarithm Loops
Multiply loops loops 1 I=1 2 Loop (I < n)
1 application code 2 I = l*2
F(n) = [log n]
F(n) = [log n]
Dependent Quadratic
I=1 loop ( I < = n) 1 j=1 2 loop( j < = l) 1 application code 2 j=j+1 3 I=I + 1 no of iterations in the body of the inner loop is 1 + 2 + 3 + 4 + + 10 = 55 I.e. n(n +1)/2 On an average = ( n+1/)2 thus total no of iterations = n (n+1)/2 1 2
Quadratic
1 l=1 2 Loop (I < = n)
1 j=1 2 Loop( j < = l) 1 application code 2 j = j+1 3 I = j+1 F(n) = n2
Algorithm Analysis
Complexity
Some questions to answer: How fast can we solve a problem? There may be many algorithms for a given problem. Which algorithm to use? What are the classical algorithm design techniques ? Are there problems inherently difficult to solve?
18
How do we express the complexity of algorithm? Resources : Time and Space Complexity lower bounds for problems. Complexity classes P, NP etc.
19
Machine model: Generic Random Access Machine (RAM) Executes operations sequentially Set of primitive operations:
Arithmetic. Logical, Comparisons, Function calls
Review of Complexity
Most of the primary sorting algorithms run on different space and time complexity. Time Complexity is defined to be the time the computer takes to run a program (or algorithm in our case). Space complexity is defined to be the amount of memory the computer needs to run a program. (Note:-What is Running Time and it is expressed as function of the size of its input.)
L1.22
Time Complexity
Real Time:
To analyze the real time complexity of a program we need to determine two numbers for each statement in it: amount of time a single statement will take.
No. of times it is executed. Product of these two, will be the total time taken by the statement.
First no. depends upon the machine and compiler used , hence the real time complexity is machine dependent.
To make analysis machine independent it is assumed that every instruction takes the same constant amount of time for execution. Hence the determination of time complexity of a program is the matter of summing the frequency counts of all the statements.
Frequency count
Kinds of analysis
Worst-case: (usually) T(n) = maximum time of algorithm on any input of size n. Average-case: (sometimes) T(n) = expected time of algorithm over all inputs of size n. Need assumption of statistical distribution of inputs. Best-case: (NEVER) Cheat with a slow algorithm that works fast on some input.
L1.26
INSERTION SORT
Insertion sort keeps making the left side of the array sorted until the whole array is sorted. Real life example:
An example of an insertion sort occurs in everyday life while playing cards. To sort the cards in your hand you extract a card, shift the remaining cards, and then insert the extracted card in the correct place.
L1.27
Insertion sort
INSERTION-SORT (A, n) A[1 . . n] for j 2 to n do key A[ j] i j1 while i > 0 and A[i] > key do A[i+1] A[i] i i1 A[i+1] = key i j
key sorted
L1.28
pseudocode
1
A:
L1.29
L1.30
L1.31
L1.32
L1.33
L1.34
8 8
L1.35
8 8
L1.36
8 8 4
L1.37
8 8 4
L1.38
8 8 4
3 3
8 6
L1.39
Selection sort
How does it work:
first find the smallest in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted. How does it sort the list in a non increasing order?
Selection sort is Inefficient for large lists. Incremental algorithms process the input elements one-byone and maintain the solution for the elements processed so far. L1.40
Analysis of Algorithms
Algorithm analysis: quantify the performance of the algorithm, i.e., the amount of time and space taken in terms of n. T(n) is the total number of accesses made from the beginning of selection_sort until the end. selection_sort itself simply calls swap and find_min_index as i goes from 1 to n-1
n 1
Or = (n - i) = n (n - 1) / 2 - O(n2)
L1.43
Binary search
sorted sequence : (search 9) 1 4 5 7 9 10 12 15 step 1 o step 2 o step 3 o best case: 1 step = O(1) worst case: (log2 n+1) steps = O(log n) average case: O(log n) steps
Big O Notation
O notation approximates the cost function of an algorithm
The approximation is usually good enough, especially when considering the efficiency of algorithm as n gets very large Allows us to estimate rate of function growth
Instead of computing the entire cost function we only need to count the number of times that an algorithm executes its barometer instruction(s)
The instruction that is executed the most number of times in an algorithm (the highest order term)
O Notation Examples
All these expressions are O(n):
n, 3n, 61n + 5, 22n 5,
given functions f(n) and g(n), we say that f(n) is O(g(n) ) if and only if there are positive constants c and n0 such that f(n) c g(n) for n n0
Example
For functions f(n) and g(n) (to the right) there are positive constants c and n0 such that: f(n) c g(n)
f(n) = 2n + 6
c g(n) ! 4n
for n n0
conclusion: 2n+6 is O(n).
n g(n) ! n
EXAMPLE Consider 1/3 n2 5n The dominating term is n2 Therefore it should be of O(n2) Given a positive constant c , a positive integer n0 to be found such that 1/3 n2 - 5 n e c n2
Use the Big-Oh notation to express the number of primitive operations executed as a function of the input size. Comparing the asymptotic running time -an algorithm that runs in O(n) time is better than one that runs in O(n2) time -similarly, O(log n) is better than O(n) -hierarchy of functions: log n << n << n2 << n3 << 2n
5-notation
For function g(n), 5(g(n)) is given by:
5(g(n)) = {f(n): +ve constants c1, c2, and n0 such that 0 e c1g(n) e f(n) e c2g(n), n u n0 } Intuitively: Set of all functions that have the same rate of growth as g(n). g(n) is an asymptotically tight bound for f(n).
O -notation
For function g(n), O(g(n)) is given by: O(g(n)) = {f(n): +ve constants c and n0 such that 0 e f(n) e cg(n), n u n0 }
Intuitively: Set of all functions whose rate of growth is the same as or lower than that of g(n).
; -notation
For function g(n), ;(g(n)) is given by: ;(g(n)) = {f(n): +ve constants c and n0 such that 0 e cg(n) e f(n), n u n0 }
Intuitively: Set of all functions whose rate of growth is the same as or higher than that of g(n).
g(n) is an asymptotic lower bound for f(n)
f(n) = 5(g(n)) f(n) = ;(g(n)). 5(g(n)) ;(g(n)).
Relations Between 5, O, ;
Practical Complexity
250
f(n) = n f(n) = log(n) f(n) = n log(n) f(n) = n^2 f(n) = n^3 f(n) = 2^n
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20