Skip to content

Commit

Permalink
马原笔记
Browse files Browse the repository at this point in the history
  • Loading branch information
bowling233 committed Jan 10, 2024
1 parent cb8bf13 commit 8098b85
Show file tree
Hide file tree
Showing 27 changed files with 1,383 additions and 47 deletions.
58 changes: 58 additions & 0 deletions docs/books/Algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,64 @@ $$
从上面的分析可知:最大子数组问题的分治解法的时间复杂度为 $T(n)=2T(n/2)+O(n)$,即 $O(n\lg n)$。
<!-- prettier-ignore-end -->

##

几个概念:

- 顶点的**度数**、路径、简单路径(无重复顶点)、环、简单环(无重复顶点和边)、长度
- 连通、子图、连通分量
- 密度、稀疏图、稠密图
- 二分图

### 无向图

无向图有两种特殊情况,我们不做考虑:自环和平行边。

我们仅考虑简单的路径和环。

<!-- prettier-ignore-start -->
!!! note "生成树"

树是无环连通图。互不相连的树成为森林。

连通图的生成树是它的一幅子图,它含有图中所有顶点,且是一棵树。图的生成树森林是它的所有连通子图的生成树的集合。

!!! note "树的数学性质"

一幅含有 $V$ 个节点的图满足下面的任意条件时,它是一棵树:

- 有 $V-1$ 条边,且不含有环。
- 有 $V-1$ 条边,且是连通的。
- 连通,但删除任意一条边都会使它不连通。
- 无环,但添加任意一条边都会产生环。
- 任意两个顶点之间都有且仅有一条简单路径。
<!-- prettier-ignore-end -->

#### 无向图的表示

API:添加边、查询相邻顶点、字符串表示。

通过上面的 API 可以进行下列操作:

- 计算度数
- 计算图的度数
- **计算自环的个数**
- 邻接表表示

有三种数据结构可选:

- 邻接矩阵(太大)
- 边的数组(存储边的信息)
- 邻接表数组

如果需要进行添加、删除节点、删除边、检查是否含有边等操作,可以进一步使用**邻接集**的表示方法。会造成一定性能损失。

#### 连通性

连通性可以用并查集算法处理。对每条边执行 `union()` 操作。

深度优先搜索更常用于图的处理。

## 案例

### union-find 动态连通性
Expand Down
120 changes: 120 additions & 0 deletions docs/books/CppPrimer.md
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,126 @@ Skipped.

### Chapter 12 Dynamic Memory

We've used automatic (saved in **stack** memory) and `static` (saved in **static** memory) objects. C++ lets
us allocate objects dynamically

Dynamically allocated objects have a lifetime that is **independent of where they are created**; they exist until they are **explicitly freed**. They are stored in **heap** memory.

Library provides two smart pointer types that manage dynamic objects. A smart pointer acts like a regular pointer with the important exception that it **automatically deletes** the object to which it points.

Common operations:

```cpp
p.get(); // return a pointer
swap(p, q);
p.swap(q);
shared_ptr<T> p(q, d);

```
#### `shared_ptr`
```cpp
shared_ptr<string> p1;
auto p6 = make_shared<vector<string>>();
auto q(p);
p.unique();
p.use_count();
```

It's a template.

Allows multiple pointers to refer to the same object. Think of a `shared_ptr` as if it has an associated counter.

- count is incremented when
- **copied**
- used as **right-hand** operand of **assignment**
- pass to or
- return from a function by **value**.
- decremented when
- assign new value to
- destroyed.

Once a **shared_ptr**’s counter goes to zero, the shared_ptr automatically frees the object that it manages.

- Cannot convert ordinary Pointers to `shared_ptr`s implicitly. Use initializer: `shared_ptr<int> p2(new int(42));`.

<!-- prettier-ignore-start -->
!!! warning "initialize a smart pointer must point to dynamic memory because use delete to free the associated object"
<!-- prettier-ignore-end -->

Use `make_shared` function to allocate and use dynamic memory.

- It returens a `shared_ptr`.
- It uses its arguments to construct an object of the given type.
- Value initialization if no arguments.
- Use `auto` to avoid type name.

<!-- prettier-ignore-start -->
!!! note "Value Initialization"

Value Initialization is similar to default initialization. In the case of **built-in types** the difference is significant; a value-initialized object of built-in type has a well-defined value but a default-initialized object does not.

We can usually omit the value and supply only a size. In this case the library creates a **value-initialized element initializer for us**. This library-generated value is used to initialize each element in the container. The value of the element initializer depends on the type of the elements stored in the vector.

If the `vector` holds elements of a built-in type, such as int, then the element initializer has a value of 0. If the elements are of a class type, such as string,then the element initializer is itself default initialized.

!!! danger "Memory Leak: `shared_ptr`s in a container"

If you put shared_ptrs in a container, and you subsequently need to use some, but not all, of the elements, remember to erase the elements you no longer need.
<!-- prettier-ignore-end -->

`shared_ptr` is usually used to allow multiple objects to **share the same state** (refer to same object when copied).

