跳到主要内容

离散数学

大学计算机科学知识点 2

本主题的视频课程 打开视频页面
5:38

离散数学

离散数学研究的是可以逐一列举的对象,例如命题、集合、图和排列。前三个单元构建证明工具:逻辑、集合和归纳法。后三个单元构建计数工具:组合数学、图和概率。本课程中的每个证明都会说明所用的策略,每个恒等式都会先用一个小例子检验。

英文讲解 · 可切换中英文字幕

探索

Push the negation inside

Set a and b and compare ¬(a ∧ b) with ¬a ∨ ¬b, then the OR form. The two sides agree on every row, and the naive rewrite without flipping the connective fails on a counterexample.

探索

Union, intersection, difference

Switch between the set operations and watch which region of the diagram each one keeps.

讲义

English

What this course is, and how to use it

This is the second course in University Computer Science: discrete mathematics 离散数学, the mathematics of things you can list: statements, sets, graphs and counts. It gives the tools for proofs about algorithms and databases, and for every claim in computer science that something is correct.

A limit, stated once: this is a study course, not a degree. Every problem is original and complete in itself, and every proof names its strategy.

  • Units 2.1–2.3 are the tools of proof: logic, sets and induction.
  • Units 2.4–2.6 are the tools of counting: combinatorics, graphs and discrete probability.

The habit to build: name the strategy before you write the proof (direct, contraposition, contradiction or induction), and check every identity on a small example first.

Logic and proofs

Why it matters. Logic checks proofs the way types check programs. Translate the claim, check the argument, and choose the shape of the proof before you write a line.

The idea. Statements are joined with ¬ (not), ∧ (and), ∨ (or), → (if … then) and ↔ (if and only if). A truth table 真值表 settles any formula by checking every combination of its variables. Key equivalences:

  • $p \to q \equiv \neg p \lor q$;
  • contraposition: $p \to q \equiv \neg q \to \neg p$;
  • De Morgan: $\neg(p \land q) \equiv \neg p \lor \neg q$.

Predicate logic 谓词逻辑 adds the quantifiers $\forall x\, P(x)$ (for all) and $\exists x\, P(x)$ (there exists). Negation swaps one for the other.

A truth table and its proof tree
The table checks; the tree structures — a proof is a data structure of justified steps

Three shapes of proof. To prove $p \to q$:

  • direct proof 直接证明: assume $p$ and derive $q$;
  • contraposition 逆否证明: prove $\neg q \to \neg p$ instead (useful when the negative statements are easier to work with);
  • contradiction 反证法: assume $p \land \neg q$ and derive something impossible.

Each shape turns the goal into an assumption you can work with, so choosing the shape means choosing your starting data.

Worked example 1: translating a claim. Claim: every email has exactly one address, $\forall e\,\exists! a\,\text{Has}(e, a)$. To negate it, change $\forall$ to $\exists$ and negate the rest: some email has no address, or has two or more. One such email breaks the claim. Translate, negate, read again: a three-step habit.

Worked example 2: naming the strategy. Claim: if $n^2$ is even, then $n$ is even. Strategy: contraposition (the negative is easier). Proof: suppose $n$ is odd, so $n = 2k+1$. Then $n^2 = 4k^2 + 4k + 1 = 2(2k^2 + 2k) + 1$, which is odd. The contrapositive is proved, so the claim is true.

Common mistakes.

  • Proving the converse instead of the claim. Direction matters: $p \to q$ is not $q \to p$.
  • Negating without swapping the connectives. De Morgan swaps ∧ and ∨.
  • Giving examples where a proof for all cases is needed. An example is a check, not a proof.

Summary. Translate → choose the shape (direct, contraposition, contradiction) → write justified steps. $p \to q \equiv \neg p \lor q$. De Morgan swaps ∧ and ∨; negation swaps $\forall$ and $\exists$. Truth tables check; proof trees give structure.

Sets, functions and relations

Why it matters. Sets are the data model of discrete mathematics. Functions are typed pipes between sets, and relations carry the structure that databases and graphs are built from.

