You are on page 1of 20

http://eclass.aueb.

gr/courses/INF161/
2016 - I.

- 2016 - . - 03 - EXAMPLES ALG & COMPL 1


Example: GCD
gcd(25,11)
gcd(a,b) 25 11
: b>0 14 11
: a,b 3 11
while a <> b do 3 8
if a > b: a=ab 3 5
else: b=ba 3 2
return a 1 2
1 1
O: if a>b then gcd(a,b) = gcd(a-b, b)
:
g=GCD(a,b), a=gx , b=gy.
x,y ( g ).

GCD(a-b,b) = GCD(gx-gy, gy) = GCD( g(x-y), gy ) = g


x,y ,

?
- 2016 - . - 03 - EXAMPLES ALG & COMPL 2
Example: GCD
gcd(100,1)
gcd(a,b)
: b>0 100 1
: a,b 99 1
while a <> b do 98 1
if a > b: a=ab 100
else: b=ba
return a 2 1
1 1

: a=b: O(1)
: a >1, b=1: O(a)
O(a)=O(2n )
(# bits): n = log a + log b 2 log a = O(log a)
H gcd(a,b) ( n)
?
- 2016 - . - 03 - EXAMPLES ALG & COMPL 3
Example: GCD - O(log )
Euclid(a,b) gcd(25,11) gcd(100,1)
: b>0 25 11 100 1
: a,b 11 3 1 0
if b=0: return a 3 2
return Euclid(b, a mod b) 2 1
1 0

O: if a>b then gcd(a,b)= gcd(a mod b, b)


k
: a
a= k b + a mod b, k : a mod b = a b b - -b
b
if a>b then gcd(a,b)= gcd(a-b, b)
.
?
- 2016 - . - 03 - EXAMPLES ALG & COMPL 4
Example: GCD - O(log )
Euclid(a,b)
: b>0
: a,b
if b=0: return a
return Euclid(b, a mod b)

: if a b then a mod b < a/2

if ba/2 then a mod b<b< a/2 if b >a/2 then a mod b=a-b < a/2

- 2016 - . - 03 - EXAMPLES ALG & COMPL 5


Example: GCD - O(log ) gcd(25,11)
Euclid(a,b) 25 11
: b0 11 3
: a,b 3 2
if b=0: return a 2 1
return Euclid(b, a mod b) 1 0
: if a b then a mod b < a/2
a b
b a mod b
a mod b b mod (a mod b)
(a/2) (b/2)

2 loga = O(log a) = O(n)


(# bits) n = O(log a)
Euclid(Fk+1 ,Fk )

- 2016 - . - 03 - EXAMPLES ALG & COMPL 6


Example: Merge two sorted arrays - O(n)
merge (A[1..p], B[1..q])
: ,
: C
,
A[p+1]=, B[q+1]=
i=1, j=1, n=p+q
for k=1 to n:
if A[i] B[j]: C[k]=A[i], i=i+1
else: C[k]=B[j], j=j+1

A: 2 4 5 7 A: 2 4 5 7
C=1 2 2 3 4 5 6 7
B: 1 2 3 6 B: 1 2 3 6

: (n)
, /
(n=p+q)

: 2n=(n), A = n ?
7
- 2016 - . - 03 - EXAMPLES ALG & COMPL
Example: Selection Sort - O(n2)
SelectionSort (A[1..n])
: [1..n]
:
for i=n,n-1,3,2:
find the position p of max element in A[1..i]
swap(A[p], A[i])

: (n2) i : max(A[1..n])

for i = n, n-1, n-2, , 3, 2


: n + n-1 + n-2 + + 3 + 2
n n
n ( n 1)
: i i (n 2 )
i2 i 1 2
( = = )

8
- 2016 - . - 03 - EXAMPLES ALG & COMPL
Insertion Sort - O(n2)
InsertionSort (A[1..n])
: [1..n]
:
for i=2 to n:
j = i
while (A[j]<A[j-1] and j>1):
swap (A[j], A[j-1])
j = j-1

: 5 12 7 20 6 3
5 12 7 20 6 3
5 7 12 20 6 3
5 7 12 20 6 3
5 6 7 12 20 3
3 5 6 7 12 20
9
- 2016 - . - 03 - EXAMPLES ALG & COMPL
Insertion Sort - O(n2)
InsertionSort (A[1..n])
: [1..n]
:
for i=2 to n:
j = i
while (A[j]<A[j-1] and j>1):
swap (A[j], A[j-1])
j = j-1

:
( )
(BEST CASE)
-
- 1 while for
- n-1 (i=2 to n)
- (n)

10
- 2016 - . - 03 - EXAMPLES ALG & COMPL
Insertion Sort - O(n2)
InsertionSort (A[1..n])
: [1..n]
:
for i=2 to n:
j = i
while (A[j]<A[j-1] and j>1):
swap (A[j], A[j-1])
j = j-1

:
( )
(WORST CASE)
-

- for (i=2 to n)

i while (j=i,i-1,2,1)
n n
n ( n 1)
- i i (n 2 )
i2 i 1 2
11
- 2016 - . - 03 - EXAMPLES ALG & COMPL
Insertion Sort - O(n2)
InsertionSort (A[1..n])
: [1..n]
:
for i=2 to n:
j = i
while (A[j]<A[j-1] and j>1):
swap (A[j], A[j-1])
j = j-1

:
( )
(VERAGE CASE)

(Expected)


(n2 )
12
- 2016 - . - 03 - EXAMPLES ALG & COMPL
(Sorting)
SelectionSort
InsertionSort
: (n2 )
: =
n O(log M)
log M =, .. log M =32
= O(n)

? : (n logn)
? !

- 2016 - . - 03 - EXAMPLES ALG & COMPL 13


Example: Matrix Multiplication - O(n3)
: X Y: n x n
: Z=X Y

n
Z ij X
k 1
ik Ykj

: O(n3)

- 2016 - . - 03 - EXAMPLES ALG & COMPL 14


Example: Matrix Multiplication - O(n3)
Mult (X[1..n,1..n], Y[1..n,1..n])
: X, Y
: Z = XY
for i=1 to n:
for j=1 to n:
Z[i,j]=0
for k=1 to n:
Z[i,j]= Z[i,j]+X[i,k] Y[k,j]

: O(n3)
n x m m x q :
O(nmq)

? ,

- 2016 - . - 03 - EXAMPLES ALG & COMPL 15


Example: Maximum sub-array
: [1..n]
: - [p..q]

p, q 0 p q n
q
V ( p, q ) A(i)
i p
, V ( p, q ) V ( p ' , q ' ), 1 p ' q' n

:
Year 1 2 3 4 5 6 7 8 9

Profit -3 2 1 -4 5 2 -1 3 -1

: V(5,8)=9

- 2016 - . - 03 - EXAMPLES ALG & COMPL 16


Example: Maximum sub-array - O(n3)
: [1..n]
: - [p..q]
Brute Force
V(i,j) 1 i j n
Vmax p,q

Vmax=A(1), p=1, q=1


for i=1 to n:
for j=i to n:
V=0
for k=i to j: do V=V+A(k)
if V>Vmax: O(n3 )
Vmax= V, p=i, q=j
return Vmax, p, q

- 2016 - . - 03 - EXAMPLES ALG & COMPL 17


Example: Maximum sub-array - O(n2)
: [1..n]
: - [p..q]
i j j+1
Re-use of data

V (i, j ) V (i ) V (i 1) ... V ( j 1) V ( j ) V (i, j 1) A( j )



V ( i , j 1)
Vmax=A(1), p=1, q=1
for i=1 to n: : O(n2 )
V=0
for j=i to n:
?
V=V+A[j]
if V>Vmax:
Vmax= V, p=i, q=j !
return Vmax, p, q (nlogn)
(n) !

- 2016 - . - 03 - EXAMPLES ALG & COMPL 18


Example: k-Subset Sum - O(nk)
: S ,
: S, k , |A|=k,
=M?
k:
Brute force
S,
for S, ||=k: ||=k,
V=0 n ? n
for x A: V=V+x
if V=M then return YES k
return NO

O(1)
:
||=k =

= O(nk) k:

- 2016 - . - 03 - EXAMPLES ALG & COMPL 19


Example: Subset Sum - O(2n)
: S ,
: S =M?

Brute force

for S:
n ?
V=0
for x A: V=V+x : 2n
if V=M then return YES
return NO
|A|=O(n)
( n)
: O(n2n)

? (nM)

- 2016 - . - 03 - EXAMPLES ALG & COMPL 20

You might also like