<!-- prettier-ignore-start -->
!!! example "`StrBlob`"

```cpp
class StrBlob {
public:
typedef std::vector<std::string>::size_type size_type;
StrBlob();
StrBlob(std::initializer_list<std::string> il);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const std::string &t) {data->push_back(t);}
void pop_back();
std::string& front();
std::string& back();
private:
std::shared_ptr<std::vector<std::string>> data;
void check(size_type i, const std::string &msg) const;
};
```

Uses the **default versions** of the operations that copy, assign, and destroy objects of its type.
<!-- prettier-ignore-end -->

#### Direct manage memory

```cpp
int *pi = new int;
int *pi = new int(1024);
vector<int> *pv = new vector<int>{0,1,2,3,4,5,6,7,8,9};
string *ps = new string(); // value initialized
string *ps1 = new string; // default initialized
auto p1 = new auto(obj);
const string *pcs = new const string;
int *p1 = new int; // ifallocation fails, new throws std::bad_alloc
int *p2 = new (nothrow) int; // if allocation fails, new returns a null pointer
```
Two operators: `new`, `delete`.
- `new` returns a pointer to allocated memory.
- can use direct initialization, traditional construction (parentheses) or list initialization.
<!-- prettier-ignore-start -->
!!! tip "Caller is responsible for deleting the memory."
!!! tip "assign `nullptr` to the pointer after we use `delete`"
!!! danger "Provides Only Limited Protection: There can be several pointers that point to the same memory."
<!-- prettier-ignore-end -->
## Part III: Tools for Class Authors
### Chapter 13 Copy Control
Expand Down
153 changes: 120 additions & 33 deletions docs/courses-zju/discrete-math/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,149 @@

## 单词表

| 英文 | 中文 |
| ---- | ---- |
| 英文 | 中文 |
| ------------- | ---- |
| propositional | 命题 |
| predicate | 谓词 |
| quantifier | 量词 |
| cardinality | 基数 |
| hypothesis | 假设 |
| antecedent | 前件 |
| premise | 前提 |
| conclusion | 结论 |
| consequence | 结果 |
| predicate | 谓词 |
| quantifier | 量词 |
| cardinality | 基数 |
| hypothesis | 假设 |
| antecedent | 前件 |
| premise | 前提 |
| conclusion | 结论 |
| consequence | 结果 |

## 命题逻辑

### 简单逻辑运算符

| 算符 | 名称 | 有关的运算性质 |
| ---- | ---- | -------------- |
| $\neg$ | 非 negation ||
| $\wedge$ | 合取 conjunction | |
| $\vee$ | 析取 disjunction | |
| $\oplus$ | 异或 exclusive or | |
| 算符 | 名称 | 有关的运算性质 |
| -------- | ----------------- | -------------- |
| $\neg$ | 非 negation | |
| $\wedge$ | 合取 conjunction | |
| $\vee$ | 析取 disjunction | |
| $\oplus$ | 异或 exclusive or | |

- 从两个及以上命题构造新命题的逻辑运算符称为**联结词(connectives)**
- 从两个及以上命题构造新命题的逻辑运算符称为**联结词(connectives)**

### 条件语句

| 算符 | 名称 | 有关的运算性质 |
| ---- | ---- | -------------- |
| $\rightarrow$ | 条件语句 condition statement<br>蕴含 implication | |
| $\leftrightarrow$ | 双条件语句 biconditional statement<br>等价 equivalence | |

- 逆命题、逆否命题、反命题
- 两个复合命题,不论变量的取值如何,真值总是相同,则称这两个复合命题**等价**
| 算符 | 名称 | 有关的运算性质 |
| ----------------- | ------------------------------------------------------ | -------------- |
| $\rightarrow$ | 条件语句 condition statement<br>蕴含 implication | |
| $\leftrightarrow$ | 双条件语句 biconditional statement<br>等价 equivalence | |

- 逆命题、逆否命题、反命题
- 两个复合命题,不论变量的取值如何,真值总是相同,则称这两个复合命题**等价**

### 优先级

$\neg$ > $\wedge$ > $\vee$ > $\rightarrow$ > $\leftrightarrow$

### 命题的等价与化简

| 公式 | 名称 | 化简 |
| ---- | ---- | ---- |
| $p \wedge \neg p$ | 矛盾式 contradiction | F |
| $p \vee \neg p$ | 永真式 tautology | T |
| $p \rightarrow q$ | 蕴含式 implication | $\neg p \vee q$ |
| $p \leftrightarrow q$ | 等价式 equivalence | $(p\wedge q) \vee (\neg p \wedge \neg q)$ |
| $\neg (p \wedge q)$ | 德摩根定律 De Morgan's law | $\neg p \vee \neg q$ |
| $\neg (p \vee q)$ | 德摩根定律 De Morgan's law | $\neg p \wedge \neg q$ |
| 公式 | 名称 | 化简 |
| --------------------- | -------------------------- | ----------------------------------------- |
| $p \wedge \neg p$ | 矛盾式 contradiction | F |
| $p \vee \neg p$ | 永真式 tautology | T |
| $p \rightarrow q$ | 蕴含式 implication | $\neg p \vee q$ |
| $p \leftrightarrow q$ | 等价式 equivalence | $(p\wedge q) \vee (\neg p \wedge \neg q)$ |
| $\neg (p \wedge q)$ | 德摩根定律 De Morgan's law | $\neg p \vee \neg q$ |
| $\neg (p \vee q)$ | 德摩根定律 De Morgan's law | $\neg p \wedge \neg q$ |

