You are on page 1of 40

http://eclass.aueb.

gr/courses/INF161/
2016

I.

: , 13:15-15:00
12, 3
( 4267)
E-mail: milis@aueb.gr


Web search
Text editors










- 2016 - . - 01 - 2
Shortest path
Dijkstra 1959

define Dijkstra(Graph G, Node s)


S := {}
Q := Nodes(G)
while not empty(Q)
u := extractMin( Q )
Network flows S := S u
for each node v in neighbors( u )

Internet routing
if d(u) + w(u,v) < d(v) then
d(v) := d(u) + w(u,v)
pi(v) := u


Distance (Delhi, Bangalore)
- 2016 - . - 01 - Path (Delhi, Bangalore)3
Avi Wigderson
Text CAUCGCGCUUCGC
String matching String CGC
Knuth-Morris-Pratt
Boyer-Moore 1977 algorithm kmp_search:
input: T (text), P (pattern sought)
define variables:
m 0, i 0, M (the table)

Spell checking while m + i is less than length of T, do:


if P[i] = T[m + i], let i i + 1

Text processing if i = length of P then return m


otherwise, let m m + i - M[i],

Genome if i > 0 let i M[i]

Molecular Biology
Web search Text CAUCGCGCUUCGC
Location X X X
4
- 2016 - . - 01 -
Avi Wigderson

-
( , -, )

:

( )

, ,

- 2016 - . - 01 - 5



,

/ (INPUT/INSTANCE):
, o

/ (UTPUT/QUESTION):

- 2016 - . - 01 - 6

:

? -
?
?

