logik zusammenfassung, Zusammenfassungen von Logik

Toggle sidebar
bg1
COMP1002 exam study sheet
Propositional logic:
•Propositional statement: expression that has a truth value (true/false). It is a tautology if it is always
true, contradiction if always false.
•Logic connectives: negation (“not”) ¬p, conjunction (“and”) p∧q, disjunction (“or”) p∨q, implication
p→q(equivalent to ¬p∨q), biconditional p↔q(equivalent to (p→q)∧(q→p)). The order of
precedence: ¬strongest, ∧next, ∨next, →and ↔the same, weakest.
•If p→qis an implication, then ¬q→ ¬pis its contrapositive,q→paconverse and ¬p→ ¬q
an inverse. An implication is equivalent to its contrapositive, but not to converse/inverse or their
negations. A negation of an implication p→qis p∧ ¬q(it is not an implication itself!)
•Atruth assignment is a string of values of variables to the formula, usually a row with values of first
several columns in the truth table (number of columns = number of variables). A truth assignment
is satisfying the formula if the value of the formula on these variables is T, otherwise the truth
assignment is falsifying. A formula is satisfiable if it has a satisfying assignment, otherwise it is
unsatisfiable (a contradiction). A truth assignment can be encoded by a formula that is a ∧of
variables and their negations, with negated variables in places that have F (false) in the assignment,
and non-negated that have T (true). For example, x=T, y =F, z =Fis encoded as (x∧ ¬y∧ ¬z).It
is an encoding in a sense that this formula is true only on this truth assignment and nowhere else.
•Atruth table has a line for each possible values of propositional variables (2klines if there are
kvariables), and a column for each variable and subformula, up to the whole statement.Its cells
contain Tand Fdepending whether the (sub)formula is true for the corresponding scenarios.
•Finding a method for checking if a formula has a satisfying assignment that is always significantly
faster than using truth tables (that is, better than brute-force search) is a one of Clay Mathematics
Institute $1,000,000 prize problems, known as ”P vs. NP”.
•Two formulas are logically equivalent, written A≡B, if they have the same truth value in all scenarios
(truth assignments). A≡Bif and only if A↔Bis a tautology.
•There are several other important pairs of logically equivalent formulas, called logical identities or
logic laws. We will talk more about them when we talk about Boolean algebras. The most famous
example of logically equivalent formulas is ¬(p∨q)≡(¬p∧ ¬q) (with a dual version ¬(p∧q)≡
(¬p∨ ¬q)) where pand qcan be arbitrary (propositional, here) formulas. These pairs of logically
equivalent formulas are called DeMorgan’s law. Here, remember that F ALSE ∧p≡p∧¬p≡F ALSE ,
F ALSE ∨p≡TRU E ∧p≡pand T RU E ∨p≡p∨ ¬p≡TRU E .
•A set of logic connectives is called complete if it is possible to make a formula with any truth table
out of these connectives. For example, ¬,∧is a complete set of connectives, and so is the Sheffer’s
stroke |(where p|q≡ ¬(p∧q)), also called NAND for “not-and”. But ∨,∧is not a complete set of
connectives since then it is impossible to express a truth table line with a 0 when all variables are 1.
•An argument consists of several formulas called premises and a final formula called a conclusion.
If we call premises A1. . . Anand conclusion B, then an argument is valid iff premises imply the
conclusion, that is, A1∧ · · · ∧ An→B. We usually write them in the following format:
1
pf3
pf4
pf5
pf8
pf9
pfa

Unvollständige Textvorschau

Nur auf Docsity: Lade logik zusammenfassung und mehr Zusammenfassungen als PDF für Logik herunter!

COMP1002 exam study sheet

