SlideShare a Scribd company logo
Universidad de  los Andes A A nalysis of lgorithms
OUTLINE Introduction What is an algorithm? Semantic analysis Correctness Analysis of complexity
What is an algorithm? 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.  We can also view an algorithms as a tool for  solving a well-specified computational problem [Cormen, 91]. A  set of instructions , clearly specified, that are performed in order to  solve a problem  [Baase, 91]. Unambiguous specification of a  sequence of steps  performed to  solve a problem  [Aho, 95].
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
Algorithm steps I : input  / O:output I O
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
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
Analysis of algorithms Complexity Time complexity Space complexity Analysis Semantic Analysis Correctness
Semantic Analysis   What does an algorithm do?  (Discovering its function?) Is the study of the function associated to an algorithm: I  input space :  the space of values of the input variables defined in the algorithm . O  output space :  the space of  the values of the output variables defined in the algorithm .   X  state space :  the space of the values of the variables defined in the algorithm . f :   I      O    {  }   function associated  to the algorithm
function mistery( n ) i    1 j    0 for  k   1 to n do j    i + j i    j - i end-for  * return j 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
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
following an algorithm. (  I  )    X 0  (O 0 )    X 1  (O 1 )     ...     X m  (O m )     ...   If  it stops in m steps the output is {O 0,  O 1   ...   O m  } If  it does not stop {O 0,  O 1   ...   O m   …   } f :   I      O    {  }   with  I      I  ,  O     O  ,  X i      X I O f ( X )
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 .
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.”
function min( n , T[1..n] ) : real x    T[1]  for  i   2 to n do if T[i] < x then   x    T[i] end_for return x end
function min( n , T[1..n] ) : real if n =1 then  return T[1] else  return MIN(min(n-1, T[1..n-1] ),T[n])   end function MIN(a,b ) : real if a < b then  return a else  return b end
function min( n , T[1..n] ) : real if n =1 then  return T[1] else if n=2 then  if T[1] < T[2]  then  return T[1] else  return T[2] else  return min(2,[min(n-1, T[1..n-1] ),T[n]])   end
Analysis of complexity How much resources are needed to run an algorithm as a function of the size of the problem it solves?   Is the study of the amount of resources: time , [ time complexity]  t(n)  , memor y, [ space complexity ]  s(n)  , …   [ other resources], that an algorithm will require to process an input of size  n .
Complexity Time complexity  t(n) computational steps   Space complexity  s(n) storage - memory   Analysis
Size of a problem n  A measure of the amount of data that an  algorithm takes as input  Examples: Sorting  n  = number of elements in the input sequence Inverting a square matrix A n  = number of elements in the matrix Finding the root of a function f(x) n  = number of precise digits in the root
Multiplying two integers n  = number of bits needed to represent the input Finding the shortest path in graph, in this case the size of the input is described by two numbers rather than one. n  = number of vertex m  = number of edges
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
Worst-case  analysis is appropriate for an algorithm whose response time is critical 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 Sorting average time taken on the n! different ways of  initially ordering n elements (assuming they are all different)
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
Data types  Integer  Up to values  n   uses  c log n  bits. Floating point  Bounded rage of values. Elementary (primitive) instructions  Arithmetic Add +, substrac -, multiply *, divide /, remainder %, floor      , ceiling      . Data movement  load, store, copy. RAM Model Random Access Model
Control Conditional and unconditional branch, subroutine call, return. Special exponentiation  2 k  for k small      k-shift left. Each instructions takes a constant amount of time   Memory-hierarchy effects  main – cache – virtual  [demand paging]  are considered only in special cases
Example function mistery( n ) i    1 j    0 for  k   1 to n do j    i + j i    j - i end-for  return j 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).
We must attribute to the additions a cost proportional to the length of the operands  For n=47 the addition “j    i + j” causes arithmetic overflow on a 32-bit machine 45.496 bits are needed to hold the result corresponding to  n=65.536 time in  O (n 2 ) !!!But the operations are not elementary  !!!
“ Two different implementations of the same algorithm will not differ in efficiency by more than  some multiplicative constant” Principle of Invariance
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
Complejidad Lineal La complejidad de un algoritmo se dice que es lineal si: Con  a  y  b  son constantes t(n)=a * n + b
function min( T[1..n] ) : real x    T[1]  /*  b */ for  i   2 to n do  /* n times  */ if T[i] < x then  /*  a  */   x    T[i] end_for return x Example
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

