Upcoming SlideShare
×

# Introducción al Análisis y diseño de algoritmos

2,015 views
1,914 views

Published on

Este es un primer acercamiento a lo que haremos en la asignatura

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
2,015
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
53
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Introducción al Análisis y diseño de algoritmos

1. 1. Tomado del libro de Cormen A A nalysis of lgorithms
2. 2. OUTLINE <ul><li>Introduction </li></ul><ul><ul><ul><li>What is an algorithm? </li></ul></ul></ul><ul><ul><ul><li>Semantic analysis </li></ul></ul></ul><ul><ul><ul><li>Correctness </li></ul></ul></ul><ul><ul><ul><li>Analysis of complexity </li></ul></ul></ul>
3. 3. What is an algorithm? <ul><ul><li>Well-defined computational procedure that takes some value, or a set of values, as input and produces some value, or a set of values, as output . An algorithm is thus a sequence of computational of steps that transforms the input into the output. </li></ul></ul><ul><ul><li>We can also view an algorithms as a tool for solving a well-specified computational problem [Cormen, 91]. </li></ul></ul><ul><ul><li>A set of instructions , clearly specified, that are performed in order to solve a problem [Baase, 91]. </li></ul></ul><ul><ul><li>Unambiguous specification of a sequence of steps performed to solve a problem [Aho, 95]. </li></ul></ul>
4. 4. Algorithm A sequence of computational of steps for solving a well-specified computational problem The statement of the problem specifies in general terms the desired input/output relationship The algorithm describes a specific computational procedure for achieving that input/ouput relationship
5. 5. Algorithm steps <ul><li>I : input / O:output </li></ul>I O
6. 6. INPUT: A sequence of numbers <a 1 ,a 2 ,a 3 ,...,a n > OUTPUT: A permutation <a’ 1 ,a’ 2 ,a’ 3 ,...a’ n > such that a’ 1  a i2  a i3  ...  a in Sorting problem
7. 7. A possible input sequence is a instance of the sorting problem <31,41,59,26,41,58> A sorting algorithms must return: <26,31,41,41,58,59> Instance of the sorting problem
8. 8. Analysis of algorithms Complexity Time complexity Space complexity Analysis Semantic Analysis Correctness
9. 9. Semantic Analysis What does an algorithm do? (Discovering its function?) <ul><li>Is the study of the function associated to an algorithm: </li></ul><ul><ul><li>I input space : the space of values of the input variables defined in the algorithm . </li></ul></ul><ul><ul><li>O output space : the space of the values of the output variables defined in the algorithm . </li></ul></ul><ul><ul><li>X state space : the space of the values of the variables defined in the algorithm . </li></ul></ul><ul><ul><li>f : I  O  {  } </li></ul></ul><ul><ul><li>function associated to the algorithm </li></ul></ul>
10. 10. <ul><li>function mistery( n ) </li></ul><ul><li>i  1 </li></ul><ul><li>j  0 </li></ul><ul><li>for k  1 to n do </li></ul><ul><ul><li>j  i + j </li></ul></ul><ul><ul><li>i  j - i </li></ul></ul><ul><ul><li>end-for * </li></ul></ul><ul><ul><li>return j </li></ul></ul>Value in the variables at the point * (n)  X 0  X 1  X 2  X 3  X 4  X 5  X 6  X 7  .…  X n  (j) k  0, 1, 2, 3, 4, 5, 6, 7,.…, j  0, 1, 1, 2, 3, 5, 8, 13,.…, i  1, 0, 1, 1, 2, 3, 5, 8,.…, Example
11. 11. mistery( 0) = 0 mistery( 1) = 1 mistery( n ) = mistery( n - 1 ) + mistery( n - 2), for n > 2 . “ mistery( n ) computess the n-th Fibonacci’s number” mistery( n ) =(  n -  n ) /  5 with  =(1 +  5)/2 [Golden ratio]  =(1 -  5)/2
12. 12. following an algorithm. <ul><li>( I )  X 0 (O 0 )  X 1 (O 1 )  ...  X m (O m )  ... </li></ul><ul><ul><li>If it stops in m steps the output is {O 0, O 1 ... O m } </li></ul></ul><ul><ul><li>If it does not stop {O 0, O 1 ... O m …  } </li></ul></ul><ul><li>f : I  O  {  } with I  I , O  O , X i  X </li></ul>I O f ( X )
13. 13. Correctness Is an algorithm correct for a problem? (Does it do a given function?) Given a computational problem f : I  O  {  } and a algorithm A . The algorithm A is correct for the problem f if the function that A calculates is f .
14. 14. Problem: Finding the minumun element in an array of n reals Input : T[1..n] : array of real Output : x  min{T[i] | 1  i  n } min : N x R N  R (n, T[1..n] )  min{T[i] | 1  i  n} “ In order to prove that a program or algorithm is correct, we need to show that the program gives the correct answer for every possible input.”
15. 15. <ul><li>function min( n , T[1..n] ) : real </li></ul><ul><li>x  T[1] </li></ul><ul><li>for i  2 to n do </li></ul><ul><ul><li>if T[i] < x then </li></ul></ul><ul><ul><li> x  T[i] </li></ul></ul><ul><ul><li>end_for </li></ul></ul><ul><ul><li>return x </li></ul></ul><ul><li>end </li></ul>
16. 16. <ul><li>function min( n , T[1..n] ) : real </li></ul><ul><li>if n =1 then </li></ul><ul><li>return T[1] </li></ul><ul><li>else </li></ul><ul><li>return MIN(min(n-1, T[1..n-1] ),T[n]) </li></ul><ul><li>end </li></ul><ul><li>function MIN(a,b ) : real </li></ul><ul><li>if a < b then </li></ul><ul><li>return a </li></ul><ul><li>else </li></ul><ul><li>return b </li></ul><ul><li>end </li></ul>
17. 17. <ul><li>function min( n , T[1..n] ) : real </li></ul><ul><li>if n =1 then </li></ul><ul><li>return T[1] </li></ul><ul><li>else if n=2 then </li></ul><ul><li>if T[1] < T[2] then </li></ul><ul><li>return T[1] </li></ul><ul><li>else </li></ul><ul><li>return T[2] </li></ul><ul><li>else </li></ul><ul><li>return min(2,[min(n-1, T[1..n-1] ),T[n]]) </li></ul><ul><li>end </li></ul>
18. 18. Analysis of complexity <ul><li>How much resources are needed to run an algorithm as a function of the size of the problem it solves? </li></ul><ul><li>Is the study of the amount of resources: </li></ul><ul><ul><li>time , [ time complexity] t(n) , </li></ul></ul><ul><ul><li>memor y, [ space complexity ] s(n) , </li></ul></ul><ul><ul><li>… [ other resources], </li></ul></ul><ul><li>that an algorithm will require to process an input of size n . </li></ul>
19. 19. Complexity <ul><li>Time complexity t(n) </li></ul><ul><li>computational steps </li></ul><ul><li>Space complexity s(n) </li></ul><ul><li>storage - memory </li></ul>Analysis
20. 20. Size of a problem n <ul><li>A measure of the amount of data that an </li></ul><ul><li>algorithm takes as input </li></ul><ul><li>Examples: </li></ul><ul><li>Sorting </li></ul><ul><ul><li>n = number of elements in the input sequence </li></ul></ul><ul><li>Inverting a square matrix A </li></ul><ul><ul><li>n = number of elements in the matrix </li></ul></ul><ul><li>Finding the root of a function f(x) </li></ul><ul><ul><li>n = number of precise digits in the root </li></ul></ul>
21. 21. <ul><li>Multiplying two integers </li></ul><ul><ul><li>n = number of bits needed to represent the input </li></ul></ul><ul><li>Finding the shortest path in graph, in this case the size of the input is described by two numbers rather than one. </li></ul><ul><ul><li>n = number of vertex </li></ul></ul><ul><ul><li>m = number of edges </li></ul></ul>
22. 22. Best case t b (n) Minimum execution time for any input of size n Worst case t w (n) Maximum execution time over all the inputs of size n Average case t a (n) Average execution time over all the inputs of size n Time Complexity t(n) Number of computational steps
23. 23. <ul><li>Worst-case analysis is appropriate for an algorithm whose response time is critical </li></ul><ul><li>In a situation where an algorithm is to be used many times on many different instances, it may be more important to know the average execution time on instances of time n </li></ul>Sorting average time taken on the n! different ways of initially ordering n elements (assuming they are all different)
24. 24. An operation whose execution time can be bounded above by a constant depending only on the particular implementation used ( machine, programming language etc ). In general we use RAM elementary instructions as the model. Elementary Operation
25. 25. <ul><li>Data types </li></ul><ul><ul><li>Integer </li></ul></ul><ul><ul><ul><li>Up to values n uses c log n bits. </li></ul></ul></ul><ul><ul><li>Floating point </li></ul></ul><ul><li>Bounded rage of values. </li></ul><ul><li>Elementary (primitive) instructions </li></ul><ul><ul><li>Arithmetic </li></ul></ul><ul><ul><ul><li>Add +, substrac -, multiply *, divide /, remainder %, floor   , ceiling   . </li></ul></ul></ul><ul><ul><li>Data movement </li></ul></ul><ul><li>load, store, copy. </li></ul>RAM Model Random Access Model
26. 26. <ul><ul><li>Control </li></ul></ul><ul><ul><ul><li>Conditional and unconditional branch, subroutine call, return. </li></ul></ul></ul><ul><ul><li>Special exponentiation </li></ul></ul><ul><ul><li>2 k for k small  k-shift left. </li></ul></ul><ul><ul><li>Each instructions takes a constant amount of time </li></ul></ul><ul><ul><li>Memory-hierarchy effects </li></ul></ul><ul><ul><li>main – cache – virtual </li></ul></ul><ul><ul><li>[demand paging] </li></ul></ul><ul><ul><li>are considered only in special cases </li></ul></ul>
27. 27. Example <ul><li>function mistery( n ) </li></ul><ul><li>i  1 </li></ul><ul><li>j  0 </li></ul><ul><li>for k  1 to n do </li></ul><ul><ul><li>j  i + j </li></ul></ul><ul><ul><li>i  j - i </li></ul></ul><ul><ul><li>end-for </li></ul></ul><ul><ul><li>return j </li></ul></ul>If the operations are elementary the algorithm takes a time equal to a+bn , for appropriate constants a and b, i.e., time in O (n).
28. 28. We must attribute to the additions a cost proportional to the length of the operands <ul><li>For n=47 the addition “j  i + j” causes arithmetic overflow on a 32-bit machine </li></ul><ul><li>45.496 bits are needed to hold the result corresponding to n=65.536 </li></ul>time in O (n 2 ) !!!But the operations are not elementary !!!
29. 29. “ Two different implementations of the same algorithm will not differ in efficiency by more than some multiplicative constant” Principle of Invariance
30. 30. El orden de complejidad de un algoritmo Es una expresion matematica que indica como los recursos necesitados para correr un algoritmo se incrementan como se incrementa el tamaño del problema (que el resuelve). Orden de complejidad de un algoritmo
31. 31. Complejidad Lineal La complejidad de un algoritmo se dice que es lineal si: Con a y b son constantes t(n)=a * n + b
32. 32. <ul><li>function min( T[1..n] ) : real </li></ul><ul><li>x  T[1] /* b */ </li></ul><ul><li>for i  2 to n do /* n times */ </li></ul><ul><li>if T[i] < x then /* a */ </li></ul><ul><ul><li> x  T[i] </li></ul></ul><ul><ul><li>end_for </li></ul></ul><ul><ul><li>return x </li></ul></ul>Example
33. 33. By the invariance principle we if we have two different implementations (computer + language + compiler +code optimizations) with worst case time complexity t w1 (n) and t w2 (n) then t w1 (n) = c t w2 (n) Worst case time complexity t w (n) = a*n + b