8/3/2019 Lec 2a Algorithms 2
1/32
Lecture 2a
Algorithms 2
8/3/2019 Lec 2a Algorithms 2
2/32
Some more examples
8/3/2019 Lec 2a Algorithms 2
3/32
Example: Integer multiplication
American method
British method
Multiplying through addition
Russian method
[Brassard]
3
8/3/2019 Lec 2a Algorithms 2
4/32
Example: American multiplication
American method
Multiply numbers x[1,m] and y[1,n]
x = 27 m = 2[2 1]
y = 431 n = 3[3 2 1]
4
8/3/2019 Lec 2a Algorithms 2
5/32
Example: American multiplication
27
x 431
-------------------
7
20
210
600
2800
8000
-------------------
11637 5
27
x 431
-------------------
27
81
108
-------------------
11637
8/3/2019 Lec 2a Algorithms 2
6/32
Example: American multiplication
American method
result = 0
for i=0 to n-1
for j=0 to m-1
result=result+shift(tableMultipl(x[i],y[j]),i+j))
6
8/3/2019 Lec 2a Algorithms 2
7/32
Example: American multiplication
m*n [additions of multiposition numbers +
1-digit multiplications + multiposition shifts]
If m=n O(n2)
7
8/3/2019 Lec 2a Algorithms 2
8/32
Elementary operation
Elementary operation is an operation thatdoes not depend on the size of the instance
Above, time of all operations is bounded fromabove
addition of two numbers
one-digit multiplication shift
8
8/3/2019 Lec 2a Algorithms 2
9/32
Example: British multiplication
9
27
x 431
-------------------
108
81
27
-------------------
11637
British
27
x 431
-------------------
27
81
108
-------------------
11637
American
8/3/2019 Lec 2a Algorithms 2
10/32
Example: British multiplication
10
Analysis same result as Americanmultiplication
8/3/2019 Lec 2a Algorithms 2
11/32
Multiplication through addition
x * y = 0 + x + x + + x
or: x * y = x + x + + x
11
y times
result = 0for i=y downto 1
result = result + xy - 1 times
result = xfor i=y downto 2result = result + x
8/3/2019 Lec 2a Algorithms 2
12/32
Multiplication through addition
y additions y < 10n
O(10n
)
12
8/3/2019 Lec 2a Algorithms 2
13/32
Russian multiplication
x * y
If x = 2k x*y = 2k*y = k*2y
If x = 2k+1 x*y = (2k+1)*y = k*2y + y
13
8/3/2019 Lec 2a Algorithms 2
14/32
Russian multiplication371 * 25 25
185 50 5092 100
46 200
23 400 40011 800 800
5 1600 1600
2 32001 6400 6400
0 ---------
9275 14
8/3/2019 Lec 2a Algorithms 2
15/32
Russian multiplication431 * 27 27
215 54 54107 108 108
53 216 216
26 43213 864 864
6 1728
3 3456 34561 6912 6912
0 ---------
11637 15
8/3/2019 Lec 2a Algorithms 2
16/32
Russian multiplication
16
Russian abacus
8/3/2019 Lec 2a Algorithms 2
17/32
Russian multiplication
result = 0halves = x
doubles = y
while (halves > 0)if halves mod 2 = 1
then result = result + doubles
halves = halves div 2doubles = doubles * 2
17
8/3/2019 Lec 2a Algorithms 2
18/32
Russian multiplication
Worst casec =log2x [divisions by 2, multiplications by 2,
additions]
Eg if x = 431, c =log2431 = log2512 = 9[as in our example]
x < 10m
log2x < log2(10m) = m*log210 3.322 m O(m)c =log2x < m*log210 m*log210
18
8/3/2019 Lec 2a Algorithms 2
19/32
Efficiency of Multiplication Methods
American method O(n2)
British method O(n2)
Multiplying through addition O(10n)
Russian method O(n)
19
8/3/2019 Lec 2a Algorithms 2
20/32
Example: Calculating the value of a
Polynomial A polynomial of power n:
where
Standard method [n2/2 mult.+ n add] O(n2)
Horners method [n mult.+ n add] O(n)
20
8/3/2019 Lec 2a Algorithms 2
21/32
Standard method
y = a[0]
for i = 1 to n do
y = y + a[i]*exp(x,i)
n additions + multiplications
n additions + n2/2 multiplications
O(n2)
21
1 multiplication
i-1 multiplications
8/3/2019 Lec 2a Algorithms 2
22/32
exp
exp(x,n) = xn
y = x
for i = 2 to n
y = y * x
(n-1) multiplications
22
8/3/2019 Lec 2a Algorithms 2
23/32
Horners algorithm
y =
y = ((an)x+an-1)x++)x+a2)x+a1)x+a0
23
8/3/2019 Lec 2a Algorithms 2
24/32
Horners algorithm
y = a[n]
for i=n-1 downto 0 do
y = y*x + a[i]
n [additions, multiplications]
O(n)
24
8/3/2019 Lec 2a Algorithms 2
25/32
Summary
Notions introduced
Problem
Solution
Algorithm
Primitive operation
Program
Instance of a problem
Size of an instance
Representations of algorithms
Elementary operation
Different algorithms may solve the same problem Efficiency of these different solutions may be very different
Some solutions may be not practical for big size of an instance
8/3/2019 Lec 2a Algorithms 2
26/32
Fundamental Questions in CompSci
What can be computed?
If something can be computed, whatresources (time, memory, ) will be needed? A computation may be possible but not practical
What are the fundamental capabilities and
limitations of computers (computing agents)?
26
8/3/2019 Lec 2a Algorithms 2
27/32
Theory of Computation
Some problems do not have algorithmicsolutions at all
solvable / unsolvable problems
Some solvableproblems are easy problems,some are hard. Some problems havealgorithmic solutions that do not complete in
a reasonable amount of time. tractable / intractable problems
27
8/3/2019 Lec 2a Algorithms 2
28/32
Theory of Computation Computability Theory (Theory of abstract
automata and languages) Characterize the type of problems that can be
solved by a particular type of computing agent
Complexity Theory ( Algorithm Analysis) Characterize classes of algorithm complexity (how
hard a problem is)
Characterize efficiency of algorithms
Efficient reciprocal to complex
28
8/3/2019 Lec 2a Algorithms 2
29/32
Theory of computation
Yet another issue: Models of computation
Structure and organization of the computingagent
Most widely used:
RAM machine (corresponds to the von Newmanarchitecture, machine- and assembly-languageprograms)
PRAM (parallel RAM)
While-programs (corresponds to high-levelprogramming languages)
Many others: -calculus (functional programming),inference systems in logic (logic programming),
production systems, 29
8/3/2019 Lec 2a Algorithms 2
30/32
Theory of Computation
Another issue
Correctness of algorithms
We wont touch it
30
8/3/2019 Lec 2a Algorithms 2
31/32
From the next lecture will start algorithmanalysis systematically
31
8/3/2019 Lec 2a Algorithms 2
32/32
HW: implement the four multiplicationalgorithms in Java or another programming
language. Submit program code + snapshotof execution. More details in the HWdocument.
32
Homework