More Related Content

What's hot (18)

PPTX
Asymptotic Notation
Protap Mondal
 
PPTX
Mathematical Analysis of Recursive Algorithm.
mohanrathod18
 
PDF
Cs2251 daa
Srinivasan Lakshmanan
 
PDF
Design & Analysis Of Algorithm
Computer Hardware & Trouble shooting
 
DOC
Time and space complexity
Ankit Katiyar
 
PPT
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
PDF
Lecture 4 asymptotic notations
jayavignesh86
 
PPT
Analysis Of Algorithms I
Sri Prasanna
 
PPT
how to calclute time complexity of algortihm
Sajid Marwat
 
PPTX
Lecture 5: Asymptotic analysis of algorithms
Vivek Bhargav
 
PPT
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
PPTX
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
PPT
CS8461 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
PDF
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
PPTX
Greedy Algorithms
Amrinder Arora
 
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
RTF
algorithm unit 1
Monika Choudhery
 
PPT
Analysis of Algorithm
أحلام انصارى
 
Asymptotic Notation
Protap Mondal
 
Mathematical Analysis of Recursive Algorithm.
mohanrathod18
 
Design & Analysis Of Algorithm
Computer Hardware & Trouble shooting
 
Time and space complexity
Ankit Katiyar
 
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Lecture 4 asymptotic notations
jayavignesh86
 
Analysis Of Algorithms I
Sri Prasanna
 
how to calclute time complexity of algortihm
Sajid Marwat
 
Lecture 5: Asymptotic analysis of algorithms
Vivek Bhargav
 
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
CS8461 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
Greedy Algorithms
Amrinder Arora
 
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
algorithm unit 1
Monika Choudhery
 
Analysis of Algorithm
أحلام انصارى
 

Similar to Alg1 (20)

PDF
01 CS316_Introduction.pdf5959695559655565
yahiaf3k
 
PPTX
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
madhu614742
 
PPT
Data_Structure_and_Algorithms_Lecture_1.ppt
ISHANAMRITSRIVASTAVA
 
PPTX
DESIGN AND ALGORITHM.pptx BCA BANGALORECITY UNIVERSITY
AneetaGrace1
 
PPTX
ANALYSIS AND DESIGN OF ALGORITHMS -M1-PPT
AIET
 
PPTX
Analysis Framework, Asymptotic Notations
DrSMeenakshiSundaram1
 
PPTX
Algorithm and C code related to data structure
Self-Employed
 
PPT
AA Lecture 01 of my lecture os ghhhggh.ppt
maryamzahra3366
 
PDF
ADA complete notes
Vinay Kumar C
 
PPSX
Ds03 part i algorithms by jyoti lakhani
jyoti_lakhani
 
PPT
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
PDF
Lecture 2 role of algorithms in computing
jayavignesh86
 
PPT
data unit notes from department of computer science
sdcmcatmk
 
PPT
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
DrKBManwade
 
PPTX
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
AntareepMajumder
 
PDF
BCS401 ADA First IA Test Question Bank.pdf
VENKATESHBHAT25
 
PPTX
Design & Analysis of Algorithm course .pptx
JeevaMCSEKIOT
 
PPTX
L1_DatabAlgorithm Basics with Design & Analysis.pptx
dpdiyakhan
 
PPTX
Introduction to data structures and complexity.pptx
PJS KUMAR
 
PPTX
Design and Analysis of Algorithm for II year Computer science and Engineering...
Kalpana Devi M
 
01 CS316_Introduction.pdf5959695559655565
yahiaf3k
 
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
madhu614742
 
