Download presentation
Presentation is loading. Please wait.
Published byGavin Palmer Modified over 9 years ago
1
ANALYSIS OF ALGORITHMS Data Structures
2
Algorithms zDefinition A step by step procedure for performing some task in a finite amount of time zAnalysis Methodology for analyzing a “good” algorithm (i.e. Archimedes) zLook at Running Time
3
How are algorithms described ? PSEUDO-CODE zrepresentation of an algorithm zcombines Õnatural language Õfamiliar programming language structures zfacilitates the high-level analysis of an algorithm
4
Pseudo-Code Conventions zExpressions zAlgorithm Structures zControl Structures
5
Expressions zStandard math symbols + - * / ( ) zRelational operators zBoolean operators and or not zAssignment operator, = zArray indexing A[i]
6
Algorithm Structure zAlgorithm heading Algorithm name(param1, param2,...): Input : input elements Output : output elements zStatements call object.method(arguments) return statementreturn value control structures
7
Control Structures zdecision structures zwhile loops zrepeat loops zfor loop n if... then... [else...] n while... do n repeat... until... n for... do
8
General Rules zcommunicate high level ideas and not implementation details (programming language specifics) zclear and informative
9
Example - Problem zProblem : Write pseudo-code for an algorithm that will determine the maximum element from a list (array)
10
Example - Algorithm Algorithm arrayMax(A,n): Input: An array A storing n integers. Output: The maximum element in A. currentMax A[0] for i 1 to n - 1 do if currentMax < A[i] then currentMax A[i] return currentMax
11
Analysis of Algorithms zRunning time Õdepends on input size n Õnumber of primitive operations performed zPrimitive operation Õunit of operation that can be identified in the pseudo-code
12
Primitive Operations-Types zAssigning a value to a variable zCalling a method/procedure zPerforming an arithmetic operation zComparing two numbers zReturning from a method/procedure
13
Primitive Operations - Some General Rules zfor Loops zNested for Loops zConditional Statements
14
for Loops The running time of a for loop is at most the running time of the statements inside the for loop (including tests) times the number of iterations. Example for i 0 to n - 1 do A[i] 0
15
Nested for Loops Analyze these inside out. The total running time of a statement inside a group of nested for loops is the running time of the statement multiplied by the product of the sizes of all the for loops. Example for i 0 to n - 1 do for j 0 to n - 1 do k++
16
Conditional Statements if (cond) then S1 else S2 The running time of an if-else statement is never more than the running time of the test plus the larger of the running times of S1 and S2.
17
Primitive Operations - Example zCount the number of primitive operations in this program : i = 0 a = 0 for i = 1 to n do print i a=a+i return i
18
Primitive Operations - Example zEstimate : 3n + 3 zLinear Time - (i.e. 6n + 2, 3n+5, n+1,etc.)
19
Primitive Operations - Example 2 zCount the number of primitive operations in this program : i = 0 a = 0 for i = 1 to n do print i for a = 1 to n do print a return i
20
Primitive Operations - Example2 zEstimate : n 2 + n + 3 zPolynomial (Quadratic) Time
21
Example 3 - Class Algorithm sum(int n): Input: Integer n Output: Sum of the cubes of 1 to n partial_sum 0 for i 1 to n do partial_sum partial_sum + i*i*i return (partial_sum)
22
Primitive Operations - Considerations zArbitrariness of measurement zWorst-case versus average-case (difficult to be exact : range) zImplicit operations Õloop control actions Õarray access
23
Analysis of Algorithms ztake-out the effect of constant factors (i.e. 2n vs 3n : same level) zthis can be attributed to differences in hardware and software environments zcompare algorithms across types (linear vs polynomial vs exponential) zlook at growth rates
24
Asymptotic Notation zGoal: ÕTo simplify analysis by getting rid of irrelevant information zThe “Big-Oh” Notation ÕGiven functions f(n) and g(n), f(n) is O (g(n)) if f(n) = n 0 where c and n 0 are constants
25
Big-Oh Notation zRule : Drop lower order terms and constant factors 5n + 2 is O (n) 4n 3 log n + 6n 3 + 1 is O (n 3 logn)
26
Big-Oh Notation - Relatives zBig Omega (reverse) zBig Theta (both ways, same level) zGeneral Rule : Use the most descriptive notation to describe the algorithm
27
Practical Significance of Big-Oh Notation zExample for i 0 to n-1 do A[i] 0 zRunning time is 2n, if we count assignments & array accesses 3n, if we include implicit loop assignments 4n, if we include implicit loop comparisons zRegardless, running time is O (n)
28
Algorithms - Common Classifications zTypical functions that classify algorithms constant O (1) logarithmicO (log n) linear O (n) quadraticO (n 2 ) exponentialO (a n ), n > 1
29
Linear Search vs Binary Search zLinear SearchO (n) zBinary Search O (log n) zExample : to search for a number from 1024 entries using the binary search algorithm will only take 10 steps
30
O(log n) Algorithm
31
O(log n) Algorithm Binary Search Algorithm bin_search(int a[], int x, int n) Input: Array a, search target x, array size n Output: Position of x in the array (if found)
32
` { low 0; high n - 1 while (low <= high) do mid (low + high)/2 if(a[mid] < x) then low mid + 1 else if (a[mid] > x) then high mid - 1 else return (mid) return (NOT_FOUND) }
33
Prefix Average Problem zGiven an array X storing n numbers, we want to compute an array A such that A[1] is the average of elements X[0] … X[I] for I = 0 … n-1. zalgorithm A = O (n 2 ) zalgorithm B = O (n)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.