- $p \leftrightarrow q$ 永真,则称 $p$ 与 $q$ 等价,记 $p \equiv q$。注意:$\equiv$ 和 $\Leftrightarrow$ 不是逻辑联结词,而是代表这一语句。

## 基本结构:集合、函数、序列、求和与矩阵

| Eng | 中文 |
| :------------------: | :-------------: |
| universal set | 全集 |
| open/closed interval | 开/闭区间 |
| singleton set | 单元素集 |
| Venn diagram | 维恩图 |
| **proper** subset | ****子集 |
| cardinality | 基数 |
| power set | 幂集 |
| Cartesian product | 笛卡尔积 |
| ordered n-**tuple** | 有序 n **元组** |
| truth set | 真值集 |

### 集合

简单内容一笔带过:

- 子集、韦恩图
- 基数 $|A|$,有限集、无限集
- 幂集 $\mathcal{P}(A)$
- 笛卡儿积 $A \times B = \{(a, b) \mid a \in A, b \in B\}$
- 笛卡儿积的子集称为从 $A$ 到 $B$ 的**关系**
- 运算:交、并、补、差

<!-- prettier-ignore-start -->
!!! warning "注意:$\emptyset$ 的幂集不是 $\{\emptyset\}$,而是 $\{\emptyset, \{\emptyset\}\}$。"
<!-- prettier-ignore-end -->

带量词的集合符号:

$$
\forall x \in S(P(x)) \equiv \forall x (x \in S \rightarrow P(x))
\forall x \in \mathbf{R}(x^2 \geq 0)
\exists x \in S(P(x)) \equiv \exists x (x \in S \wedge P(x))
\exists x \in \mathbf{Z}(x^2=1)
$$

真值集和量词:给定谓词 $P$ 和论域 $D$,真值集 $\{x \in D \mid P(x)\}$。

集合运算,其实可以转化为命题逻辑的运算:

$$
\begin{aligned}
\overline{A\cap B} &= \{x \mid x \notin A \cap B\} \\
&= \{x \mid \neg (x \in A \cap B)\} \\
&= \{x \mid \neg (x \in A \wedge x \in B)\} \\
&= \{x \mid \neg x \in A \vee \neg x \in B\} \\
&= \{x \mid x \notin A \vee x \notin B\} \\
&= \{x \mid x \notin A\} \cup \{x \mid x \notin B\} \\
&= \overline{A} \cup \overline{B}
\end{aligned}
$$

上面的方法可用于证明基础的集合恒等式。化简集合等式时,直接应用已知的恒等式即可。

- $p \leftrightarrow q$ 永真,则称 $p$ 与 $q$ 等价,记 $p \equiv q$。注意:$\equiv$ 和 $\Leftrightarrow$ 不是逻辑联结词,而是代表这一语句。
### 集合的基数

- 衡量无限集的相对大小

<!-- prettier-ignore-start -->
!!! note "基数"

当且仅当集合 $A$ 和 $B$ 之间存在双射时,称 $A$ 和 $B$ 的基数相同,记作 $|A|=|B|$。
!!! note "可数集与不可数集"

- 可数集:和正整数集有相同基数 $\aleph_0$ 的集合

如果能将集合中的元素排成序列,那么这个集合就是可数集。正有理数集是可数集,就是通过排列的方式证明的。

- 不可数集:不是可数集的集合

通常使用反证法证明不可数集。假定实数集是可数的,那么可以将实数集排成一个序列,然后构造一个不在这个序列中的实数(通过取数字的方法),从而得到矛盾。
<!-- prettier-ignore-end -->

- 两个可数集合的并集是可数集

根据集合是否有限,分三种情况证明即可。

- Schroder-Bernstein 定理:如果 $|A| \leq |B|$ 且 $|B| \leq |A|$,则 $|A|=|B|$
> 该证明较难:构造双射 $f:A \rightarrow B$ 和 $g:B \rightarrow A$,然后证明 $f \circ g$ 和 $g \circ f$ 都是恒等映射。
<!-- prettier-ignore-start -->
!!! example "证明 $|(0, 1)|=|(0, 1]|$"
<!-- prettier-ignore-end -->

- 不可计算性
1. 任何编程语言编写的计算机程序的集合是**可数的**
2. 从一个特定的可数无限集到自身的函数有**不可数无限多个**
3. 结合上面两个结论,可得到:存在不可计算的函数。

- 连续统假设
1. $|\mathbf{P}(\mathbf{Z^+})|=|\mathbf{R}|=c$
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8098b85

Please sign in to comment.