The idea.

  • Set operations: union, intersection, difference and complement.
  • The power set 幂集 $\mathcal{P}(A)$ is the set of all subsets of $A$, and $|\mathcal{P}(A)| = 2^{|A|}$.
  • Functions are classified by their arrow diagrams:
    • injective 单射: no two inputs share an output;
    • surjective 满射: every output is reached;
    • bijective 双射: both, so the pairing is perfect.
  • Composition $g \circ f$ applies $f$ first, then $g$. Bijections have inverses.
Set operations, arrow diagrams and relation properties
Arrows classify the function; the matrix reads the relation's properties at a glance

Why bijections count things. A bijection pairs every element of $A$ with exactly one element of $B$, so if one exists, $|A| = |B|$. Finding a bijection is a counting proof. For example, the subsets of an $n$-element set pair up with bit strings of length $n$ (each element is in or out: one bit each). This proves there are $2^n$ subsets without listing them.

Worked example 1: three functions on the integers.

  • $f(n) = 2n$ is injective (different inputs give different doubles) but not surjective (odd numbers are never reached).
  • $g(n) = \lfloor n/2 \rfloor$ is surjective but not injective.
  • $h(n) = -n$ is bijective, and it is its own inverse.

Worked example 2: testing a relation. On $\{1,2,3\}$, take $R = \{(1,1),(2,2),(3,3),(1,2),(2,1)\}$.

  1. Reflexive: every element is related to itself (all three loops are there).
  2. Symmetric: the pairs come in both directions.
  3. Transitive: the chain $(1,2),(2,1)$ needs $(1,1)$, and the chain $(2,1),(1,2)$ needs $(2,2)$; both are present.

So $R$ is an equivalence relation 等价关系, with classes $\{1,2\}$ and $\{3\}$. Check each property against its definition, not by feeling.

Common mistakes.

  • Mixing up $\in$ (is a member of) and $\subseteq$ (is a subset of).
  • Writing $f \circ g$ for $g \circ f$. Composition is read from right to left.
  • Testing symmetry on one pair instead of all pairs.

Summary. Union, intersection, difference, complement. $|\mathcal{P}(A)| = 2^{|A|}$ (by a bijection with bit strings). Injective, surjective and bijective, from the arrows. Composition reads right to left. Relation properties (reflexive, symmetric, antisymmetric, transitive) are tested against their definitions.

Induction and recursion

Why it matters. Induction proves that every loop and every recursive data structure is correct. Recursion is induction run in the other direction.

The idea. To prove $P(n)$ for all $n \ge n_0$:

  1. base case 基础情形: prove $P(n_0)$;
  2. inductive step 归纳步骤: assume $P(k)$ (the induction hypothesis 归纳假设) and derive $P(k+1)$.

Recursion defines $F$ using smaller calls of itself: factorial is $0! = 1$ and $n! = n \cdot (n-1)!$. It is the same ladder, built instead of climbed.

The induction ladder: base, hypothesis, step
Base plants the ladder; the step climbs one rung; every rung below is the hypothesis

Why induction works. The step proves that if rung $k$ holds, rung $k+1$ holds. The base proves rung $n_0$. Chaining these reaches every rung up to any $n$: induction is the rule "if $p$, and $p \to q$, then $q$", used again and again. Three faults to look for:

  • no base (a ladder standing on air);
  • the step does not establish $P(k+1)$ for every required $k$;
  • the step from $k$ assumes more than $P(k)$.

Worked example 1: the sum identity. Claim: $\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$. Strategy: induction.

  1. Base, $n = 1$: $1 = \frac{1 \cdot 2}{2}$. True.
  2. Step: assume $\sum_{i=1}^{k} i = \frac{k(k+1)}{2}$. Then
$$\sum_{i=1}^{k+1} i = \frac{k(k+1)}{2} + (k+1) = \frac{(k+1)(k+2)}{2}$$