Propositional logic:

  • Propositional statement: expression that has a truth value (true/false). It is a tautology if it is always true, contradiction if always false.
  • Logic connectives: negation (“not”) ¬p, conjunction (“and”) p∧q, disjunction (“or”) p∨q, implication p → q (equivalent to ¬p ∨ q), biconditional p ↔ q (equivalent to (p → q) ∧ (q → p)). The order of precedence: ¬ strongest, ∧ next, ∨ next, → and ↔ the same, weakest.
  • If p → q is an implication, then ¬q → ¬p is its contrapositive, q → p a converse and ¬p → ¬q an inverse. An implication is equivalent to its contrapositive, but not to converse/inverse or their negations. A negation of an implication p → q is p ∧ ¬q (it is not an implication itself!)
  • A truth assignment is a string of values of variables to the formula, usually a row with values of first several columns in the truth table (number of columns = number of variables). A truth assignment is satisfying the formula if the value of the formula on these variables is T, otherwise the truth assignment is falsifying. A formula is satisfiable if it has a satisfying assignment, otherwise it is unsatisfiable (a contradiction). A truth assignment can be encoded by a formula that is a ∧ of variables and their negations, with negated variables in places that have F (false) in the assignment, and non-negated that have T (true). For example, x = T, y = F, z = F is encoded as (x ∧ ¬y ∧ ¬z).It is an encoding in a sense that this formula is true only on this truth assignment and nowhere else.
  • A truth table has a line for each possible values of propositional variables (2k^ lines if there are k variables), and a column for each variable and subformula, up to the whole statement.Its cells contain T and F depending whether the (sub)formula is true for the corresponding scenarios.
  • Finding a method for checking if a formula has a satisfying assignment that is always significantly faster than using truth tables (that is, better than brute-force search) is a one of Clay Mathematics Institute $1,000,000 prize problems, known as ”P vs. NP”.
  • Two formulas are logically equivalent, written A ≡ B, if they have the same truth value in all scenarios (truth assignments). A ≡ B if and only if A ↔ B is a tautology.
  • There are several other important pairs of logically equivalent formulas, called logical identities or logic laws. We will talk more about them when we talk about Boolean algebras. The most famous example of logically equivalent formulas is ¬(p ∨ q) ≡ (¬p ∧ ¬q) (with a dual version ¬(p ∧ q) ≡ (¬p ∨ ¬q)) where p and q can be arbitrary (propositional, here) formulas. These pairs of logically equivalent formulas are called DeMorgan’s law. Here, remember that F ALSE∧p ≡ p∧¬p ≡ F ALSE, F ALSE ∨ p ≡ T RU E ∧ p ≡ p and T RU E ∨ p ≡ p ∨ ¬p ≡ T RU E.
  • A set of logic connectives is called complete if it is possible to make a formula with any truth table out of these connectives. For example, ¬, ∧ is a complete set of connectives, and so is the Sheffer’s stroke | (where p|q ≡ ¬(p ∧ q)), also called NAND for “not-and”. But ∨, ∧ is not a complete set of connectives since then it is impossible to express a truth table line with a 0 when all variables are 1.
  • An argument consists of several formulas called premises and a final formula called a conclusion. If we call premises A 1... An and conclusion B, then an argument is valid iff premises imply the conclusion, that is, A 1 ∧ · · · ∧ An → B. We usually write them in the following format:

Today is either Thursday or Friday On Thursdays I have to go to a lecture Today is not Friday (alternatively, On Friday I have to go to the lecture) —————————————————– ∴ I have to go to a lecture today

  • A valid form of argument is called rule of inference. The most prominent such rule is called modus ponens.