?
?
= T(n)
n ( # bits)

- 2016 - . - 01 - 7

Al Khwarizmi "Dixit algorizmi
(780-850 ..) 12 ..

+


+
x ,

53 13
+35 x 11
88 13
13
143

- 2016 - . - 01 - 8

1 111 ()
1 1 0 1 0 1 (53) n bits
+ 1 0 0 0 1 1 (35) n bits
1 0 1 1 0 0 0 (88) n+1 bits

? ()
3 2 (?)

/ ? ()
=n+1, : (n) = c1 n + c2 = O(n)
32/64 bits
1 !

?
:
- 2016 - . - 01 - 9
1
1 1 0 1 (13) n bits
x 1 0 1 1 (11) n bits
1 1 0 1 (1101 1) n bits
1 1 0 1 (1101 1, 1 ) n+1 bits
0000 (1101 0, 2 )
+1101 (1101 1, 3 ) 2n-1 bits
1 0 0 0 1 1 1 1 (143) 2n bits
? ()

/ ? ()
: n
+ n-1 2 x O(n)
T(n)=n+(n-1) n = O(n2)
?
- 2016 - . - 01 - 10
2
11 x13=11+2(11 x 6)
=11+2(2(11 x 3))
=11+2(2(11+2(11 x 1)))

- 2016 - . - 01 - 11
2

//

11 x13=11+2(11 x 6)
=11+2(2(11 x 3))
=11+2(2(11+2(11 x 1)))

z=x+2z=143
z=2z=66
z=x+2z=33 z=x+2z=11
z=0
11,13
11,6 11,3
11,1
x,y 11,0

- 2016 - . - 01 - 12
2

:
( )

- 2016 - . - 01 - 13
2

//

:
n (n=# bits) ?
: 2 (right shift) 1
(LSB) 1
2 (left shift) 1
( y ) (n)
T(n) = n(3+ n) = (n2) !
- 2016 - . - 01 - 14

2 (n2)
?

:
n x ( y)
n
(n2)

32/64 bits
1
- 2016 - . - 01 - 15
Exponentiation
exp1(a,N)
: - ,
: a
p = 1
for i = 1 to N: p = p a
return p

O:
:
, , ()

: = T(n), n: bits

E: =T(N), N:
n: bit

- 2016 - . - 01 - 16
Exponentiation
exp1(a,N): ()

I n ? (n = bits )
?
- n log b ( N 1)
n
: n log
2 ( N 1) ( 2 )

exp1(a,N): O(N)=(2n )
n
n=MHKO = bits /
?

- 2016 - . - 01 - 17
Exponentiation
N 2
a 2 if N is even

N

a a0 1
N
2

2
a a if N is odd

exp2(a,N)
: - ,
: a
if =0: return 1
z = exp2 (a, N / 2 );
if N : return z2
else: return az2

O:
18
- 2016 - . - 01 -
Exponentiation
exp2(a,N)
: - ,
: a
if =0: return 1
z = exp2 (a,N / 2 );
if N : return z2
else: return az2

z=az2=a13
z=z2=a6
z=az2=a3 z=az2=a
z=1
a,13
a,6 a,3
a,1
a,N a,0

19
- 2016 - . - 01 -
Exponentiation
exp2(a,N)
: - ,
: a
if N=0: return 1
z= exp2 (a, N / 2)
if N : return z2
else: return az2
:
N N N N
k : N , , 2 , 3 ,..., k 1 k log N
2 2 2 2
( log N 1 )

exp2(a,N): (log )=O(n)


20
- 2016 - . - 01 -
Fibonacci numbers
Leonardo da Pisa (Fibonacci) 1171-1249

Liber Abaci, 1202

Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,

FN 1 FN 2 if N 2

FN 1 if N 1
0 if N 0

, FN 20.694 N

- 2016 - . - 01 - 21
Fibonacci numbers
fib1(N)
: - n
: FN
if N<2: return N
return fib1(N-1) + fib1(N-2)

O:
:
T(N): FN
T(0) =1
T(1) =1
T(N) = T(N-1) + T(N-2) +2 ( N<2, )

(N) > FN : ! (200) >F200 2138

- 2016 - . - 01 - 22
Fibonacci numbers
fib1(6):
1 st call
6
nd
2
17 th
5 4
3 rd
12 th 18 th 23 rd
4 3 3 2
4 th 9 th 13 th 19 th 22 nd
3 2 2 1 2 1 1 0
16 th 24 th 25 th call
5 th
2 1 1 0 1 0 1 0
8 th 10 th 11 th 14 th 15 th 20 th 21 st
1 0
th
6 7 th

N/2 > 2 N/2 (n) > 2 N/2

? !


- 2016 - . - 01 - 23
Fibonacci numbers
fib2(N)
: - N
: FN
f[0..N]
f[0]=0, f[1]:=1
For i=2 to N: f[i]= f[i-1] + f[i-2]
return f[N]
O:
: O(N),
=n= # bits = logN => N=2n
(2n ),
n
: , (N)
(3) ?

- 2016 - . - 01 - 24
Fibonacci numbers
N N 1 5 1 5
1. FN , where and
5 5 2 2
N N 1
FN round Fn
5 5 2
( !)
exp2 ( , N ) 1
F N
5 2
: O(log ) , .

2. F (F F )F
2N N 1 N 1 N
F F2 F2
2N 1 N N 1
: O(log ) ,

25
- 2016 - . - 01 -
Primality
prime(N)
: - N
: / N /
for i=2 to N :
if mod i= 0: return OXI
return NAI

O:

:

If N=2k: 1

If N=3k: 2 !

If N is prime: N ???

- 2016 - . - 01 - 26
Primality
prime(N)
: - N
: / N /
for i=2 to N :
if ( mod i)= 0: return OXI
return NAI

: O(1/2), if
n= # bits = logN => N=2n
(2n/2 )
MH = n

? , (log6 ), KS primality test, 2002

- 2016 - . - 01 - 27

, n
?
(Best-case complexity)
n

(Worst-case complexity)
n


(Average case complexity)
n

- 2016 - . - 01 - 28
Example: MAX
max(A[1..n])
: [1..n]
: H
max= A[1], position=1
for i=2 to n:
if A[i]>max: max=A[i], position=i
return position


= = = (n)

?
log A[1]+log A[2]++ log A[n] nM, =32/64 bits ( )

= (n) !

To n
- 2015 - . - 03 - 29
Example: MAX, # assignments
max(A[1..n])
: [1..n]
: H
max= A[1], position=1
for i=2 to n: (*)
if A[i]>max: max=A[i], position=i
return position

T(n) = # of assignments (*)

: T(n) = 1 : O(1)

: T(n) = n : O(n)

: ?

- 2015 - . - 03 - 30
Example: MAX, # assignments
max(A[1..n])
: [1..n]
: H
max= A[1], position=1
for i=2 to n:
if A[i]>max: max=A[i], position=i (*)
return position
(Average case)
n! n :
Ti = 1,0 : assignment in the ith iteration
Pr [assignment in the ith iteration] = Pr [ A[j] < A[i], j<i ] = 1/i
Pr [no assignment in the ith iteration] = Pr [ j<i : A[j] > A[i] ] = (i-1)/i
n
Expected value rule E( x) pk xk :
1 i 1 1
E[Ti ] 1 0
k 1 i i i
31
- 2015 - . - 03 -
Example: MAX, # assignments
max(A[1..n])
: [1..n]
: H
max= A[1], position=1
for i=2 to n: (*)
if A[i]>max: max=A[i], position=i
return position
(Average case)
n! n :
1
Ti = 1,0 : assignment in the ith iteration E(Ti )
n i
T(n) : total # of assignments T (n) Ti
i 1
n n n
1
ET (n) E Ti E[Ti ] H n O(log n)
i 1 i 1 i 1 i

(Linearity of Expectation)
32
- 2015 - . - 03 -

k log b n b k n,

log b n, log 2 n log n, log e n ln n

log a n log10 n
log b n : log 2 n 3.322 log10 n
log a b log10 2

log n O(log n)
b

- 2016 - . - 01 - 33

log N :
2

1
log N

bit log( 1)

log N
1 1 1
H N 1 O(log N )
2 3 N

- 2016 - . - 01 - 34

:

? -
?
?

/ ? -
~ # , ~ #
T(n) n ( # bits)
T(n) as n increases (n)
T(n)
Notations O, , , , ,

- 2016 - . - 01 - 35

-

(Divide and Conquer)
(Greedy algorithms)
(Dynamic Programming)
:
-
:
(---)
(, , )



- 2016 - . - 01 - 36
-

(..), 11-1, 24
(..), 3-5, 24


(..)
(. )

,
,


eclass ( 12 /)

- 2016 - . - 01 - 37

[DPV] S. Dasgupta, C.H. Papadimitriou, U.V. Vazirani : Algorithms
http://www.cs.berkeley.edu/~vazirani/algorithms/

[CLRS] T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein:


Introduction to Algorithms

[KT] J. Kleinberg, E.Tardos: Algorithm Design

[PAP] C. H.Papadimitriou: Computational Complexity

WWW

- 2016 - . - 01 - 38


: ~5 , 9 11
90

12% +12%+12% ( )


35/100

- 2016 - . - 01 - 39



!

- 2016 - . - 01 - 40

You might also like