Docsity

Algorithm Analysis and Time Complexity in Computer Science, Slides of Data Structures and Algorithms

Toggle sidebar
bg1
CS 242
Data Structures
Algorithm Analysis
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Algorithm Analysis and Time Complexity in Computer Science and more Slides Data Structures and Algorithms in PDF only on Docsity!

CS 242

Data Structures

Algorithm Analysis

Why need algorithm analysis?

} Writing a working program is not good enough

} The program may be inefficient!

} If the program is running on a large data set, then the

running time becomes an issue

Time complexity

} Time complexity is the execution time it takes for your algorithm to solve a problem. } On other words, time complexity is the number of steps taken by the algorithm, measured with respect to n (input data to be processed) } The content of the input affects the running time } The input size (number of items in the input) is the main consideration } E.g. sorting problem: the number of items to be sorted } E.g. multiply two matrices together: the total number of elements in the two matrices } Machine model assumed Instructions are executed one after another, with no concurrent operations } Not parallel computers

Algorithm Efficiency

} Algorithm efficiency

} A function of Fundamental Operations to be processed } f(n)=efficiency } Basic computations performed by an algorithm } Evaluating an expression } Assigning a value to a variable } Indexing into an array } Calling a method } Returning from a method

Worst- / average- / best-case

} Worst-case running time of an algorithm } The longest running time for any input of size n } An upper bound on the running time for any input guarantee that the algorithm will never take longer } Example: Sort a set of numbers in increasing order; and the data is in decreasing order } Best-case running time } sort a set of numbers in increasing order; and the data is already in increasing order } Average-case running time } May be difficult to define what “average” means

Big-O Notation

} We are not interested in knowing the exact number of operations the algorithm performs. mainly interested in knowing how the number of operations grows with increased input size. } Why? } Given large enough input, the algorithm with faster growth will execute more operations and takes long time } The simplification of efficiency is known as big-O analysis. } We don’t need to determine the complete measure of efficiency, only the factor that determines the magnitude. This factor is the big-O , and expressed as O(n) – that is, on the order of n.

Algorithm Efficiency Categories

Efficiency Big-O Iterations Estimated Time Logarithmic O(log 2 n) 14 Microseconds Linear O(n) 10,000 0.1 seconds Linear logarithmic O(n(log 2 n)) 140,000 2 seconds Quadratic O(n^2 ) 10,000 15 - 20 minutes Polynomial O(nk) 10,000 Hours Exponential O(cn) 2 10,000^ Intractable Factorial O(n!) 10,000! Intractable *assume instruction speed of 1 microsecond and 10 instructions in loop

Example 1

} Linear loop

} What if we change the incremental condition? Ex: i=i+ iteration Value of i 1 1 2 2 3 3 … … n n i= loop (i<=n) application code i=i+ end loop f(n)=n O(n) n

Example 2

} Logarithmic loop

} Multiply iteration Value of i 1 1 2 2 3 4 4 8 5 16 6 32 7 64 8 128 9 256 10 512 Exit 1024 i= loop (i<n) application code i=i x 2 end loop 2 iteration^ < 1000 f(log 2 n) O(log 2 n) i n

Example 4

} Nested loop

} Iterations = inner loop iterations x outer loop iterations

} Linear logarithmic

i= loop (i<=n) j= loop (j<=n) application code j= j x 2 end loop i=i + 1 end loop f(n [log 2 n]) O(n [log 2 n]) log 2 n n

Example 5

} Nested loop

} Quadratic i= loop (i<=10) j= loop (j<=10) application code j= j + end loop i=i + 1 end loop n n O(n^2 ) f(n^2 )

Consecutive program fragments

} The total running time is the maximum of the running

time of the individual fragments.

} The first loop runs in O(n) time, the second - O(n

2

) time,

the maximum is O(n

2

sum = 0; for( i = 0; i < n; i++) sum = sum + i; sum = 0; for( i = 0; i < n; i++) for( j = 0; j < 2n; j++) sum++; n 2n^2 à O(n) à O(n^2 ) à O(n^2 )

If statement

} The running time is the maximum of the running times of

S 1 and S 2.

if cond { S1; } else { S2; }