• No se han encontrado resultados

Crecimiento de funciones - Complejidad

N/A
N/A
Protected

Academic year: 2021

Share "Crecimiento de funciones - Complejidad"

Copied!
20
0
0

Texto completo

(1)

Crecimiento de funciones - Complejidad

AyED1

Big-O

DC - FCEN

(2)

Objetivo

Tener una noci´

on de que tan ”eficiente” es nuestro algoritmo.

Poder comparar algoritmos entre s´ı.

(3)

Eficiencia

Eficiencia espacial: Espacio utilizado por el algoritmo.

Eficiencia temporal: Velocidad del algoritmo respecto de su entrada.

Nosotros nos vamos a centrar en la ´

ultima.

(4)

Comparar dos algoritmos

Existe, lo correcto..

1 i n t s u m a r D o s I n t ( i n t n1 , i n t n2 ) { 2 i n t r e s u l t a d o = n1 ; 3 i n t i = 0 ; 4 w h i l e ( i <=n2 ) { 5 r e s u l t a d o ++; 6 i ++; 7 } 8 r e t u r n ( r e s u l t a d o ) ; 9 }

y lo correcto..

1 i n t s u m a r D o s I n t ( i n t n1 , i n t n2 ) { 2 r e t u r n ( n1+n2 ) ; 3 }

(5)
(6)

etricas para algoritmos

Por cantidad de l´ıneas.

1 v o i d n u n c a H a g a n E s t o ( ) { 2 w h i l e ( t r u e ) 3 c o u t <<” . . ” ; 4 }

(7)

Medir algoritmos por tiempo

1 v o i d i m p r i m i r M u c h o s 9 ( ) { 2 i n t i =0; 3 w h i l e ( i <99999999){ 4 c o u t <<9; 5 ++i ; 6 } 7 }

Tarda 2 meses

1

Tarda 0 segundos

1

(8)

La posta — Big-O

Definicion. Si f y g son dos funciones, decimos que f ∈ O(g ) si existen

c1 ∈ R y n

0

∈ N tales que:

(9)
(10)

La posta — Big-O

F

∈ O(G)? o

G

∈ O(F

)?

G

∈ O(F

) con n

0

= 10 y c

1

= 1

(11)

La posta — Big-O

(12)

Nomenclatura

O(1) = Tiempo constante.

O(n) = Tiempo lineal .

O(n

k

) con k fijo = Tiempo polinomial .

...

(13)

Ejercicios

Easy 1, 2 y 3.

Medium.

Exactas.

(14)

Ejercicios - Easy 1

1 // R e q u i e r e : n>0 && | a r r e g l o |==n 2 i n t maximo ( i n t [ ] a r r e g l o , i n t n ) { 3 i n t maximo = a r r e g l o [ 0 ] ; 4 i n t i =1; 5 w h i l e ( i <n ) { 6 i f ( maximo<a r r e g l o [ i ] ) 7 maximo = a r r e g l o [ i ] ; 8 i ++; 9 } 10 r e t u r n maximo ; 11 }

Complejidad? O(n)

(15)

Ejercicios - Easy 2

1 // R e q u i e r e : | a r r e g l o |==n && e l e m e n t o i n a r r e g l o 2 i n t p o s i c i o n E l e m e n t o ( i n t [ ] a r r e g l o , i n t n , i n t e l e m e n t o ) { 3 i n t i =0; 4 w h i l e ( i <n && e l e m e n t o != a r r e g l o [ i ] ) 5 i ++; 6 r e t u r n i ; 7 }

Complejidad? O(n)

(16)

Ejercicios - Easy 3

1 // R e q u i e r e : | a r r e g l o |==n 2 i n t s u m a r T r e s L e j a n o s ( i n t [ ] a r r e g l o , i n t n ) { 3 i n t i =0; 4 w h i l e ( i <n ) { 5 suma += a r r e g l o [ i ] ; 6 i+=n / 2 ; 7 } 8 suma += a r r e g l o [ n − 1 ] ; 9 r e t u r n suma ; 10 }

Complejidad? O(1)

(17)