p → q p ————– ∴ q

  • We studied three methods for proving that a formula is a tautology: truth tables, natural deduction and resolution (where resolution proves that a formula is a tautology by proving that its negation is a contradiction).
  • A natural deduction proof consists of a sequence of applications of modus ponens (and other rules of inference) until a desired conclusion is reached, or there is nothing new left to derive. Example: treasure hunt, where ”desired conclusion” is a statement that the treasure is in a specific location.
  • There are two main normal forms for the propositional formulas. One is called Conjunctive normal form (CNF, also known as Product-of-Sums) and is an ∧ of ∨ of either variables or their negations (here, by ∧ and ∨ we mean several formulas with ∧ between each pair, as in (¬x ∨ y ∨ z) ∧ (¬u ∨ y) ∧ x. A literal is a variable or its negation (x or ¬x, for example). A ∨ of (possibly more than 2) literals is called a clause, for example (¬u ∨ z ∨ x), so a CNF is true for some truth assignment whenever this assignment makes each of the clauses is true, that is, each clause has a literal that evaluates to true under this assignment. A Disjunctive normal form (DNF, Sum-of-Products) is like CNF except the roles of ∧ and ∨ are reversed. A ∧ of literals in a DNF is called a term. To construct canonical DNF and a CNF, start from a truth table and then for every satisfying truth assignment ∨ its encoding to a DNF, and for every falsifying truth assignment ∧ the negation of its encoding to the CNF, and apply DeMorgan’s law. This may result in a very large CNFs and DNFs, comparable to the size of the truth table itself (2number of variables).
  • A resolution proof system is used to find a contradiction in a formula (and, similarly, to prove that a formula is a tautology by finding a contradiction in its negation). Resolution starts with a formula in a CNF form, and applies the rule “from clause (C ∨ x) and clause (D ∨ ¬x) derive clause (C ∨ D) until a falsity F (equivalently, empty clause () ) is reached (so in the last step one of the clauses being resolved contains just one variable and another clause being resolved contains just that variable’s negation.) Note that if a clause has opposing literals (e.g., from resolving (x ∨ y) with (¬x ∨ ¬y) then it evaluates to true, and so is useless for deriving a contradiction. Resolution can be used to check the validity of an argument by running it on the ∧ of all premises (converted, each, to a CNF) ∧ together with the negation of the conclusion.
  • Pigeonhole principle If n pigeons sit in n − 1 holes, so that each pigeon sits in some hole, then some hole has at least two pigeons. There is no small resolution proof of the pigeonhole principle.
  • Formulas with finite domains If the domain of a formula is finite, a formula can be converted into a propositional formula by changing each ∀x quantifier with a ∧ of the formula on all possible values of x; an ∃ quantifier becomes a ∨. Then terms of the form P (value) (e.g., Even(5)) are treated as propositional variables.
  • Limitations of first-order logic There are concepts that are not expressible by first-order formulas, for example, transitivity (“is there a flight from A to B with arbitrary many legs?” cannot be a database query described by a first-order formula).

Proof strategies

  • Existential statement: ∀xF (x). Constructive proof: give an example satisfying the formula under the quantifier (e.g, exists x which is both even and prime: take n = 2), then conclude by the existential generalization rule that ∃xF (x) is true. Non-constructive proof: If the proof says ∃nP (n), show that assuming ∀n¬P (n) leads to contradiction.
  • Universal statement: ∀xF (x). To prove that it is false, give a counterexample. To prove that it is true, start with the universal instantiation: take an arbitrary element, give it a name (say n) , and prove that F (n) holds without any additional assumptions. By universal generalization, conclude that ∀xF (x) holds.
  • To prove F (n)
    • Direct proof: show that F (n) holds directly, using definition, algebra, etc. If F (n) is of the form G(n) → H(n), then assume G(n) and derive H(n) from this assumption. Examples: sum of even integers is even, if n ≡ m( mod d) then there is k ∈ Z such that n = m + kd, Pythagoras’ theorem.
    • Proof by cases If F (x) is of the form (G 1 (x) ∨ G 2 (x) ∨ · · · ∨ Gk(x)) → H(x), then prove G 1 (x) → H(x), G 2 (x) → H(x)... Gk(x) → H(x). Examples: sum of two consecutive integers is odd, ∀x, y ∈ R|x + y| ≤ |x| + |y|, min(x, y) = (x + y − |x − y|)/2, k^2 + k is even.
    • Proof by contraposition If F (n) is of the form G(n) → H(n), can prove ¬H(n) → ¬G(n) (that is, assume that H(n) is false, and derive that G(n) is false). Examples: pigeonhole principle, if a square of an integer is even, then integer itself is even.
    • Proof by contradiction To prove F (n), show that ¬F (n) → F ALSE. Examples:

(2) is irrational, there are infinitely many primes.

  • Some definitions:
    • An n ∈ Z is even if ∃k ∈ Z such that n = 2k. An n ∈ Z is odd if ∃k ∈ Z such that n = 2k + 1. An n ∈ Z is divisible by m ∈ Z if ∃k ∈ Z such that n = km.
    • Modular arithmetic: for any n, d 6 = 0 ∈ Z ∃q, r ∈ Z such that n = qd + r and 0 ≤ r < d. Here, q is a quotient and r is a remainder. Congruence: for n, m, d 6 = 0 ∈ Z, n ≡ m( mod d) (”n is congruent to m mod d”) iff ∃q 1 , q 2 , r ∈ Z such that 0 ≤ r ≤ d, n = q 1 d + r and m = q 2 d + r. That is, n and m have the same remainder modulo d.
    • Absolute value of x ∈ R, denoted |x|, is x if x ≥ 0 and −x if x < 0.
    • A ceiling of x ∈ R, denoted dxe, is the smallest integer y such that y ≥ x. Similarly, a floor of x ∈ R, denoted bxc, is the largest integer y such that y ≤ x.

Table 1: Laws of boolean algebras, logic and sets Name Logic law Set theory law Boolean algebra law

Double Negation ¬¬p ≡ p A = A x = x DeMorgan’s laws ¬(p ∨ q) ≡ (¬p ∧ ¬q) A ∪ B = A ∩ B x + y = x · y ¬(p ∧ q) ≡ (¬p ∨ ¬q) A ∩ B = A ∪ B x · y = x + y Associativity (p ∨ q) ∨ r ≡ p ∨ (q ∨ r) (A ∪ B) ∪ C = A ∪ (B ∪ C) (x + y) + z = x + (y + z) (p ∧ q) ∧ r ≡ p ∧ (q ∧ r) (A ∩ B) ∩ C = A ∩ (B ∩ C) (x · y) · z = x · (y · z)

Commutativity p ∨ q ≡ q ∨ p A ∪ B = B ∪ A x + y = y + x p ∧ q ≡ q ∧ p A ∩ B = B ∩ A x · y = y · x

Distributivity p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r) A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) x · (y + z) = (x · y) + (x · z) p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r) A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C) x + (y · z) = (x + y) · (x + z) Idempotence (p ∨ p) ≡ p ≡ (p ∧ p) A ∪ A = A = A ∩ A x + x = x = x · x Identity p ∨ F ≡ p ≡ p ∧ T A ∪ ∅ = A = A ∩ U x + 0 = x = x · 1 Inverse p ∨ ¬p ≡ T A ∪ A¯ = U x + ¯x = 1 p ∧ ¬p ≡ F A ∩ A¯ = ∅ x · x¯ = 0 Domination p ∨ T ≡ T A ∪ U = U x + 1 = 1 p ∧ F ≡ F A ∩ ∅ = ∅ x · 0 = 0

