Abstract Informatics is concerned with such topics as
Abstract Informatics is concerned with such topics as theory and properties of algorithms , methods for the design and analysis of algorithms, programming and other languages for describing algorithms and so on. We now study these description schemes in more detail and also introduce several important analysis issues.
Euclid • Euclid live in the city of Euclid is the most famous ancient Greek mathematician. • With regard to the life we knows very little. About early schooling in Athens, and well aware of Plato's theory. • Euclid’s algorithm ( Greatest Common Divisor)
‘Greatest common divisor’ is based on the following two theorems. • Theorem 1. 1 – Given positive integers m and n , there exist unique integers q and r such that q ≥ 0, 0≤r<n, and m = q ×n+r • Theorem 1. 2 – Given positive integers m and n and the unique integers q and r such that q ≥ 0, 0≤r<n , and m = q × n + r , then • (1) if r = 0, then gcd ( m , n) = n , and • (2) if r≠ 0, then gcd (m , n)=gcd (n , r).
Describe Euclid’s algorithm in mathematics. • For example , let m = 20 and n = 8, then m = 2 × 8+ 4. therefore gcd(20, 8) = gcd(8, 4). • • • r 0=m – q 0 × n (0 ≤q 0, 0 <r 0 <n) r 1 = n – q 1 × r 0 (0 ≤q 1, 0 <r 1 <r 0) r 2 = r 0 – q 2 × r 1 (0 ≤q 2, 0 <r 2 <r 1) ………… ri = ri-2 – qi × ri-1 (0 ≤qi, 0 <ri-1)
Not an entirely evident translation of the mathematics • 1. Divide m by n , and obtain the remainder r. • 2. if r = 0, then halt. The answer is n. • 3. Replace m by n (m ←n) and n by r (n ← r). • 4. Go to step 1.
Flowchart –Euclid’s Algorithm shows instructions in flow chart form Mod : take the remainder on dividing m by n
Another flow chart which is Correctness of Euclid’s Algorithm For example M=20 and N=8, yields the command execution sequence : {A 0} C 0 {A 1} C 1 {A 2} C 2 {A 3} C 3 {A 5} C 4 {A 6} C 1 {A 2} C 2 {A 3} C 3 {A 4}
One Algolic program that directly follows the preceding descriptions for Euclid’s algorithm • • • 0 1 2 3 4 5 6 7 8 9 10 11 algorithm GCD 0; m , n , r : integer begin read ( m , n); write(‘ m =‘, m , ’n =‘, n); l: r: = m mod n; if r≠ 0 then begin m: = n ; n : = r; go to l end; write(‘ The gcd is ’, n) end.
Another way to express the same algorithm as a program in a given language • • • 0 algorithm GCD 1; 1 m , n , r : integer; 2 begin 3 read ( m , n ) ; write (‘ m ’ , m , ’ n= ’, n); 4 while n≠ 0 do begin { Perform loop as long as n ≠ 0. } 5 r: = m mod n; 6 m: = n ; n: = r 7 end ; { gcd is in m. } 8 write ( ‘The gcd is ’, m) 9 end.
A description that closely resembles the original mathematical basis of the algorithm • 0 function gcd ( m , n : integer ) : integer; • 1 gcd: =if n = 0 then m • 2 • else gcd ( n , m mod n); Recursive : invoke themselves.
Another way to solve the same problem • 1. Set g equal to n ( g ←n). • 2. If g divides m and n , then halt. G is the gcd. • 3. Decrement g by 1 (g ←g-1). • 4. Go to step 2.
Euclid’s algorithm is better than Simple. GCD • • • 0 algorithm Simple. GCD; 1 …… 2 g: =n; 3 l: if not (m mod g = 0) and (n mod g=0) then begin 4 {g is not a divisor of both m and n. } 5 g: =g-1; 6 go to l 7 end; 8 {g is the gcd. } 9 …. 10 end • • Efficient ( in time ) : Simple. GCD tests every integer between n and the gcd ( m , n ), but Euclid’s algorithm use the remainder. Complicated : Simple. GCD requiring two divisions ( m mod g and n mod g )for each candidate. And For example , gcd(20, 8) is obtained in two passes through the loop with Euclid’s algorithm , and five passes using Simple. GCD.
Thank you.
- Slides: 13