The hypothesis is used, the algebra is shown, and the claim follows.

Worked example 2: catching a false proof. "All horses are the same colour." The base is trivially true. The step removes one horse from each side and uses the overlap. It fails at $k+1 = 2$, because the two groups of one horse each have no horse in common. The step must work for every $k$, including the smallest. A step that quietly needs $k \ge 2$, without a base case $P(2)$, is the classic fault.

Common mistakes.

  • Leaving out the base. Hypothesis and step prove nothing without a floor.
  • Using $P(k+1)$ to prove $P(k+1)$ (a circular argument).
  • Announcing strong induction but writing an ordinary step. Say which one you use, and use it.

Summary. Base + hypothesis + step, with the hypothesis clearly used. Strong induction assumes every rung below. Recursion is the mirror of induction (defined downwards). Faults: a missing base, an unused hypothesis, a step that only works for large $k$.

Counting and combinatorics

Why it matters. Counting is the arithmetic of complexity analysis: the size of a state space, the number of branches, and how many times a loop runs.

The idea.

  • Addition rule 加法原理: choices that cannot happen together add.
  • Multiplication rule 乘法原理: choices made one after another multiply.
  • Permutations 排列 (order matters): $P(n, r) = \frac{n!}{(n-r)!}$.
  • Combinations 组合 (order does not matter): $\binom{n}{r} = \frac{n!}{r!\,(n-r)!}$.
  • Inclusion–exclusion 容斥原理: $|A \cup B| = |A| + |B| - |A \cap B|$. Add, then subtract what was counted twice.
Counting trees for ordered and unordered selection
The tree makes the rules visible: branches multiply, alternatives add, overcounts subtract

Why the combination formula divides. A permutation chooses and arranges: $n$ options, then $n-1$, and so on, $r$ factors multiplied. A combination treats all arrangements of the same $r$ objects as one. Each set of $r$ objects was counted $r!$ times as an arrangement, so divide by $r!$. That division is the whole difference between ordered and unordered choices.

Worked example 1: the committee. Choose a team of 3 from 8 people, with one of them as leader.

  • Route 1: choose the team, $\binom{8}{3} = 56$, then the leader among them in 3 ways: $56 \times 3 = 168$.
  • Route 2: choose the leader first (8 ways), then 2 members from the other 7, $\binom{7}{2} = 21$: $8 \times 21 = 168$.

Two counting routes give one answer, and the agreement is the check.

Worked example 2: inclusion–exclusion with three sets. Among 100 students, 45 like tea, 35 coffee and 30 cocoa; 12 like tea and coffee, 10 tea and cocoa, 8 coffee and cocoa; and 5 like all three. The number who like at least one is

$$45 + 35 + 30 - 12 - 10 - 8 + 5 = 85$$

so $100 - 85 = 15$ like none. Add the singles, subtract the pairs, add back the triple: each student is then counted exactly once.

Common mistakes.

  • Adding when choices are made one after another (routes × vehicles multiply).
  • Using permutations when order does not matter (committees are combinations).
  • Using the two-set formula for three sets (the triple comes back with a plus sign).

Summary. Choices that cannot happen together add; choices in sequence multiply. $P(n,r)$ is ordered; $\binom{n}{r} = P(n,r)/r!$ is unordered. Inclusion–exclusion: + singles − pairs + triples. Count two ways and compare: this is the check in combinatorics.

Graphs and trees

Why it matters. Graphs can model almost anything: networks, states, dependencies. Trees are the main tool for searching and storing data. The handshake lemma 握手引理 is the first identity in the course that must always hold.

The idea. A graph is $G = (V, E)$, vertices and edges. Two ways to store it:

representation space edge test
adjacency matrix 邻接矩阵 $n^2$ $O(1)$
adjacency list 邻接表 $O(\lvert V\rvert+\lvert E\rvert)$ slower