Set Theory

  • A set is a well-defined collection of objects, called elements of a set. An object x belongs to set A is denoted x ∈ A (said “x in A” or “x is a member of A”). Usually for every set we consider a bigger “universe” from which its elements come (for example, for a set of even numbers, the universe can be all natural numbers). A set is often constructed using set-builder notation: A = {x ∈ U |P (x)} where U is a universe , and P (x) is a predicate statement; this is read as “x in U such that P (x)” and denotes all elements in the universe for which P (x) holds. Alternatively, for a small set, one can list its elements in curly brackets (e.g., A = { 1 , 2 , 3 , 4 }.)
  • A set A is a subset of set B, denotedA ⊆ B, if ∀x(x ∈ A → x ∈ B). It is a proper subset if ∃x ∈ B such that x /∈ A. Otherwise, if ∀x(x ∈ A ↔ x ∈ B) two sets are equal.
  • Special sets are: empty set ∅, defined as ∀x(x /∈ ∅). Universal set U : all potential elements under consideration at given moment. Natural numbers N (here, 0 ∈ N), integers Z, rationals Q , reals R.
  • A power set for a given set A, denoted 2A^ or P(A), is the set of all subsets of A. If A has n elements, then 2A^ has 2n^ elements (since for every element there are two choices, either it is in, or not).
  • Basic set operations are a complement A¯, denoting all elements in the universe that are not in A, then union A ∪ B= {x|x ∈ A or x ∈ B}, and intersection A ∩ B= {x|x ∈ A and x ∈ B} and set difference A − B = {x|x ∈ A and x ∈/ B}. Lastly, the Cartesian product of two sets A × B = {(a, b)|a ∈ A and b ∈ B}.
  • To prove that A ⊆ B, show that if you take an arbitrary element of A then it is always an element of B. To prove that two sets are equal, show both A ⊆ B and B ⊆ A. You can also use set-theoretic identities.
  • A function f (x) = x is called the identity function. It has the property that f −^1 (x) = f (x). A function f (x) = c for some fixed constant c (e.g., f (x) = 3) is called a constant function.
  1. Comparing set sizes Two sets A and B have the same cardinality if exists f that is a bijection from A to B. If a set has the same cardinality as N, we call it a countable set. If it has cardinality larger than the cardinality of N, we call it uncountable. If it has k elements for some k ∈ N, we call it finite, otherwise infinite (so countable and uncountable sets are infinite). E.g: N, Z, Q, Even, set of all finite strings, Java programs, or algorithms are all countable, and R, C, power set of N, are all uncountable. E.g., to

show that Z is countable, we prove that there is a bijection f: Z → N: take f (x) =

2 x x ≥ 0 1 − 2 x x < 0

