思维提升篇-数学抽象与计算机实现
强烈建议大家:把第19.4节的原文喂给任何一个大模型,然后用如下提示词来问大模型,并查看下输出,绝对有收获,请相信我!!!
本文试图探讨数学抽象跟计算机实现之间隔着怎样的鸿沟,借助第19.4节,我觉得答案不言自明,即图结构。
大模型提示词
- 根据第19.4节的描述,给了我们一种重新看待数学证明的思路,即思考数学推导如何转换为算法实现,这样就将证明形象化了。
- 学习第19.4节后,我要对数学里的构造性证明重视起来,因为它可能蕴含着一种算法实现思路。
- 从第19.4节的角度出发,需要重视离散数学里出现的构造性定义、证明,它们可能对应一个数据结构的构造、算法的执行流程。
- 根据第19.4节,帮我列出一张“常见数学结构 ↔ 图模型 ↔ 算法实现”的系统对照表。
第19.4节原文
19.4-Equivalence Relations and Partial Orders
## 19.4 Equivalence Relations and Partial Orders
This section is concerned with basic concepts in set theory and their relationship to abstract-transitive-closure algorithms. Its purposes are to put the ideas that we are studying into a larger context and to demonstrate the wide applicability of the algorithms that we are considering. Mathematically inclined readers who are familiar with set theory may wish to skip to section 19.5 because the material that we cover is elementary (although our brief review of terminology may be helpful); readers who are not familiar with set
theory may wish to consult an elementary text on discrete mathematics because our treatment is rather succinct. The connections between digraphs and these fundamental mathematical concepts are too important for us to ignore.Given a set, a **relation** among its objects is defined to be a set of ordered pairs of the objects. Except possibly for details relating to parallel edges and self-loops, this definition is the same as our definition of a digraph: Relations and digraphs are different representations of the same abstraction. The mathematical concept is somewhat more powerful because the sets may be infinite, whereas our computer programs all work with finite sets, but we ignore this difference for the moment.Typically, we choose a symbol `R` and use the notation `sRt` as shorthand for the statement "the ordered pair `(s, t)` is in the relation `R`." For example, we use the symbol "$<$" to represent the "less than" relation among numbers.Using this terminology, we can characterize various properties of relations. For example, a relation `R` is said to be **symmetric** if `sRt` implies that `tRs` for all `s` and `t`; it is said to be **reflexive** if `sRs` for all `s`.
- Symmetric relations are the same as undirected graphs.
- Reflexive relations correspond to graphs in which all vertices have self-loops, relations that correspond to graphs where no vertices have self-loops are said to be irreflexive.A relation `R` is said to be **transitive** when `sRt` and `tRu` implies that `sRu` for all `s`, `t`, and `u`. The transitive closure of a relation is a well-defined concept; but instead of redefining it in set-theoretic terms, we appeal to the definition that we gave for digraphs in section 19.3. **Any relation is equivalent to a digraph, and the transitive closure of the relation is equivalent to the transitive closure of the digraph**. The transitive closure of any relation is transitive.In the context of graph algorithms, we are particularly interested in two particular transitive relations that are defined by further constraints. These two types, which are widely applicable, are known as **equivalence relations** and **partial orders**.An equivalence relation $\equiv$ is a transitive relation that is also reflexive and symmetric. Note that a symmetric, transitive relation that includes each object in some ordered pair must be an equivalence relation: If $s\equiv t$, then $t\equiv s$ (by symmetry) and $s\equiv s$ (by transitivity).Equivalence relations divide the objects in a set into subsets known as **equivalence classes**. Two objects `s` and `t` are in the same equivalence class if and only if $s\equiv t$. The following examples are typical equivalence relations:
- **Modular arithmetic** Any positive integer $k$ defines an equivalence relation on the set of
integers, with $s\equiv t (\mod k)$ if and only if the remainder that results when we divide `s` by `k`
is equal to the the remainder that results when we divide `t` by `k`. The relation is obviously symmetric; a short proof establishes that it is also transitive (see Exercise 19.70) and therefore is an equivalence relation.
- **Connectivity in graphs** The relation "is in the same connected component as" among vertices is an equivalence relation because it is symmetric and transitive. The equivalence classes correspond to the connected components in the graph.When we build a graph ADT that gives clients the ability to test whether two vertices are in the same connected component, we are implementing an equivalence-relation ADT that provides clients with the ability to test whether two objects are equivalent. In practice, this correspondence is significant because the graph is a succinct representation of the equivalence relation (see Exercise 19.74). In fact, as we saw in Chapters 1 and 18, to build such an ADT we need to maintain only a single vertex-indexed array.A partial order $\prec$ is a transitive relation that is also irreflexive. As a direct consequence of transitivity and irreflexivity, it is trivial to prove that partial orders are also asymmetric: If $s\prec t$ and $t\prec s$, then $s\prec s$ (by transitivity), which contradicts irreflexivity, so we cannot have both $s\prec t$ and $t\prec s$. Moreover, extending the same argument shows that a partial order cannot have a cycle, such as $s\prec t$, $t\prec u$, and $u\prec s$.The following examples are typical partial orders:
- **Subset inclusion** The relation "includes but is not equal to" ($\subset$) among subsets of a given set is a partial order—it is certainly irreflexive, and if $s\subset t$ and $t\subset u$, then certainly $s\subset u$.
- **Paths in DAGs** The relation "can be reached by a nonempty directed path from" is a partial order on vertices in DAGs with no self-loops because it is transitive and irreflexive. Like equivalence relations and undirected graphs, this particular partial order is significant for many applications because a DAG provides a succinct implicit representation of the partial order. For example, Figure 19.17 illustrates DAGs for subset containment partial orders whose number of edges is only a fraction of the cardinality of the partial order (see Exercise 19.76).
Indeed, we rarely define partial orders by enumerating all their ordered pairs, because there are too many of such pairs. Instead, we generally specify an irreflexive relation (a DAG) and consider its transitive closure. This usage is a primary reason for considering abstract–transitive-closure ADT implementations for DAGs. Using DAGs, we consider examples of partial orders in section 19.5.A total order `T` is a partial order where either `sTt` or `tTs` for all $s\neq t$. Familiar examples of total orders are the “less than” relation among integers or real numbers and lexicographic ordering among strings of characters. Our study of sorting and searching algorithms in Parts 3 and 4 was based on a total-order ADT implementation for sets. In a total order, there is one and only one way to arrange the elements in the set such that `sTt` whenever `s` is before `t` in the arrangement; in a partial order that is not total, there are many ways to do so. In section 19.5, we examine algorithms for this task.In summary, the following correspondences between sets and graph models help us to understand the importance and wide applicability of fundamental graph algorithms.
- Relations and digraphs
- Symmetric relations and undirected graphs
- Transitive relations and paths in digraphs
- Equivalence relations and paths in undirected graphs
- Partial orders and paths in DAGsThis context places in perspective the types of graphs and algorithms that we are considering and provides one motivation for us to move on to consider basic properties of DAGs and algorithms for processing those DAGs.### Exercises
19.70 Show that “has the same remainder after dividing by $k$” is a transitive relation (and therefore is an equivalence relation) on the set of integers.19.71 Show that “is in the same edge-connected component as” is an equivalence relation among vertices in any graph.19.72 Show that “is in the same biconnected component as” is not an equivalence relation among vertices in all graphs.19.73 Prove that the transitive closure of an equivalence relation is an equivalence relation, and that the transitive closure of a partial order is a partial order.19.74 The cardinality of a relation is its number of ordered pairs. Prove that the cardinality of an equivalence relation is equal to the sum of the squares of the cardinalities of that relation’s equivalence classes.19.75 Using an online dictionary, build a graph that represents the equivalence relation “has $k$ letters in common with” among words. Determine the number of equivalence classes for $k = 1$ through $5$.19.76 The cardinality of a partial order is its number of ordered pairs. What is the cardinality of the subset containment partial order for an $n$-element set?19.77 Show that “is a factor of” is a partial order among integers.---
感悟
第19.4节不仅仅是讲了几个数学概念,它更像是一把钥匙,打开了我们看待数学抽象与计算机实现之间关系的大门。它强有力地揭示了离散数学如何为算法和数据结构提供坚实的理论基础和直观的指导。
这种启发是双向的:
-
从数学到算法: 当我们理解了“关系”就是“有向图”,“等价关系”就是“无向图的连通分量”,“偏序”就是“DAG中的路径”时,那些抽象的数学定义和证明立刻变得鲜活起来。它们不再是纸面上的符号推演,而是可以被编写成代码、在计算机上运行的逻辑。数学中的构造性定义和证明,直接就成了我们设计数据结构和算法的蓝图。
-
从算法到数学: 反过来,当你实现一个图算法,比如DFS来查找连通分量时,你也在无形中“证明”了连通性是一种等价关系;当你计算传递闭包时,你就是在显式地“构造”一个传递关系。算法的执行过程本身,就是对数学原理的动态演示。
这种深层次的联系,不仅让学习变得更有趣,也极大地提升了我们解决问题的能力。它告诉我们,计算机科学不仅仅是编程,更是将数学智慧转化为实用工具的艺术。