A tree 树 is connected and has no cycles, and $|E| = |V| - 1$. Two ways to walk a graph:

  • BFS 广度优先搜索 (breadth-first search) uses a queue, visits level by level, and finds shortest paths in unweighted graphs;
  • DFS 深度优先搜索 (depth-first search) uses a stack or recursion, and finds connected components.
Small explicit graphs with adjacency structures
Three representations of the same graph — matrix, list, picture — and the degree sum checked by handshake

Why the handshake lemma must hold. Each edge adds 1 to the degree of each of its two ends, so every edge adds exactly 2 to the total degree:

$$\sum_{v} \deg(v) = 2|E|$$

Two results follow at once:

  • the number of vertices with odd degree is even;
  • no graph with $n \ge 2$ vertices can have all degrees different. The degrees would have to be $0, 1, \dots, n-1$, but a vertex of degree 0 and a vertex of degree $n-1$ cannot both exist.

Worked example 1: choosing the model. (Design question)

  • Friendships: undirected; store as an adjacency list, because the graph is sparse.
  • Task dependencies: directed with no cycles; a matrix is fine when the graph is small, and makes cycle checks easy.
  • A city metro: weighted, and needs Dijkstra's algorithm with a priority queue (beyond this unit).

The symmetry of the problem decides the type of graph, and the density decides how to store it.

Worked example 2: tree arithmetic. A tree has 12 vertices, and 5 of them are leaves (degree 1). Then $|E| = 11$, so the degree sum is 22. The leaves give 5, so the 7 inner vertices have a degree sum of $22 - 5 = 17$. Every tree question uses $|E| = |V| - 1$ and $\sum \deg = 2|E|$.

Common mistakes.

  • Forgetting vertices of degree 0. Isolated vertices are still vertices.
  • Mixing up connected (one piece) and complete (every possible edge).
  • Thinking BFS gives shortest paths in weighted graphs. It does not; that needs weighted methods.

Summary. $G = (V,E)$; choose matrix or list by density. A tree is connected, has no cycles, and $|E| = |V|-1$. BFS goes level by level; DFS finds components. $\sum \deg = 2|E|$, so the number of odd-degree vertices is even. Choose the model from the symmetry of the problem.

Discrete probability

Why it matters. Randomised algorithms, average-case analysis and hashing all use exact discrete probability: the counting of unit 2.4, now over outcomes that are equally likely.

Two ordinary six-sided dice, one cream and one red
Random outcomes: dice make each face equally likely under fair conditions

The idea.

  • A sample space 样本空间 $\Omega$ is the set of possible outcomes. Events are subsets. For finitely many equally likely outcomes, $P(E) = |E|/|\Omega|$.
  • Conditional probability 条件概率: $P(A \mid B) = \frac{P(A \cap B)}{P(B)}$.
  • Bayes' theorem 贝叶斯定理: $P(A \mid B) = \frac{P(B \mid A)\,P(A)}{P(B)}$.
  • Independence: $P(A \cap B) = P(A)P(B)$.
An outcome tree with conditional branches
The tree shows what the conditioning removes: P(A|B) lives on the B-branch, renormalised

Why conditioning makes the world smaller. Conditioning on $B$ keeps only the outcomes in $B$, and rescales their probabilities so that they add up to 1. $P(A \mid B)$ is the fraction of the $B$-world where $A$ is also true. This is all that Bayes' theorem does: $P(B \mid A)P(A)$ gives the size of the $A \cap B$ slice, and dividing by $P(B)$ rescales it to the $B$-world.

Worked example 1: dice by counting. Two fair dice give 36 equally likely pairs.

  • $P(\text{sum} = 7) = 6/36 = 1/6$ (six pairs).
  • $P(\text{sum} = 11) = 2/36$.
  • $P(\text{sum} = 7 \text{ or } 11) = 8/36 = 2/9$.

The counting of unit 2.4 does all the work.

Worked example 2: Bayes on a test. A disease affects 1% of people. A test has a sensitivity of 90% and a specificity of 90%. Then