It is one-to-one because f (x) = f (y) only if x = y, and it is onto because for any y ∈ N, if it is even then its preimage is y/2, if it is odd − y− 2 1. Often it is easier to give instead two one-to-one functions, from the first set to the second and another from the second to the third. Also, often instead of a full description of a function it is enough to show that there is an enumeration such that every element of, say, Z is mapped to a distinct element of N. To show that one finite set is smaller than another, just compare the number of elements. To show that one infinite set is smaller than another, in particular that a set is uncountable, use diagonalization: suppose that a there is an enumeration of elements of a set, say, 2N^ by elements of N. List all elements of 2N^ according to that enumeration. Now, construct a new set which is not in the enumeration by making it differ from the kth^ element of the enumeration in the kth^ place (e.g., if the second set contains element 2, then the diagonal set will not contain the element 2, and vice versa).

Mathematical induction.

  • Let n 0 , n ∈ N, and P (n) is a predicate with free variable n. Then the mathematical induction principle says: (P (n 0 ) ∧ ∀n ≥ n 0 (P (n) → P (n + 1))) → ∀n ≥ n 0 P (n) That is, to prove that a statement is true for all (sufficiently large) n, it is enough to prove that it holds for the smallest n = n 0 (base case) and prove that if it holds for some arbitrary n > n 0 (induction hypothesis) then it also holds for the next value of n, n + 1 (induction step).
  • A strong induction is a variant of induction in which instead of assuming that the statement holds for just one value of n we assume it holds for several: P (k) ∧ P (k + 1) ∧ · · · ∧ P (n) → P (n + 1) instead of just P (n) → P (n + 1). In particular, in complete induction k = n 0 , so we are assuming that the statement holds for all values smaller than n + 1.
  • A well-ordering principle states that every set of natural numbers has the smallest element. It is used to prove statements by counterexample: e.g., “define set of elements for which P (n) does not hold. Take the smallest such n. Show that it is either not the smallest, or P (n) holds for it”. These three principles, Induction, Strong Induction and Well-ordering are equivalent. If you can prove a statement by one of them, you can prove it by the others. The following is the structure of an induction proof. 1. P (n). State which assertion P (n) you are proving by induction. E.g., P (n): 2n^ < n!.
  1. Base case: Prove P (n 0 ) (usually just put n 0 in the expression and check that it works). E.g., P (4): 2^4 < 4! holds because 2^4 = 16 and 4! = 24 and 16 < 24.
  2. Induction hypothesis: “assume P (n) for some n > n 0 ”. I like to rewrite the statement for P (n) at this point, just to see what I am using. For example, “Assume 2n^ < n!”.
  3. Induction step: prove P (n + 1) under assumption that P (n) holds. This is where all the work is. Start by writing P (n + 1) (for example, 2n+1^ < (n + 1)!. Then try to make one side of the expression to “look like” (one side of) the induction hypothesis, maybe + some stuff and/or times some other stuff. For example, 2n+1^ = 2· 2 n, which is 2n^ times additional 2. The next step is either to substitute the right side of induction hypothesis in the resulting expression with the left side (e.g., 2n^ in 2 · 2 n^ with n!, giving 2 · n!, or just apply the induction hypothesis assumption to prove the final result You might need to do some manipulations with the resulting expression to get what you want, but applying the induction hypothesis should be the main part of the proof of the induction step.

Recursive definitions, grammars, function growth, structural induction.

  • A recursive definition (of a set) consists of 1) The base of recursion: “these several elements are in the set”. 2) The recursion: “these rules are used to get new elements”. 3) A restriction: “nothing extraneous is in the set”.
  • A Structural induction is used to prove properties about recursively defined sets. The base case of the structural induction is to prove that P (x) holds for the elements in the base, and the induction steps proves that if the property holds for some elements in the set, then it holds for all elements obtained using the rules in the recursion.
  • Recursive definitions of functions are defined similar to sets: define a function on 0 or 1 (or several), and then give a rule for constructing new values from smaller ones. Some recursive definitions do not give a well-defined function (e.g., G(n) = G(n/2) if n is even and G(3n + 1) if N is odd is not well defined). Some functions are well-defined but grow extremely fast: Ackermann function defined as ∀m, n > 0 , A(0, n) = n + 1, A(m, 0) = A(m − 1 , 1), A(m, n) = A(m − 1 , A(m, n − 1))
  • To compare grows rate of the functions, use O()-notation. f (n) ∈ O(g(n)) if ∃n 0 , c > 0 such that ∀n ≥ n 0 f (n) ≤ cg(n). In algorithmic terms, if f (n) and g(n) are running times of two algorithms for the same problem, f (n) works faster on large inputs.
  • An alphabet is a finite set of symbols (e.g.: binary alphabet { 0 , 1 }, English alphabet, etc). An alphabet is usually denoted Σ (not to be confused with the summation sign, this is a capital Greek letter “Sigma”). A (finite) string (also called word ) is a (finite) sequence of letters from an alphabet. A special empty string is denoted  or λ. A set of all strings is denoted Σ∗^ (pronounced “Sigma-star”, star notation is explained below). A language L over an alphabet Σ is a (possibly infinite) set of words from this language: L ⊆ Σ∗.
  • A context-free grammar consists of 1) Finite set Σ of terminals (letters in the alphabet). 2) Finite set V of variables (also called non-terminals), including a specical starting variable. 3) Finite set of rules, each of the form A → w for some variable A and a string of variables and terminals w (several rules for the same variable can also be written using symbol ”—” for ”or”: A → w 1 |w 2 |... |wk has the same meaning as A → w 1 , A → w 2 ,... , A → wk. For example, the following grammar defines
  • A sample space S is a set of all possible outcomes of an experiment ( {heads, tails} for a coin toss, {1,2,3,4,5,6} for a die throw). An event is a subset of the sample space. If all outcomes are equally likely, probability of each is 1/|S| (uniform distribution). Otherwise, sum of probabilities of all outcomes is 1, and probability of each is between 0 and 1: probability distribution on the sample space. A probability of an event P r(A) = Σa∈AP r(a). P r(A ∪ B) = P r(A) + P r(B) − P r(A ∩ B), so if events A and B are disjoint, then P r(A ∪ B) = P r(A) + P r(B).
  • Birthday paradox : about 23 people enough to have 1/2 probability that two have birthday the same day (if birthdays are uniformly random days of the year).
  • Conditional probability: P r(A|B) = P r(A ∩ B)/P r(B). If P r(A ∩ B) = P r(A) · P r(B), events A and B are independent. Better to switch the door in Monty Hall puzzle.
  • Bayes theorem: P r(B|A) = P r(A|B)·P r(B)/P r(A) = P r(A|B)·P r(B)/(P r(A|B)·P r(B)+P r(A| B¯)· P r( B¯). Generalizes to partition into arbitary many events rather than just B and B¯. For a medical test, let A be an event that the test came up positive, and B that a person is sick. If this medical test has a false positive rate P r(A| B¯) (healthy mistakenly labeled sick, specificity 1 − P r(A| B¯)) and false negative rate P r( A¯|B) (sick labeled healthy, sensitivity 1 − P r( A¯|B), and probability of being sick is P r(B), then probability of a person being sick if the test came up positive is P r(B|A) = P r(A|B) · P r(B)/P r(A), where P r(A) = P r(A|B) · P r(B) + P r(A| B¯) · P r( B¯).
  • Expectaton: let X be a random variable for some event over a sample space {a 1 ,... , an} (e.g., X is the number of coin tosses that came up heads, amount won in a lottery or X is 1 iff some event happened (indicator variable)). Then E(X) = Σni=1akP r(X(ak)) (if outcomes are numbers, often write X = ak in the equation). Linearity of expectation: E(X 1 + X 2 ) = E(X 1 ) + E(X 2 ), and E(aX + b) = aE(X) + b. Example: hat check problem.

Algorithm analysis.

  • Preconditions and postconditions for a piece of code state, respectively, assumptions about in- put/values of the variables before this code is executed and the result after. If the code is correct, then preconditions + code imply postconditions.
  • A Loop invariant is used to prove correctness of a loop. This is a statement implied by the precon- dition of the loop, true on every iteration of the loop (with loop guard variables as a parameter) and after the loop finishes implies the postcondition of the loop. A guard condition is a check whether to exist the loop or do another iteration. To prove total correctness of a program, need to prove that eventually the guard becomes false and the loop exists (however, it is not always possible to prove it given somebody’s code), otherwise, the proof is of partial correctness.
  • Correctness of recursive programs is proven by (strong) induction, with base case being the tail recursion call, and the induction step assumes that the calls returned the correct values and proves that the current iteration returns a correct value.
  • Running time of an algorithm is a function of the length of the input. Usually we talk about worst- case running time, and give a bound on it using O()-notation.
  • Average-case running time of an algorithm is an expectation over all inputs of a given length of the running time of this algorithm, also a function of the length of the input.