O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Algorithms & Complexity Calculation

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Carregando em…3
×

Confira estes a seguir

1 de 33 Anúncio
Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (20)

Semelhante a Algorithms & Complexity Calculation (20)

Anúncio

Mais de Akhil Kaushik (17)

Mais recentes (20)

Anúncio

Algorithms & Complexity Calculation

  1. 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani Algorithms
  2. 2. What is Algorithm? • Algorithm is a step by step procedure. • It defines a set of instructions to be executed in certain order to get the desired output. • Algorithms are generally created independent of underlying languages.
  3. 3. Algorithm Development • Good, logical programming is developed through good pre-code planning and organization. • This is assisted by the use of pseudocode and program flowcharts. • Flowcharts are written with program flow from the top of a page to the bottom. • Each command is placed in a box of the appropriate shape, and arrows are used to direct program flow.
  4. 4. Pseudocode • It is a method of describing computer algorithms using a combination of natural and programming language. • The usual Fortran symbols are used for arithmetic operations (+, -, *, / ). • Symbolic names are used to indicate the quantities being processed. • Certain Fortran keywords can be used, such as PRINT, WRITE, READ, etc. • Indentation should be used to indicate branches and loops of instruction.
  5. 5. Example:- Pseudocode to Add Two Nos. step 1 − START step 2 − Declare three integers a, b & c step 3 − define values of a & b step 4 − Add values of a & b step 5 − Store output of step 4 to c step 6 − Print value of c step 7 − STOP
  6. 6. Characteristics of an Algorithm • Unambiguous − Algorithm should be clear and unambiguous. • Input − An algorithm should have 0 or more well defined inputs. • Output − An algorithm should have 1 or more well defined outputs, and should match the desired output. • Finiteness − Algorithms must terminate after a finite number of steps. • Feasibility − Should be feasible with the available resources. • Independent − An algorithm should have step-by-step directions which should be independent of any programming code.
  7. 7. Algo Design Techniques 1. Top down 2. Bottom up 3. Incremental 4. Divide and conquer 5. Greedy. 6. Dynamic approach
  8. 8. Top-Down Design • Divide main program into a main module and its related module. • Divide each module in sub module according to s/w engineering and programming style. • Division of module continues till elementary process that cant be divided. • In this each module has single entry and single exit point. • In c language it is done by functions.
  9. 9. Bottom-Up Approach • Its principle is to start with specific module and built into more complex structure, ending at top. • It is widely used for testing because each of lowest level function is written and tested first. • This strategy often resembles a "seed" model, by which the beginnings are small but eventually grow in complexity and completeness.
  10. 10. Incremental Approach • This approach start with single module then adding module to finally build the whole algorithm. • It is piecing together of systems to give rise to more complex systems.
  11. 11. Divide and Conquer • This approach divide the original problem into sub problems. • Solve each problem individually. • Finally combine solution of top sub problems into a solution of whole problem.
  12. 12. Greedy Approach • It seeks to optimize a function by making choice which are best locally but not globally. • Result is good solution but necessarily not best. Dynamic Approach • It is a technique for efficiently computing recurrences by storing partial results. • It is a method of solving problems exhibiting properties of overlapping sub problems.
  13. 13. Algorithm Analysis • A priori analysis − This is theoretical analysis of an algorithm. – Efficiency of algorithm is measured by assuming that all other factors e.g. processor speed, are constant and have no effect on implementation. • A posterior analysis − The selected algorithm is implemented using programming language. – This is then executed on target computer machine. In this analysis, actual statistics like running time and space required, are collected.
  14. 14. Algorithm Complexity • Time Factor − It is measured by counting the no. of key operations such as comparisons in sorting algorithm • Space Factor − The space is measured by counting the maximum memory space required by the algo. • The complexity of an algorithm f(n) gives the running time and / or storage space required by the algorithm in terms of n as the size of input data.
  15. 15. Space Complexity • It is amount of memory space required by the algorithm in its life cycle. • Space required by an algo is equal to the sum of the following two components:- – A fixed part is a space required to store certain data and variables, that are independent of the size of the problem. – A variable part is a space required by variables, whose size depends on the size of the problem. For example dynamic memory allocation, recursion stack space etc.
  16. 16. Time Complexity • It is amount of time required by the algorithm to run to completion. • Time requirements can be defined as a numerical function T(n), which is measured as the number of steps, provided each step consumes constant time. • Ex: Addition of two n-bit integers takes n steps. Thus, the total computational time is T(n) = c*n, where c is the time taken for addition of two bits. – Here, we observe that T(n) grows linearly as input size increases.
  17. 17. Asymptotic Analysis • Usually, time required by an algorithm falls under three types − • Best Case − Minimum time required for program execution(Ω). • Average Case − Average time required for program execution(θ). • Worst Case − Maximum time required for program execution(O).
  18. 18. Omega Notation, Ω • The Ω(n) is the formal way to express the lower bound of an algorithm's running time. • It measures best amount of time an algorithm can possibly take to complete. • Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
  19. 19. Theta Notation, θ • The θ(n) is the formal way to express both the lower bound and upper bound of an algorithm's running time. It is represented as following − • θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }
  20. 20. Big Oh Notation, Ο • The Ο(n) is the formal way to express the upper bound of an algorithm's running time. • It measures the worst case or longest amount of time an algorithm can possibly take to complete. Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
  21. 21. Common Asymptotic Notations • Constant−Ο(1) • Logarithmic−Ο(log n) or O(log(log(n)) • Linear−Ο(n) or O(log(n2)) • Quadratic − Ο(n log n) or Ο(n2) • Cubic−Ο(n3) • Polynomial−O(nk), where k>0 • Exponential−O(cn), where c>1. Ex: O(2n)
  22. 22. Common Asymptotic Notations
  23. 23. Common Asymptotic Notations • Constant − Ο(1) -> When algo takes same amount of time to compute regardless of input size. • Ex: 1. If a no. is even or odd • 2. If an item on an array is null • 3. Print 1st element from list • 4. Find a value on map
  24. 24. Common Asymptotic Notations • Linear − Ο(n) -> As input grows, algo takes proportionally longer. • Ex: 1. Get max/ min value in array • 2. Find/ search an element in a collection • 3. Print all values in a list
  25. 25. Common Asymptotic Notations • Quadratic − Ο(n2) -> If the input size of 2, it will do 4 operations. If input is 8, it will do 64 operations. • Ex: 1. Sorting – Bubble, Insertion, Selection • 2. Check if array has duplicated values • 3. Find all possible ordered pairs in array
  26. 26. Common Asymptotic Notations • Polynomial−O(nc) -> Here, c>1. Here, it takes huge running times as input grow. • Ex: Triple nested loop has O(n3) • Quadratic O(n2) and Cubic O(n3) come under this category.
  27. 27. Common Asymptotic Notations • Logarithmic − Ο(log n) -> Here, it uses divide-&- conquer strategy to divide the problems into half. – Ex: 1. Find a word in dictionary – 2. Find a person on phone book • Linear Arithmetic - O(n log(n)) -> It is slower than linear, but better than a quadratic algo. – Ex: Sorting – Merge sort, quick sort, etc.
  28. 28. Common Asymptotic Notations • Exponential – O(2n) -> Calculations performed by algo double every time as input grows. – Ex: 1. Power set – find all subsets on set. – 2. Fibonacci series or Travelling salesman problem. • Factorial – O(n!) -> Most calculations are worst case scenarios.
  29. 29. Examples • If f(n) = 7(2n2+5), then O(f(n)) is n2 • If f(n) = 5 + 4n, then O(n) • If f(n) = n3 + 4n2 + 20n + 1, then O(n3) • If f(n) = n & g(n) = n2, then f(n) < g(n), Hence f(n) = O(g(n))
  30. 30. Examples • If f(n) = n, g(n) = n2 & h(n) = n3, then f(n) = O(g(n)), g(n) = O(h(n)) & thus f(n) = O(h(n)) • If f(n) = n2 log n & g(n) = n(log n)10, then: Hence, f(n) = Ω(g(n)) f(n) n2 log n n.n log n n g(n) n(log n)10 n.log n (log n)9 (log n)9
  31. 31. Examples • If f(n) = n2 & g(n) = 2n, then: Hence, f(n) = O(g(n)) for n>=4 f(n) 0 for n=0 1 for n=1 4 for n=2 9 for n=3 16 for n=4 25 for n=5 36 for n=6 g(n) 1 for n=0 2 for n=1 4 for n=2 8 for n=3 16 for n=4 32 for n=5 64 for n=6
  32. 32. Akhil Kaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 THANK YOU !!!

×