$$P(\text{disease} \mid +) = \frac{0.9 \times 0.01}{0.9 \times 0.01 + 0.1 \times 0.99} = \frac{0.009}{0.108} = \frac{1}{12} \approx 8.3\%$$

This is the same arithmetic as unit 4 of the Medicine course MED12, here derived from the tree: a rare condition stays unlikely after imperfect evidence.

Common mistakes.

  • Adding conditional probabilities from different conditions. They must all be in the same conditioned world.
  • Mixing up $P(A \mid B)$ and $P(B \mid A)$ (the "prosecutor's fallacy").
  • Treating outcomes that are not equally likely as equal. Only equally likely outcomes give $|E|/|\Omega|$.

Summary. $P = |E|/|\Omega|$ for equally likely outcomes. $P(A \mid B) = P(A \cap B)/P(B)$: the $B$-branch, rescaled. Bayes rescales slices between worlds. Independent events multiply. Dice, cards and hash tables are all counting problems underneath.

词汇
English 中文 拼音
discrete mathematics/dɪˈskriːt ˌmæθɪˈmætɪks/ 离散数学 lí sàn shù xué
truth table/truːθ ˈteɪbl/ 真值表 zhēn zhí biǎo
predicate logic/ˈpredɪkeɪt ˈlɒdʒɪk/ 谓词逻辑 wèi cí luó jí
direct proof/daɪˈrekt pruːf/ 直接证明 zhí jiē zhèng míng
contraposition/ˌkɒntrəpəˈzɪʃn/ 逆否证明 nì fǒu zhèng míng
contradiction/ˌkɒntrəˈdɪkʃn/ 反证法 fǎn zhèng fǎ
power set/ˈpaʊə set/ 幂集 mì jí
injective/ɪnˈdʒektɪv/ 单射 dān shè
surjective/sɜːˈdʒektɪv/ 满射 mǎn shè
bijective/baɪˈdʒektɪv/ 双射 shuāng shè
equivalence relation/ɪˈkwɪvələns rɪˈleɪʃn/ 等价关系 děng jià guān xì
base case/beɪs keɪs/ 基础情形 jī chǔ qíng xíng
inductive step/ɪnˈdʌktɪv step/ 归纳步骤 guī nà bù zhòu
induction hypothesis/ɪnˈdʌkʃn haɪˈpɒθəsɪs/ 归纳假设 guī nà jiǎ shè
addition rule/əˈdɪʃn ruːl/ 加法原理 jiā fǎ yuán lǐ
multiplication rule/ˌmʌltɪplɪˈkeɪʃn ruːl/ 乘法原理 chéng fǎ yuán lǐ
permutation/ˌpɜːmjuːˈteɪʃn/ 排列 pái liè
combination/ˌkɒmbɪˈneɪʃn/ 组合 zǔ hé
inclusion–exclusion/ɪnˈkluːʒn eksˈkluːʒn/ 容斥原理 róng chì yuán lǐ
handshake lemma/ˈhændʃeɪk ˈlemə/ 握手引理 wò shǒu yǐn lǐ
adjacency matrix/əˈdʒeɪsənsi ˈmeɪtrɪks/ 邻接矩阵 lín jiē jǔ zhèn
adjacency list/əˈdʒeɪsənsi lɪst/ 邻接表 lín jiē biǎo
tree/triː/ 树 shù
BFS/ˌbiː ef ˈes/ 广度优先搜索 guǎng dù yōu xiān sōu suǒ
DFS/ˌdiː ef ˈes/ 深度优先搜索 shēn dù yōu xiān sōu suǒ
sample space/ˈsæmpl speɪs/ 样本空间 yàng běn kōng jiān
conditional probability/kənˈdɪʃənl ˌprɒbəˈbɪlɪti/ 条件概率 tiáo jiàn gài lǜ
Bayes' theorem/beɪz ˈθɪərəm/ 贝叶斯定理 bèi yè sī dìng lǐ

该知识点的互动课程

逐步完成,配合即时检查练习。

更多 大学计算机科学 知识点

登录或创建账户

IGCSE, A-Level & AP