Data_Structure_and_Algorithms_Lecture_1.ppt
ISHANAMRITSRIVASTAVA
 
DESIGN AND ALGORITHM.pptx BCA BANGALORECITY UNIVERSITY
AneetaGrace1
 
ANALYSIS AND DESIGN OF ALGORITHMS -M1-PPT
AIET
 
Analysis Framework, Asymptotic Notations
DrSMeenakshiSundaram1
 
Algorithm and C code related to data structure
Self-Employed
 
AA Lecture 01 of my lecture os ghhhggh.ppt
maryamzahra3366
 
ADA complete notes
Vinay Kumar C
 
Ds03 part i algorithms by jyoti lakhani
jyoti_lakhani
 
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
Lecture 2 role of algorithms in computing
jayavignesh86
 
data unit notes from department of computer science
sdcmcatmk
 
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
DrKBManwade
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
AntareepMajumder
 
BCS401 ADA First IA Test Question Bank.pdf
VENKATESHBHAT25
 
Design & Analysis of Algorithm course .pptx
JeevaMCSEKIOT
 
L1_DatabAlgorithm Basics with Design & Analysis.pptx
dpdiyakhan
 
Introduction to data structures and complexity.pptx
PJS KUMAR
 
Design and Analysis of Algorithm for II year Computer science and Engineering...
Kalpana Devi M
 
Ad

More from luzenith_g (20)

PPTX
Formacion y crecimiento
luzenith_g
 
PPTX
Formacion y crecimiento
luzenith_g
 
PPTX
Carácterísticas técnicas de las wikis
luzenith_g
 
PPTX
Web 2 0
luzenith_g
 
DOCX
Ejercicios Ada
luzenith_g
 
DOCX
Ejercicios Ada
luzenith_g
 
DOCX
Proyecto Unal2009 2
luzenith_g
 
DOC
Taller3 Programacion Ii
luzenith_g
 
PPT
Algoritmos Voraces (Greedy)
luzenith_g
 
PPT
Algoritmos Greedy
luzenith_g
 
PPT
Resumen
luzenith_g
 
PPT
Clase3 Notacion
luzenith_g
 
PPT
Analisis Clase2
luzenith_g
 
PPT
Introducción al Análisis y diseño de algoritmos
luzenith_g
 
DOC
Como construir un DSS
luzenith_g
 
PPT
Mergesort
luzenith_g
 
PPT
State Space Search(2)
luzenith_g
 
PPT
DSS:Conceptos, metodologias y Tecnologias
luzenith_g
 
PPT
Soporte a las Decisiones Computarizado
luzenith_g
 
PPT
Decision Support Systems
luzenith_g
 
Formacion y crecimiento
luzenith_g
 
Formacion y crecimiento
luzenith_g
 
Carácterísticas técnicas de las wikis
luzenith_g
 
Web 2 0
luzenith_g
 
Ejercicios Ada
luzenith_g
 
Ejercicios Ada
luzenith_g
 
Proyecto Unal2009 2
luzenith_g
 
Taller3 Programacion Ii
luzenith_g
 
Algoritmos Voraces (Greedy)
luzenith_g
 
Algoritmos Greedy
luzenith_g
 
Resumen
luzenith_g
 
Clase3 Notacion
luzenith_g
 
Analisis Clase2
luzenith_g
 
Introducción al Análisis y diseño de algoritmos
luzenith_g
 
Como construir un DSS
luzenith_g
 
Mergesort
luzenith_g
 
State Space Search(2)
luzenith_g
 
DSS:Conceptos, metodologias y Tecnologias
luzenith_g
 
Soporte a las Decisiones Computarizado
luzenith_g
 
Decision Support Systems
luzenith_g
 
Ad

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 

