1. Trang chủ
  2. » Giáo án - Bài giảng

Discrrete mathematics for computer science invariant warmup

9 78 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 9
Dung lượng 135,98 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

GCD correctnessEuclidean Algorithm for greatest common divisor of a,b 1... GCD Termination•y decreases at each step •y another invariant •Well Ordering implies reaches minimum & st

Trang 1

Invariant Warmup

Trang 2

GCD correctness

Euclidean Algorithm for greatest

common divisor of a,b

1 x := a, y := b

2 If y = 0, return x & terminate;

3 else (x, y) := (y, rem(x,y))

simultaneously;

4 Go to step 2.

Trang 3

GCD correctness

Example: GCD(662,414)

= GCD(414, 248) since rem(662,414) = 248

= GCD(248, 166) since rem(414,248) = 166

= GCD(166, 82) since rem(248,166) = 82

= GCD(82, 2) since rem(166,82) = 2

Trang 4

GCD correctness

• Euclid Algorithm as State Machine:

• States ::=

• start ::= (a,b)

• state transitions defined by

• (x,y) (y, rem(x,y)) for y ≠ 0

Trang 5

GCD correctness

preserved invariant is

P((x,y)) ::= [gcd(a,b) = gcd(x,y)]

P(start): at start x = a , y = b,

so

P(start) ≡ [gcd(a,b) =

gcd(a,b)]

which holds trivially

Trang 6

transitions: (x, y) → (y, rem(x,

y))

any divisor 2 of these 3 terms

divides all 3.

P is preserved because:

for y ≠ 0

GCD correctness

Trang 7

GCD correctness

Conclusion: on termination

x = gcd(a,b)

Proof: at termination, y = 0,

so

x = gcd(x,0) = gcd(x,y) = gcd(a,b)

preserved invariant

Trang 8

GCD Termination

•y decreases at each

step

•y (another

invariant)

•Well Ordering implies reaches minimum &

stops

Trang 9

Team Problems

Ngày đăng: 22/03/2019, 11:38