Discrrete mathematics for computer science invariant

9 26 0
Discrrete mathematics for computer science invariant

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

Thông tin tài liệu

States and Invariants 3/22/19 State Space • A state space is just a binary relation on a set S (the set of “states”) – (Meyer calls this a “state machine”) • E.g S = 3x3 arrangements of X, O, blank • a→b, where a, b ∈S, iff a move from state a can result Xin state b X X O 3/22/19 → X O X → O O Moving from state to state • Many computational problems can be described in terms of the states that are reachable from a state after a series of moves X • In tic-tac-toe, is O XX reachable from state 3/22/19 ? Invariant • In tic-tac-toe, is from state O X X X reachable ? No because tic-tac-toe preserves an invariant property of states: #X=#O or #X=#O+1 3/22/19 Invariant Tic-tac-toe preserves an invariant property of states: #X=#O or #X=#O+1 #X = #O = 0: Initial state X always goes first a→b: state b is a successor of state a if #X=#O in a, then #X=#O+1 in b if #X=#O+1 in a, then #X=#O in b X Therefore is unreachable O 3/22/19 X X Floyd’s Invariant Principle Preserved Invariant, P(state): if P(q) and q r ,then P(r) • Conclusion: if P(start), then P(r) for all reachable states r, including final state (if any) 3/22/19 Computing Factorials n! = n∙(n-1)∙…∙2∙1 Let b:=1, a:=n If a=1 then return b b := b∙a a:=a-1 Go to step • To prove Correctness (if it computes anything it computes n!): – Let state set S = N×N×N: the values of (a, b, n) – (a, b, n) → (a-1, a∙b, n) in each iteration of loop steps 2-5 – Let P(a, b, n) ≡ “a! ∙ b = n!” – Show • P(n, 1, n) • P(a, b, n) ⇒P(a-1, a∙b, n), because a! ∙ b = n ⇒ (a-1)! ∙ (a∙b) = n! • So the loop preserves the invariant P(a, b, n) – When & if a=1, since P(1, b, n) is true, b must be n! 3/22/19 Computing Factorials n! = n∙(n-1)∙…∙2∙1 Let b:=1, a:=n If a=1 then return b b := b∙a a:=a-1 Go to step • If the program computes anything it computes n! • But still must show Termination: – a initially is n and decreases by on each iteration and therefore must eventually reach – PROVIDED n≥1 !! 3/22/19 FINIS 3/22/19 ... ? Invariant • In tic-tac-toe, is from state O X X X reachable ? No because tic-tac-toe preserves an invariant property of states: #X=#O or #X=#O+1 3/22/19 Invariant Tic-tac-toe preserves an invariant. .. #X=#O in b X Therefore is unreachable O 3/22/19 X X Floyd’s Invariant Principle Preserved Invariant, P(state): if P(q) and q r ,then P(r) • Conclusion: if P(start), then P(r) for all reachable... P(a, b, n) ⇒P(a-1, a∙b, n), because a! ∙ b = n ⇒ (a-1)! ∙ (a∙b) = n! • So the loop preserves the invariant P(a, b, n) – When & if a=1, since P(1, b, n) is true, b must be n! 3/22/19 Computing Factorials

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

Mục lục

    Moving from state to state

    Floyd’s Invariant Principle

    Computing Factorials n! = n∙(n-1)∙…∙2∙1

    Computing Factorials n! = n∙(n-1)∙…∙2∙1

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan