Académique Documents
Professionnel Documents
Culture Documents
Humberto Longo
I
I
Instituto de Informtica
Universidade Federal de Gois
I
I
I
(1 1 de 1025)
Instrues de mquina
instrues de mquina;
operaes;
instrues de controle;
instrues condicionais e loops;
funes;
funes recursivas.
Instrues de mquina
Operadores
Operadores
Blocos de operaes
Sequncia de blocos
(1)
// Trocar contedo
// de variveis
int temp = x;
x = y;
y = temp;
// Atualizar sequncia
// de variveis
++i;
anterior = atual;
atual = proximo;
proximo = tabela_valores[i];
T *array_old = array;
int capacity_old = array_capacity;
array_capacity += delta;
array = new T[array_capacity];
(n)
(1)
delete[] array_old;
Sequncia de blocos
Instrues de controle
i f ( condio )
{
// b l o c o 1 de i n s t r u e s
}
else
{
// b l o c o 2 de i n s t r u e s
}
Instrues de controle
Instrues de controle
Instrues de controle
Instrues de controle
1
2
3
i n t find_max ( i n t V , i n t n )
{
max = V [ 0 ] ;
f o r ( i n t i = 1 ; i < n ; ++i )
{
max = (V [ i ] > max ) ? V [ i ] : ;
}
5
6
7
8
9
10
11
r e t u r n max ;
2
3
4
f o r ( i n t i = 0 ; i < N ; ++i )
{
// b l o c o de i n s t r u e s .
}
Declarao equivalente:
1
2
3
4
5
6
int i = 0;
// i n i c i a l i z a o
w h i l e ( i < N) // c o n d i o de p a r a d a
{
// b l o c o de i n s t r u e s
++i ;
// i n c r e m e n t o
}
1
2
3
4
f o r ( i n t i = 0 ; i < n ; ++i )
{
// b l o c o de i n s t r u e s (f (n)) .
}
i n t sum = 0 ;
f o r ( i n t i = 0 ; i < n ; ++i )
{
f o r ( i n t j = 0 ; j < n ; ++j )
{
sum += 1 ;
// (1)
}
}
(1)
(1), se sets = n;
(n), caso contrrio.
void
Disjoint_sets::clear() {
if (sets == n) {
return;
}
(1)
i=0
parent[i] = i;
tree_height[i] = 0;
}
2
3
4
max_height = 0;
num_disjoint_sets = n;
(n)
f o r ( i n t i = 0 ; i < n ; ++i )
{
// b u s c a l i n e a r em m a t r i z 1 m O(m) .
}
f o r ( i n t i = 0 ; i < n ; ++i )
{
// b l o c o de i n s t r u e s O(f (i, n)) .
}
e o loop externo :
(1) +
n1
X
!
((1) + (i))
= (1) + (n) +
i=0
n1
X
!!
i=0
= (n2 ).
5
6
1
2
3
4
5
6
7
8
i n t sum = 0 ;
f o r ( i n t i = 0 ; i < n ; ++i )
{
f o r ( i n t j = 0 ; j < i ; ++j )
{
sum += i + j ;
}
}
8
9
10
11
Sequncias de instrues
< i ; ++j )
k < j ; ++k )
+ k;
n ; ++i )
Sequncias de instrues
I
I
i n t sum = 0 ;
for ( int i = 0; i <
{
for ( int j = 0; j
{
for ( int k = 0;
{
sum += i + j
}
}
}
Sequncias de instrues
Funes
I
I
Resumo:
I
I
Exemplos:
I
I
I
I
Funes
Funes
Funes
I
Funes recursivas
2Tf ind
void
Disjoint_sets::set_union(int m, int n) {
m = find( m );
n = find( n );
(1)
Fatorial:
(1)
if (m == n) {
return;
}
T (n 1) + (1)
--num_disjoint_sets;
if (tree_height[m] >= tree_height[n]) {
parent[n] = m;
if (tree_height[m] == tree_height[n]) {
++(tree_height[m]);
max_height = std::max( max_height, tree_height[m] );
}
} else
parent[m] = n;
Funes recursivas
Funes recursivas
para k = n 1:
T (n) = T (n 1) + (1)
T (n 1) + c1
= (T (n 2) + c1 ) + c1 = T (n 2) + 2c1
= T (n 3) + 3c1
..
.
= T (1) + (n 1)c1
= (1) + c1 n c1
c2 + c1 n c1
= c1 n + (c2 c1 ).
= T (n k) + kc1
Funes recursivas
Funes recursivas
Selection sort
Selection sort
1
2
3
void s e l e c t i o n S o r t ( i n t array ,
i f ( n <= 1 )
return ;
int n ) {
(1)
i n t posn = 0 ;
i n t max = a r r a y [ p o s n ] ;
5
6
(1)
//
caso especial:
if (n <= 1){
return;
}
f o r ( i n t i = 1 ; i < n ; ++i ) {
i f ( a r r a y [ i ] > max ) {
posn = i ;
max = a r r a y [ p o s n ] ;
}
}
8
9
10
11
12
13
14
i n t tmp = a r r a y [ n 1 ] ;
a r r a y [ n 1 ] = a r r a y [ posn ] ;
a r r a y [ p o s n ] = tmp ;
15
16
17
18
19
20
s e l e c t i o n S o r t ( array , n 1 ) ;
Funes recursivas
Funes recursivas
Selection sort
Selection sort
(n)
posn = i;
max = array[posn];
};
};
(1)
T (n 1)
//
Troca a posio do maior com o ltimo.
int tmp = array[n - 1];
array[n - 1] = array[posn];
array[posn] = tmp;
// Classifica os demais elementos.
[sort( array, n - 1 );
Funes recursivas
Funes recursivas
Selection sort
Selection sort
Relao de recorrncia:
(
(1),
se n 1;
T (n) =
T (n 1) + (n), se n > 1.
Relao de recorrncia:
T (n) T (n 1) + c1 n
= (T (n 2) + c1 (n 1)) + c1 n
= T (n 2) + c1 n + c1 (n 1)
Verso simplificada:
= T (n 3) + c1 n + c1 (n 1) + c1 (n 2)
..
.
n
n
X
X
= T (1) + c1
i = (1) + c1
i
(
c2 ,
se n 1;
T (n)
T (n 1) + c1 n, se n > 1.
i=2
max{c1 , c2 }
n
X
i=1
i=2
i = max{c1 , c2 }
n(n + 1)
2
(n ).
Funes recursivas
Funes recursivas
Busca binria
Busca binria
Cdigo C:
1
2
3
4
5
i n t b u s c a B i n a r i a ( i n t V , i n t cha ve ,
{
i n t i n f = 0 ; // L i m i t e i n f e r i o r
i n t s u p = n 1; // L i m i t e s u p e r i o r
i n t meio ;
Relao de recorrncia:
(
(1),
se n 1;
n
T (n) =
T 2 + (1), se n > 1.
Verso simplificada:
int n )
w h i l e ( i n f <= s u p )
{
meio = i n f + ( s u p i n f ) / 2 ;
i f ( c h a v e == V [ meio ] )
r e t u r n meio ; // E n c o n t r o u e l e m e n t o
else
i f ( c h a v e < V [ meio ] )
s u p = meio 1;
else
i n f = meio +1;
}
r e t u r n 1;
// No e n c o n t r o u
7
8
9
10
11
12
13
14
15
16
17
18
19
(
c2 ,
se n 1;
n
T (n)
T 2 + c1 , se n > 1.
I
Funes recursivas
Funes recursivas
Busca binria
Busca binria
I
I
k1
T (n) T (2k1 ) + c1
2
n 2 , onde k um nmero inteiro positivo.
2k1 n k lg nl+ 1.
m
k
Para n 2k , n2 22 = 2k1 = 2k1 .
= T (2k2 ) + 2c1
..
.
T (1) = T (20 ) = 1.
= T (2ki ) + ic1 .
Quando 2ki = 20 i = k:
T (n) T (1) + c1 k
c2 + c1 (lg n + 1) = c1 lg n + (c1 + c2 ).
Funes recursivas
Busca binria
I
Lembre-se que:
f (n) (g(n)) se lim
f (n)
= c, para 0 < c < .
g(n)
Logo,
lim
c1 lg n + (c1 + c2 )
lg n
c1 + c2
= lim c1
+ lim
n
ln n
ln n n ln n
= c1 lim
ln n
ln 2
+0
ln n
1
= c1 lim
c1 1, 4427.
n ln 2
n
1
n
do
n
1 n(n + 1)
n+1
1 X
i=
=
,
n
n
2
2
i=1
que O(n).
Assim:
I
I
1
2
3
X
X
1
1
C(n) =
<
=1
i
2
2i
i=1
i=1
f o r ( i n t i = 1 ; i < n ; ++i )
{
max = (V [ i ] > max ) ? V [ i ] : ;
}
5
6
7
8
9
10
11
r e t u r n max ;
Livros Texto I
n
n
X
X
1
1
1
C(n) =
=
=1+
i+1
i
i
i=0
i=1
i=2
n
Z n
1
dx = 1 + ln(x)
1+
1 x
1
J. L. Gersting
Fundamentos Matemticos para a Cincia da Computao.
LTC Editora, 3 Edio, 2001.
R. P. Grimaldi
Discrete and Combinatorial Mathematics An Applied Introduction.
Addison Wesley, 1994.
D. J. Velleman
How To Prove It A Structured Approach.
Cambridge University Press, 1996.
T. H. Cormen, C. E. Leiserson e R. L. Rivest.
Introduction to Algorithms.
McGraw-Hill, New York, 1990.
Livros Texto II
C. H. Papadimitriou, U. V. Vazirani e S. Dasgupta.
Algoritmos.
Mcgraw-Hill Brasil, 2009.
U. Manber.
Algorithms: A Creative Approach.
Addison-Wesley, 1989.
D. E. Knuth.
The Art of Computer Programming. Volume 1 Fundamental Algorithms.
Addison Wesley, 1998.
D. E. Knuth.
The Art of Computer Programming. Volume 2 Sorting and Searching.
Addison Wesley, 1998.
N. Ziviani.
Projeto de Algoritmos com Implementaes em Pascal e C.
Editora Thomson. 2a Edio. 2004.