Alg1

  • 1. Universidad de los Andes A A nalysis of lgorithms
  • 2. OUTLINE Introduction What is an algorithm? Semantic analysis Correctness Analysis of complexity
  • 3. What is an algorithm? 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. We can also view an algorithms as a tool for solving a well-specified computational problem [Cormen, 91]. A set of instructions , clearly specified, that are performed in order to solve a problem [Baase, 91]. Unambiguous specification of a sequence of steps performed to solve a problem [Aho, 95].
  • 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. Algorithm steps I : input / O:output I O
  • 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. 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. Analysis of algorithms Complexity Time complexity Space complexity Analysis Semantic Analysis Correctness
  • 9. Semantic Analysis What does an algorithm do? (Discovering its function?) Is the study of the function associated to an algorithm: I input space : the space of values of the input variables defined in the algorithm . O output space : the space of the values of the output variables defined in the algorithm . X state space : the space of the values of the variables defined in the algorithm . f : I  O  {  } function associated to the algorithm
  • 10. function mistery( n ) i  1 j  0 for k  1 to n do j  i + j i  j - i end-for * return j 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. 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. following an algorithm. ( I )  X 0 (O 0 )  X 1 (O 1 )  ...  X m (O m )  ... If it stops in m steps the output is {O 0, O 1 ... O m } If it does not stop {O 0, O 1 ... O m …  } f : I  O  {  } with I  I , O  O , X i  X I O f ( X )
  • 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. 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. function min( n , T[1..n] ) : real x  T[1] for i  2 to n do if T[i] < x then x  T[i] end_for return x end
  • 16. function min( n , T[1..n] ) : real if n =1 then return T[1] else return MIN(min(n-1, T[1..n-1] ),T[n]) end function MIN(a,b ) : real if a < b then return a else return b end
  • 17. function min( n , T[1..n] ) : real if n =1 then return T[1] else if n=2 then if T[1] < T[2] then return T[1] else return T[2] else return min(2,[min(n-1, T[1..n-1] ),T[n]]) end
  • 18. Analysis of complexity How much resources are needed to run an algorithm as a function of the size of the problem it solves? Is the study of the amount of resources: time , [ time complexity] t(n) , memor y, [ space complexity ] s(n) , … [ other resources], that an algorithm will require to process an input of size n .
  • 19. Complexity Time complexity t(n) computational steps Space complexity s(n) storage - memory Analysis
  • 20. Size of a problem n A measure of the amount of data that an algorithm takes as input Examples: Sorting n = number of elements in the input sequence Inverting a square matrix A n = number of elements in the matrix Finding the root of a function f(x) n = number of precise digits in the root
  • 21. Multiplying two integers n = number of bits needed to represent the input Finding the shortest path in graph, in this case the size of the input is described by two numbers rather than one. n = number of vertex m = number of edges
  • 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. Worst-case analysis is appropriate for an algorithm whose response time is critical 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 Sorting average time taken on the n! different ways of initially ordering n elements (assuming they are all different)
  • 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. Data types Integer Up to values n uses c log n bits. Floating point Bounded rage of values. Elementary (primitive) instructions Arithmetic Add +, substrac -, multiply *, divide /, remainder %, floor   , ceiling   . Data movement load, store, copy. RAM Model Random Access Model
  • 26. Control Conditional and unconditional branch, subroutine call, return. Special exponentiation 2 k for k small  k-shift left. Each instructions takes a constant amount of time Memory-hierarchy effects main – cache – virtual [demand paging] are considered only in special cases
  • 27. Example function mistery( n ) i  1 j  0 for k  1 to n do j  i + j i  j - i end-for return j 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. We must attribute to the additions a cost proportional to the length of the operands For n=47 the addition “j  i + j” causes arithmetic overflow on a 32-bit machine 45.496 bits are needed to hold the result corresponding to n=65.536 time in O (n 2 ) !!!But the operations are not elementary !!!
  • 29. “ Two different implementations of the same algorithm will not differ in efficiency by more than some multiplicative constant” Principle of Invariance
  • 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. 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. function min( T[1..n] ) : real x  T[1] /* b */ for i  2 to n do /* n times */ if T[i] < x then /* a */ x  T[i] end_for return x Example
  • 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