VelocerMở ứng dụng

Velocer › Thẻ ghi nhớ › University of Wisconsin–Madison › comp sci 240 midterm 2

comp sci 240 midterm 2

Induction, invariants, program correctness, recursion. proofs proofs proofs. · bởi sunset_point_notes · 149 thẻ

University of Wisconsin–Madison

Học bộ thẻ này Nhúng

  1. Mathematical inductionProves (∀n∈N)P(n) with a base case P(0) and a step showing P(n)→P(n+1).
  2. Induction ruleP(0) plus (∀n)(P(n)→P(n+1)) gives (∀n)P(n).
  3. Why does induction cover all naturals?It's a chain: 0 true → 1 true → 2 true → … reaches every natural.
  4. Induction hypothesisThe assumption P(n) you use to prove P(n+1) in the inductive step.
  5. Sum formula 1+2+…+nn(n+1)/2 for all n∈N.
  6. Base case for 1+2+…+n = n(n+1)/2Empty sum is 0, and 0(0+1)/2 = 0.
  7. Inductive step for 1+2+…+n = n(n+1)/2Add n+1 to both sides: n(n+1)/2 + (n+1) = (n+1)(n+2)/2.
  8. What if P(n) won't prove by induction?Try a stronger P′(n) that implies P(n); the stronger hypothesis gives more to work with.
  9. Strengthening the predicateProve (∀n)P′(n), then use P′(n)→P(n) to get (∀n)P(n).
  10. Grid tiling problemTile a 2ⁿ×2ⁿ grid with L-shapes (3 squares) leaving exactly one square uncovered.
  11. Why does naive grid tiling fail?Subgrid centers aren't whole-grid centers, so you get three stray holes.
  12. Fix for grid tilingGeneralize: any chosen square s can be the uncovered one.
  13. Grid tiling stepSplit into four subgrids; tile three leaving inner corners; one L-shape covers those three corners.
  14. Horses errorThe step breaks at n=0: two groups of size 1 share no horse, so colors can't be linked.
  15. Lesson from the horses errorCheck the smallest n in the step; overlapping notation can hide a gap.
  16. Induction for n≥mBase P(m), step (∀n≥m)(P(n)→P(n+1)) gives (∀n≥m)P(n).
  17. Stamp problemWith 4¢ and 7¢ stamps, every amount n≥18 is realizable.
  18. Stamp step when b>0Swap one 7 for two 4s: (a,b) becomes (a+2, b−1).
  19. Strong induction hypothesisAssume all m≤n satisfy P(m), not just n.
  20. How does strong induction work?You prove P(0). Then show: if P(0), P(1), ..., P(n) all hold, then P(n+1) holds too. That proves P(n) for every n.
  21. Strong induction as ordinary inductionApply ordinary induction to Q(n) = P(0)∧…∧P(n).
  22. What does strong induction with base case m prove?If P(m) holds, and for every n ≥ m, P(m) ∧ P(m+1) ∧ ... ∧ P(n) implies P(n+1), then P(n) holds for all n ≥ m.
  23. Prime factorization theoremEvery integer n≥2 is a product of primes.
  24. Base case for prime factorization2 is prime.
  25. Step for prime factorizationIf n+1 is composite, n+1=k·l with 1<k,l<n+1; strong hypothesis factors k and l.
  26. Why ordinary induction fails for prime factorizationThe hypothesis on n alone says nothing about k and l, which are smaller than n+1.
  27. Unstacking gameSplit a pile into two; score is the product of the two new sizes; total is the sum over all splits.
  28. Unstacking game total scoreIndependent of strategy; equals n(n−1)/2 for n starting boxes.
  29. Easy strategy for unstackingPeel one box at a time: score (n−1)+(n−2)+…+1.
  30. Base case for unstackingP(1): score 0 = 1·0/2.
  31. Unstacking stepFirst split into k and n+1−k, score k(n+1−k), then two independent games; sum equals (n+1)n/2.
  32. Inductive definitionTwo parts: foundation rule (simplest instances) and constructor rule (build complex from simpler).
  33. Foundation rule vs. base caseThe foundation rule in an inductive definition corresponds to the base case in induction.
  34. Constructor rule vs. inductive stepThe constructor rule in an inductive definition corresponds to the inductive step.
  35. Why does structural induction work?Every instance arises from some number of constructor-rule applications.
  36. vars(F)The number of variable occurrences in formula F.
  37. binops(F)The number of binary operator occurrences in formula F.
  38. vars = binops + 1 theoremIn every propositional formula, variable occurrences = binary operators + 1.
  39. Base case for vars = binops + 1Single variable: vars=1, binops=0.
  40. Case ¬F1 for vars = binops + 1Negation adds no vars and no binary ops, so vars and binops are unchanged.
  41. Recurrence relationDefines a term from earlier terms; it's an inductive definition in disguise.
  42. Fibonacci recurrenceF1=1, F2=1, Fn=Fn−1+Fn−2 for n≥3.
  43. First five Fibonacci terms1, 1, 2, 3, 5.
  44. Structural induction on recurrencesBase: first terms; step: assume property for first n terms, use recurrence for term n+1.
  45. Golden ratio φ(1+√5)/2 ≈ 1.62.
  46. Binet's formula for FibonacciFn = (φⁿ − (1−φ)ⁿ)/√5.
  47. Approximation of Fn for large nFn ≈ φⁿ because (1−φ)ⁿ becomes negligible.
  48. Common mix-up: combining operatorDon't forget the extra +1 binary op from the operator that combines two formulas.
  49. Common mix-up: negationNegation is unary, so it adds no binary operator.
  50. Common mix-up: vars countsvars counts occurrences, not distinct variables.
  51. InvariantA property that stays true after every step of a discrete system.
  52. How do you prove a property holds after every step?By induction on the step number t.
  53. Induction recipe for invariantsShow it holds at t=0, then show it holding at t forces it at t+1.
  54. Predicate P(t) for an invariantP(t) means the property holds after t steps.
  55. Why are invariants useful in programs?They let you prove program correctness.
  56. Is finding the right invariant usually easy?No, it's often hard, and it may capture only what you need.
  57. Robot on a grid: what moves are allowed?One square diagonally: (x±1, y±1).
  58. Robot starting at (0,0) with diagonal moves: invariant?x + y is always even.
  59. Base case for the robot's x + y invariantAt (0,0), 0 + 0 = 0, which is even.
  60. How much can x + y change in one diagonal move?By 0, +2, or −2, so its parity never changes.
  61. Can the robot reach (8,9)?No: 8 + 9 = 17 is odd, so it's unreachable.
  62. When is a grid point reachable by the diagonal robot?Exactly when x + y is even.
  63. 8-puzzle moveSwap the blank with an adjacent tile, in its row or column.
  64. Error pair in the 8-puzzlePositions i < j where the letter at j comes earlier alphabetically than the one at i.
  65. Error count of the 8-puzzle goal0, since A…H are in order with the blank last.
  66. Effect of a row move on the 8-puzzle error countNone: the blank swaps with an adjacent tile, so no two letters change relative order.
  67. Effect of a column move on the error countOnly the blank's order versus two letters can flip, so the change is −2, 0, or +2.
  68. What is invariant in the 8-puzzle?The parity of the error count, odd or even.
  69. ParityWhether a number is odd or even.
  70. Do 6 and 8 have the same parity?Yes, both are even.
  71. Do 7 and 156 have the same parity?No: 7 is odd and 156 is even.
  72. Why is the 8-puzzle start arrangement unreachable from the goal?The start has 1 error (odd) and the goal has 0 (even), so parity differs.
  73. How is the 8-puzzle parity invariant proved?By induction on the number of moves n.
  74. Does an invariant fully describe the reachable states?No, it may only capture part of the system.
  75. Does a row move ever change the 8-puzzle error count?No, it changes it by 0.
  76. Can a column move change the 8-puzzle error count by an odd amount?No, only by an even amount.
  77. What is preserved in the 8-puzzle: the error count or its parity?The parity, not the error count itself.
  78. Program correctnessFor every valid input, the program's output matches the specified input→output relation.
  79. Two parts of program correctnessPartial correctness (if it returns, the result is right) plus termination (it does return).
  80. Why prove termination separately from partial correctness?Partial correctness alone still allows a program to loop forever.
  81. Partial correctnessIf the program returns, its result is correct.
  82. TerminationThe program actually returns on every valid input.
  83. How do you prove a loop invariant?By induction: base case is after initialization; inductive step is one loop iteration.
  84. If a loop body has an if, how does the inductive step change?It splits into separate cases, one per branch.
  85. Multiplication invariantAfter n iterations, ab = xₙyₙ + pₙ.
  86. Multiplication base casex₀=a, y₀=b, p₀=0, so ab = ab + 0 holds.
  87. Multiplication odd case (y odd)pₙ₊₁ = pₙ + xₙ, yₙ₊₁ = (yₙ−1)/2, xₙ₊₁ = 2xₙ; invariant is preserved.
  88. Multiplication even case (y even)p unchanged, yₙ₊₁ = yₙ/2, xₙ₊₁ = 2xₙ; invariant is preserved.
  89. Second invariant in the multiplication algorithmy ≥ 0 after every iteration, since the floor of a non-negative can't go negative.
  90. How does the multiplication loop end with p = ab?It exits at y ≤ 0; with y ≥ 0 that means y = 0, so ab = 0 + p.
  91. Termination bound for the multiplication loopIf y > 0 then ⌊y/2⌋ ≤ y − 1, so y drops by at least 1 each iteration.
  92. gcd(a,b)The largest integer d that divides both a and b.
  93. If c divides both a and b, what else does c divide?c divides gcd(a,b).
  94. gcd(a,0) for a > 0a.
  95. gcd(0,0)Undefined.
  96. Does every integer divide 0?Yes.
  97. gcd(a,b) when a = bgcd(a,b) = a.
  98. How do you simplify gcd(a,b) if a is smaller than b?Subtract the smaller from the bigger: gcd(a,b) = gcd(a, b−a). Example: gcd(4,10) = gcd(4,6) = gcd(4,2) = 2.
  99. How do you simplify gcd(a,b) if a is bigger than b?You can subtract: gcd(a,b) = gcd(a−b, b). Example: gcd(12,8) = gcd(4,8) = 4.
  100. Why does subtracting the smaller from the larger keep the gcd?d divides a and b exactly when d divides a and b−a, so the divisor sets match.
  101. Subtractive GCD algorithm(x,y)←(a,b); while x≠y: if x<y then y←y−x else x←x−y; return x.
  102. GCD loop invariantgcd(a,b) = gcd(x,y) after n iterations.
  103. How does the GCD loop return the right answer?On exit x = y, so gcd(x,y) = x, which equals gcd(a,b).
  104. Positivity invariant in the GCD algorithmx > 0 and y > 0 always, which needs a,b > 0 as the base case.
  105. Termination potential for the GCD algorithmx + y decreases by at least 1 each iteration, since you subtract the smaller from the larger.
  106. Iteration bound for the GCD algorithmAt most a + b − 2 iterations, since x + y ≥ 2 until x = y = 1.
  107. Why isn't the gcd invariant enough to prove GCD termination?You also need the positivity invariant to bound x + y below.
  108. What happens to the GCD algorithm if a = 0 is allowed?x = 0, y = 1 gives y←y−0 forever, an infinite loop.
  109. What does a specification fix besides the input→output relation?The valid input domain; correctness is only required there.
  110. RecursionSolving a problem by reducing it to easier instances of the same problem.
  111. Recursive callWhen a program calls itself on a smaller input.
  112. What must happen to the input in each recursive call?It must shrink so you eventually hit a trivial case.
  113. Trivial caseThe simple case answered directly; it ends the recursion.
  114. Which call returns first in recursion?The last call made returns first.
  115. What happens if the input never shrinks?Infinite self-calls; the program never terminates.
  116. Fibonacci constructor ruleF_n = F_(n−1) + F_(n−2)
  117. Base cases for FibonacciF₁ = 1 and F₂ = 1
  118. Recursive Fibonacci algorithmBase case: if n is 1 or 2, return 1. Otherwise, return fib(n−1) + fib(n−2). You call the function twice and add the results.
  119. Input and output of Fib(n)Input: positive integer n; output: F_n.
  120. Recursive GCD when a = bReturn a.
  121. Identity behind recursive GCDgcd(a, b) = gcd(a, b−a)
  122. Tail recursionThe recursive call is the last action and its value is returned directly.
  123. Write b as 2q + r: what are q and r?q = ⌊b/2⌋ and r is the last bit, 0 or 1.
  124. Multiplying by 2 in binaryAppend a 0.
  125. Why is ar trivial in grade school multiplication?r = 0 gives 0; r = 1 gives a, so no recursive call.
  126. Recursive MULT(a,b) when b = 0Return 0.
  127. Recursive MULT(a,b) when b is even2 · MULT(a, ⌊b/2⌋)
  128. Recursive MULT(a,b) when b is odd2 · MULT(a, ⌊b/2⌋) + a
  129. What does it mean for a recursive program to be correct?For all valid inputs, it produces the correct output.
  130. Two parts of recursive program correctnessPartial correctness and termination.
  131. To show partial correctness, what must hold for every valid x?Each recursive call is on a valid input, and assuming correct returns, the program returns correct output.
  132. How does the correctness proof structure mirror the program?Base/simplest inputs first, then the inductive step.
  133. How do you prove termination of a recursive program?By induction on a quantity that decreases with each call.
  134. GCD correctness when a < b: why is a, b−a a valid input?Both are positive, so the recursive call gets a valid input.
  135. Termination measure for recursive GCDInduct on a+b; each call lowers a+b by at least 1.
  136. Minimum value of a+b in recursive GCD2, when a = b = 1.
  137. MULT correctness when b is evenab = 2 · (a · ⌊b/2⌋)
  138. MULT correctness when b is oddab = 2 · (a · ⌊b/2⌋) + a
  139. Termination measure for MULTStrong induction on b, since ⌊(b+1)/2⌋ ≤ b.
  140. Towers of Hanoi setup3 pegs A, B, C; n disks stacked on A, largest at bottom.
  141. Goal of Towers of HanoiMove all disks from A to C.
  142. Rules of Towers of HanoiOne disk per move; never place a larger disk on a smaller one.
  143. Towers of Hanoi strategyMove top n−1 disks A→B using C, move largest A→C, move n−1 disks B→C using A.
  144. TOWERS(n,A,B,C) when n = 0Return immediately.
  145. How do you break down TOWERS(n,A,B,C) into recursive steps?First call TOWERS(n−1,A,C,B). Then move the top disk from A to C. Finally call TOWERS(n−1,B,A,C).
  146. Why are Towers of Hanoi moves legal during the subproblem?The largest disk stays put, so subproblem moves never violate the rule.
  147. Termination proof for Towers of HanoiInduction on the number of disks.
  148. Does partial correctness imply halting?No; a program can loop forever yet still be partially correct.
  149. What does termination require beyond input looking smaller?A decreasing measure that provably reaches a base case.

Velocer miễn phí: ôn tập ngắt quãng và thẻ của riêng bạn từ bất kỳ tài liệu nào.

PDF to flashcards · Lecture to flashcards · Practice exam · Syllabus study plan · Anki import

Thêm bộ thẻ của University of Wisconsin–Madison