Velocer › フラッシュカード › University of Wisconsin–Madison › comp sci 240 midterm 2
comp sci 240 midterm 2
Induction, invariants, program correctness, recursion. proofs proofs proofs. · 作成者: sunset_point_notes · カード 149 枚
- Mathematical inductionProves (∀n∈N)P(n) with a base case P(0) and a step showing P(n)→P(n+1).
- Induction ruleP(0) plus (∀n)(P(n)→P(n+1)) gives (∀n)P(n).
- Why does induction cover all naturals?It's a chain: 0 true → 1 true → 2 true → … reaches every natural.
- Induction hypothesisThe assumption P(n) you use to prove P(n+1) in the inductive step.
- Sum formula 1+2+…+nn(n+1)/2 for all n∈N.
- Base case for 1+2+…+n = n(n+1)/2Empty sum is 0, and 0(0+1)/2 = 0.
- 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.
- 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.
- Strengthening the predicateProve (∀n)P′(n), then use P′(n)→P(n) to get (∀n)P(n).
- Grid tiling problemTile a 2ⁿ×2ⁿ grid with L-shapes (3 squares) leaving exactly one square uncovered.
- Why does naive grid tiling fail?Subgrid centers aren't whole-grid centers, so you get three stray holes.
- Fix for grid tilingGeneralize: any chosen square s can be the uncovered one.
- Grid tiling stepSplit into four subgrids; tile three leaving inner corners; one L-shape covers those three corners.
- Horses errorThe step breaks at n=0: two groups of size 1 share no horse, so colors can't be linked.
- Lesson from the horses errorCheck the smallest n in the step; overlapping notation can hide a gap.
- Induction for n≥mBase P(m), step (∀n≥m)(P(n)→P(n+1)) gives (∀n≥m)P(n).
- Stamp problemWith 4¢ and 7¢ stamps, every amount n≥18 is realizable.
- Stamp step when b>0Swap one 7 for two 4s: (a,b) becomes (a+2, b−1).
- Strong induction hypothesisAssume all m≤n satisfy P(m), not just n.
- 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.
- Strong induction as ordinary inductionApply ordinary induction to Q(n) = P(0)∧…∧P(n).
- 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.
- Prime factorization theoremEvery integer n≥2 is a product of primes.
- Base case for prime factorization2 is prime.
- Step for prime factorizationIf n+1 is composite, n+1=k·l with 1<k,l<n+1; strong hypothesis factors k and l.
- Why ordinary induction fails for prime factorizationThe hypothesis on n alone says nothing about k and l, which are smaller than n+1.
- Unstacking gameSplit a pile into two; score is the product of the two new sizes; total is the sum over all splits.
- Unstacking game total scoreIndependent of strategy; equals n(n−1)/2 for n starting boxes.
- Easy strategy for unstackingPeel one box at a time: score (n−1)+(n−2)+…+1.
- Base case for unstackingP(1): score 0 = 1·0/2.
- Unstacking stepFirst split into k and n+1−k, score k(n+1−k), then two independent games; sum equals (n+1)n/2.
- Inductive definitionTwo parts: foundation rule (simplest instances) and constructor rule (build complex from simpler).
- Foundation rule vs. base caseThe foundation rule in an inductive definition corresponds to the base case in induction.
- Constructor rule vs. inductive stepThe constructor rule in an inductive definition corresponds to the inductive step.
- Why does structural induction work?Every instance arises from some number of constructor-rule applications.
- vars(F)The number of variable occurrences in formula F.
- binops(F)The number of binary operator occurrences in formula F.
- vars = binops + 1 theoremIn every propositional formula, variable occurrences = binary operators + 1.
- Base case for vars = binops + 1Single variable: vars=1, binops=0.
- Case ¬F1 for vars = binops + 1Negation adds no vars and no binary ops, so vars and binops are unchanged.
- Recurrence relationDefines a term from earlier terms; it's an inductive definition in disguise.
- Fibonacci recurrenceF1=1, F2=1, Fn=Fn−1+Fn−2 for n≥3.
- First five Fibonacci terms1, 1, 2, 3, 5.
- Structural induction on recurrencesBase: first terms; step: assume property for first n terms, use recurrence for term n+1.
- Golden ratio φ(1+√5)/2 ≈ 1.62.
- Binet's formula for FibonacciFn = (φⁿ − (1−φ)ⁿ)/√5.
- Approximation of Fn for large nFn ≈ φⁿ because (1−φ)ⁿ becomes negligible.
- Common mix-up: combining operatorDon't forget the extra +1 binary op from the operator that combines two formulas.
- Common mix-up: negationNegation is unary, so it adds no binary operator.
- Common mix-up: vars countsvars counts occurrences, not distinct variables.
- InvariantA property that stays true after every step of a discrete system.
- How do you prove a property holds after every step?By induction on the step number t.
- Induction recipe for invariantsShow it holds at t=0, then show it holding at t forces it at t+1.
- Predicate P(t) for an invariantP(t) means the property holds after t steps.
- Why are invariants useful in programs?They let you prove program correctness.
- Is finding the right invariant usually easy?No, it's often hard, and it may capture only what you need.
- Robot on a grid: what moves are allowed?One square diagonally: (x±1, y±1).
- Robot starting at (0,0) with diagonal moves: invariant?x + y is always even.
- Base case for the robot's x + y invariantAt (0,0), 0 + 0 = 0, which is even.
- How much can x + y change in one diagonal move?By 0, +2, or −2, so its parity never changes.
- Can the robot reach (8,9)?No: 8 + 9 = 17 is odd, so it's unreachable.
- When is a grid point reachable by the diagonal robot?Exactly when x + y is even.
- 8-puzzle moveSwap the blank with an adjacent tile, in its row or column.
- Error pair in the 8-puzzlePositions i < j where the letter at j comes earlier alphabetically than the one at i.
- Error count of the 8-puzzle goal0, since A…H are in order with the blank last.
- 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.
- 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.
- What is invariant in the 8-puzzle?The parity of the error count, odd or even.
- ParityWhether a number is odd or even.
- Do 6 and 8 have the same parity?Yes, both are even.
- Do 7 and 156 have the same parity?No: 7 is odd and 156 is even.
- 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.
- How is the 8-puzzle parity invariant proved?By induction on the number of moves n.
- Does an invariant fully describe the reachable states?No, it may only capture part of the system.
- Does a row move ever change the 8-puzzle error count?No, it changes it by 0.
- Can a column move change the 8-puzzle error count by an odd amount?No, only by an even amount.
- What is preserved in the 8-puzzle: the error count or its parity?The parity, not the error count itself.
- Program correctnessFor every valid input, the program's output matches the specified input→output relation.
- Two parts of program correctnessPartial correctness (if it returns, the result is right) plus termination (it does return).
- Why prove termination separately from partial correctness?Partial correctness alone still allows a program to loop forever.
- Partial correctnessIf the program returns, its result is correct.
- TerminationThe program actually returns on every valid input.
- How do you prove a loop invariant?By induction: base case is after initialization; inductive step is one loop iteration.
- If a loop body has an if, how does the inductive step change?It splits into separate cases, one per branch.
- Multiplication invariantAfter n iterations, ab = xₙyₙ + pₙ.
- Multiplication base casex₀=a, y₀=b, p₀=0, so ab = ab + 0 holds.
- Multiplication odd case (y odd)pₙ₊₁ = pₙ + xₙ, yₙ₊₁ = (yₙ−1)/2, xₙ₊₁ = 2xₙ; invariant is preserved.
- Multiplication even case (y even)p unchanged, yₙ₊₁ = yₙ/2, xₙ₊₁ = 2xₙ; invariant is preserved.
- Second invariant in the multiplication algorithmy ≥ 0 after every iteration, since the floor of a non-negative can't go negative.
- 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.
- Termination bound for the multiplication loopIf y > 0 then ⌊y/2⌋ ≤ y − 1, so y drops by at least 1 each iteration.
- gcd(a,b)The largest integer d that divides both a and b.
- If c divides both a and b, what else does c divide?c divides gcd(a,b).
- gcd(a,0) for a > 0a.
- gcd(0,0)Undefined.
- Does every integer divide 0?Yes.
- gcd(a,b) when a = bgcd(a,b) = a.
- 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.
- 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.
- 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.
- Subtractive GCD algorithm(x,y)←(a,b); while x≠y: if x<y then y←y−x else x←x−y; return x.
- GCD loop invariantgcd(a,b) = gcd(x,y) after n iterations.
- How does the GCD loop return the right answer?On exit x = y, so gcd(x,y) = x, which equals gcd(a,b).
- Positivity invariant in the GCD algorithmx > 0 and y > 0 always, which needs a,b > 0 as the base case.
- Termination potential for the GCD algorithmx + y decreases by at least 1 each iteration, since you subtract the smaller from the larger.
- Iteration bound for the GCD algorithmAt most a + b − 2 iterations, since x + y ≥ 2 until x = y = 1.
- Why isn't the gcd invariant enough to prove GCD termination?You also need the positivity invariant to bound x + y below.
- What happens to the GCD algorithm if a = 0 is allowed?x = 0, y = 1 gives y←y−0 forever, an infinite loop.
- What does a specification fix besides the input→output relation?The valid input domain; correctness is only required there.
- RecursionSolving a problem by reducing it to easier instances of the same problem.
- Recursive callWhen a program calls itself on a smaller input.
- What must happen to the input in each recursive call?It must shrink so you eventually hit a trivial case.
- Trivial caseThe simple case answered directly; it ends the recursion.
- Which call returns first in recursion?The last call made returns first.
- What happens if the input never shrinks?Infinite self-calls; the program never terminates.
- Fibonacci constructor ruleF_n = F_(n−1) + F_(n−2)
- Base cases for FibonacciF₁ = 1 and F₂ = 1
- 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.
- Input and output of Fib(n)Input: positive integer n; output: F_n.
- Recursive GCD when a = bReturn a.
- Identity behind recursive GCDgcd(a, b) = gcd(a, b−a)
- Tail recursionThe recursive call is the last action and its value is returned directly.
- Write b as 2q + r: what are q and r?q = ⌊b/2⌋ and r is the last bit, 0 or 1.
- Multiplying by 2 in binaryAppend a 0.
- Why is ar trivial in grade school multiplication?r = 0 gives 0; r = 1 gives a, so no recursive call.
- Recursive MULT(a,b) when b = 0Return 0.
- Recursive MULT(a,b) when b is even2 · MULT(a, ⌊b/2⌋)
- Recursive MULT(a,b) when b is odd2 · MULT(a, ⌊b/2⌋) + a
- What does it mean for a recursive program to be correct?For all valid inputs, it produces the correct output.
- Two parts of recursive program correctnessPartial correctness and termination.
- 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.
- How does the correctness proof structure mirror the program?Base/simplest inputs first, then the inductive step.
- How do you prove termination of a recursive program?By induction on a quantity that decreases with each call.
- GCD correctness when a < b: why is a, b−a a valid input?Both are positive, so the recursive call gets a valid input.
- Termination measure for recursive GCDInduct on a+b; each call lowers a+b by at least 1.
- Minimum value of a+b in recursive GCD2, when a = b = 1.
- MULT correctness when b is evenab = 2 · (a · ⌊b/2⌋)
- MULT correctness when b is oddab = 2 · (a · ⌊b/2⌋) + a
- Termination measure for MULTStrong induction on b, since ⌊(b+1)/2⌋ ≤ b.
- Towers of Hanoi setup3 pegs A, B, C; n disks stacked on A, largest at bottom.
- Goal of Towers of HanoiMove all disks from A to C.
- Rules of Towers of HanoiOne disk per move; never place a larger disk on a smaller one.
- Towers of Hanoi strategyMove top n−1 disks A→B using C, move largest A→C, move n−1 disks B→C using A.
- TOWERS(n,A,B,C) when n = 0Return immediately.
- 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).
- Why are Towers of Hanoi moves legal during the subproblem?The largest disk stays put, so subproblem moves never violate the rule.
- Termination proof for Towers of HanoiInduction on the number of disks.
- Does partial correctness imply halting?No; a program can loop forever yet still be partially correct.
- What does termination require beyond input looking smaller?A decreasing measure that provably reaches a base case.
Velocerは無料です。間隔反復と、あらゆる文書から作成できる自分だけのカードをご利用いただけます。
PDF to flashcards · Lecture to flashcards · Practice exam · Syllabus study plan · Anki import
University of Wisconsin–Madisonのほかのデッキ
- comp sci 240 midterm 1
- COMP SCI 240 Midterm 3
- COMP SCI 240 Final
- comp sci 300 full course review
- CS 639: Introduction to Reinforcement Learning · UW-Madison
- CS 839: Reinforcement Learning · UW-Madison
- CS 544: Intro to Big Data Systems · UW-Madison
- CS 580: Intelligent Robotics · UW-Madison
- CS 784: Foundations of Data Management · UW-Madison
- CS 880: Quantum Algorithms · UW-Madison
- CS 765: Data Visualization · UW-Madison
- CS 639: AI Coding · UW-Madison