290 likes | 429 Views
Today’s Agenda. Efficiency and Complexity Resources and Measurements Complexity Time Complexity Space Complexity. Efficiency. Efficiency of algorithm needs algorithm analysis. Analyzing algorithms means to predict the resources that the algorithm requires. Complexity of Algorithm.
E N D
Today’s Agenda • Efficiency and Complexity • Resources and Measurements • Complexity • Time Complexity • Space Complexity
Efficiency • Efficiency of algorithm needs algorithm analysis. • Analyzing algorithms means to predict the resources that the algorithm requires.
Complexity of Algorithm • Resources • CPU Time • Storage • Complexity • Time Complexity • Space Complexity
Time Complexity • Running Time of Algorithm is affected by • Hardware • CPU, Clock Rate, memory, disk etc • Software • Operating system, programming language, compilers etc • Running time of an algorithm is always proportional to the input size.
Time Complexity • Efficiency of algorithms must be compared on their running time in the same h/w and s/w environment. • So implementation of the algorithm is needed to study time complexity. • But it is always not possible.
Analytical Framework • Based on algorithm (high level description of problem) • It takes into account all possible inputs • It also allows us to evaluate relative efficiency of two algorithms
Computational Model • Primitive Operations • Assigning a value to a variable • Calling a function • Performing an arithmetic operation • Comparing two numbers • Indexing into an array • Returning value from function
Computational Model • Each Primitive operation is an instruction • One instruction takes one unit execution time. • This approach assumes that all primitive operations take fairly similar execution time. • Primitive operations does not depend on input size. • Run time of an algo is estimated based on number of primitive operations.
Complexity Example [1] • Example 1 (Y and Z are input) X = 3 * Y + Z; X = 2 + X; // 2 units of time and 1 unit of storage
Complexity Example [2] • Example 2 (a and N are input) j = 0; while (j < N) do a[j] = a[j] * a[j]; b[j] = a[j] + j; j = j + 1; endwhile; // 3N + 1 units of time and N+1 units of storage
Order of complexity [1] • Example 1 (Y and Z are input) X = 3 * Y + Z; X = 2 + X; // Constant Unit of time and Constant Unit of storage
Order of complexity [2] • Example 2 (a and N are input) j = 0; while (j < N) do a[j] = a[j] * a[j]; b[j] = a[j] + j; j = j + 1; endwhile; // time units prop. to N and storage prop. to N
Order of complexity [3] • Example 2 (a and N are input) j = 0; while (j < N) do k = 0; while (k < N) do a[k] = a[j] + a[k]; k = k + 1; endwhile; b[j] = a[j] + j; j = j + 1; endwhile; // time prop. to N2 and storage prop. to N
What if the algorithm is more complicated? • How this analytical approach will work for large input size? • Each statement may have small number of primitive operations. • So there are limitations of analytical approach.
Asymptotic Notation • Allows to characterize main factors affecting an algo’s running time without going in much details of the algorithm. • Run time is proportional to the input size of the algorithm.
Order Notation • Purpose • Capture proportionality • Machine independent measurement • Asymptotic growth (I.e. large values of input size N)
Asymptotic Notation • f(n) – function which characterizes the running time of algorithm. • Notation is f(n) <= cg(n) • Where c > 0, n >= n0 where n0 >= 1 • f(n) is order of g(n) • f(n) is O(g(n))
Motivation for Order Notation Speed factor of 10
Motivation for Order Notation Speed factor of 104
Order Notation • Examples • (17*N + 5) is O(N) • (5*N3 + 10*N2 + 3) is O(N3) • (C1*Nk + C2*Nk-1 + … + Ck*N + C) is O(Nk) • (2N + 4*N3 + 16) is O(2N) • (5*N*log(N) + 3*N) is O(N*log(N)) • 1789 is O(1)
Linear Search - Complexity function search(X, A, N) j = 0; while (j < N) if (A[j] == X) return j; j++; endwhile; return “Not_found”;
Binary Search Algorithm low = 1; high = N; while (low <= high) do mid = (low + high) /2; if (A[mid] = = x) return x; else if (A[mid] < x) low = mid +1; else high = mid – 1; endwhile; return Not_Found;
Binary Search Let key=7; BinarySearch(A, 0,16, 7) • Find the index of the middle element: A[mid]=A[(0+16)/2]=A[8]=45 • A[mid]==7?
7 < 45, search left sublist:A[0..7] • Begin BinarySearch(A, 0, 7, 7) • Find the index of the middle element: A[mid]=A[(0+7)/2]=A[3]=8 • A[mid]==7?
7 < A[mid]=8, perform BinarySearch(A, 0, 2, 7) • mid=1, so we compare 7 with A[1]= 6.
7 >A[mid]=6, look at sublist A[2..2] • The value of element 2 matches the search key, so our search is successful and we return the index 2.
Algorithm for computing xy • Algorithm 1 • Multiplying x to itself (y-1) number of times • So number of multiplications will be (y-1) • Time complexity will be dependent on value of y. • Time complexity will be O(N) where N = y. • So for large values of y this algorithm is not very efficient. • Any alternate solution ?
Algorithm II for xy • Decompose the problem into sub-problems • We observe that if y is power of 2 then data decomposition gives good result. • If y is not a power of 2 then divide the problem into two sub-problems where one is power of 2 and other is not power of 2.