1 / 29

Today’s Agenda

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.

luther
Download Presentation

Today’s Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s Agenda • Efficiency and Complexity • Resources and Measurements • Complexity • Time Complexity • Space Complexity

  2. Efficiency • Efficiency of algorithm needs algorithm analysis. • Analyzing algorithms means to predict the resources that the algorithm requires.

  3. Complexity of Algorithm • Resources • CPU Time • Storage • Complexity • Time Complexity • Space Complexity

  4. 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.

  5. 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.

  6. 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

  7. 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

  8. 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.

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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.

  15. 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.

  16. Order Notation

  17. Order Notation • Purpose • Capture proportionality • Machine independent measurement • Asymptotic growth (I.e. large values of input size N)

  18. 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))

  19. Motivation for Order Notation Speed factor of 10

  20. Motivation for Order Notation Speed factor of 104

  21. 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)

  22. Linear Search - Complexity function search(X, A, N) j = 0; while (j < N) if (A[j] == X) return j; j++; endwhile; return “Not_found”;

  23. 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;

  24. 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?

  25. 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?

  26. 7 < A[mid]=8, perform BinarySearch(A, 0, 2, 7) • mid=1, so we compare 7 with A[1]= 6.

  27. 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.

  28. 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 ?

  29. 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.

More Related