Ejercicios - Medium

1 // R e q u i e r e : | a r r e g l o |==n 2 v o i d o r d e n a r S i H a y U n 1 ( i n t [ ] a r r e g l o , i n t n ) { 3 i n t i =0 , j ; 4 i f ( hayUn1 ( a r r e g l o , n ) ) { 5 s e l e c t i o n S o r t ( a r r e g l o , n ) ; 6 } 7 } 8 9 v o i d s e l e c t i o n S o r t ( i n t [ ] & a r r e g l o , i n t n ) { 10 i n t i =0 , j ; 11 w h i l e ( i <n ) { 12 j= i ; 13 w h i l e ( j <n ) { 14 i f ( a r r e g l o [ i ]> a r r e g l o [ j ] ) 15 swap ( a r r e g l o [ i ] , a r r e g l o [ j ] ) 16 j ++; 17 } 18 i ++; 19 } 20 }

(18)

Ejercicios - Medium

1 b o o l hayUn1 ( i n t [ ] a r r e g l o , i n t n ) { 2 i n t i =0; 3 w h i l e ( i <n ) { 4 i f ( a r r e g l o [ i ]==1) 5 r e t u r n t r u e ; 6 i ++; 7 } 8 r e t u r n f a l s e ; 9 }

10 v o i d swap ( i n t& a , i n t& b ) { i n t aux = a ; a = b ; b = aux ; }

Complejidad? O(n

2

)

(19)

Ejercicios - Exactas

Dadas dos barajas de cartas mezcladas (la primera de tama˜

no m y la

segunda de tama˜

no n) decir que carta es la m´

as repetida (o alguna si son

varias). Las cartas se numeran de 1 a 30. El algoritmo no puede ser mayor

a O(n+m).

(20)

Ejercicios - Exactas

Utilizamos un counting-sort..

1 i n t c a r t a M a s R e p e t i d a ( v e c t o r <i n t > b a j a r a 1 , v e c t o r <i n t > b a j a r a 2 ) { 2 v e c t o r <i n t > c a r t a s ( 3 0 , 0 ) ; 3 i n t i =0; 4 w h i l e ( i <b a r a j a 1 . s i z e ( ) ) { 5 c a r t a s [ b a r a j a 1 [ i ] ] + + ; 6 ++i ; 7 } 8 i =0; 9 w h i l e ( i <b a r a j a 2 . s i z e ( ) ) { 10 c a r t a s [ b a r a j a 2 [ i ] ] + + ; 11 ++i ; 12 } 13 i n t max = 0 ; 14 i =0; 15 w h i l e ( i <c a r t a s . s i z e ( ) ) { 16 i f ( c a r t a s [ i ]> c a r t a s [ max ] ) 17 max = i ; 18 i ++; 19 } 20 r e t u r n max ;

Referencias

Documento similar

Products Management Services (PMS) - Implementation of International Organization for Standardization (ISO) standards for the identification of medicinal products (IDMP) in

This section provides guidance with examples on encoding medicinal product packaging information, together with the relationship between Pack Size, Package Item (container)

Y tendiendo ellos la vista vieron cuanto en el mundo había y dieron las gracias al Criador diciendo: Repetidas gracias os damos porque nos habéis criado hombres, nos

E Clamades andaua sienpre sobre el caua- 11o de madera, y en poco tienpo fue tan lexos, que el no sabia en donde estaña; pero el tomo muy gran esfuergo en si, y pensó yendo assi

diabetes, chronic respiratory disease and cancer) targeted in the Global Action Plan on NCDs as well as other noncommunicable conditions of particular concern in the European

d) que haya «identidad de órgano» (con identidad de Sala y Sección); e) que haya alteridad, es decir, que las sentencias aportadas sean de persona distinta a la recurrente, e) que

De hecho, este sometimiento periódico al voto, esta decisión periódica de los electores sobre la gestión ha sido uno de los componentes teóricos más interesantes de la

Las manifestaciones musicales y su organización institucional a lo largo de los siglos XVI al XVIII son aspectos poco conocidos de la cultura alicantina. Analizar el alcance y