الرسوم البيانية والأشجار
مُقدَّم
5:38
الرياضيات المتقطعة
Discrete mathematics is the mathematics of objects you can list: statements, sets, graphs and arrangements. The first three units build the proof engine…
سرد باللغة الإنجليزية · ترجمة نصية بالإنجليزية و中文 يمكنك التبديل بينهما
في هذا الدرس
| English | المعنى |
|---|---|
| graph/ɡræf/ | الرسم البياني |
| edge/edʒ/ | الضلع |
| degree/dɪˈɡriː/ | الدرجة |
| directed/daɪˈrektɪd/ | موجّه |
| vertex/ˈvɜːteks/ | رأس |
| adjacency matrix/əˈdʒeɪsənsi ˈmeɪtrɪks/ | مصفوفة التجاور |
| adjacency list/əˈdʒeɪsənsi lɪst/ | قائمة التجاور |
| sparse/spɑːs/ | متفرع |
| handshake lemma/ˈhændʃeɪk ˈlemə/ | مبرهنة المصافحة |
| tree/triː/ | الشجرة |
| acyclic/əˈkaɪklɪk/ | غير دوري |
| leaf/liːf/ | ورقة |
| breadth-first search/bredθ fɜːst sɜːtʃ/ | البحث بالعرض أولاً |
| depth-first search/depθ fɜːst sɜːtʃ/ | البحث بالعمق أولاً |
Friends, flights and build steps
- A social network, an airline map and the order in which code modules must compile look nothing alike. Each one is a set of things joined by links.
- That shared shape is a graph 图. Learn it once and the same tools count, search and check all three.
Vertices, edges and degree
- A graph 图 $G = (V, E)$ has vertices 顶点 $V$ and edges 边 $E$ joining pairs of them. Edges can be directed 有向 (one-way, like build order) or undirected (like friendship).
- The degree 度 of a vertex is the number of edges touching it. An isolated vertex has degree 0 and still counts as a vertex.
صل كل نظام بنوع الرسم البياني الذي يمثله.
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
الصداقة تعمل في الاتجاهين؛ ترتيب البناء يعمل في اتجاه واحد ولا يمكن أن يشكل حلقة؛ المسافات هي أوزان؛ الروابط يمكن أن تشير إلى الوراء.
Two ways to store a graph
- An adjacency matrix 邻接矩阵 is an $n \times n$ grid of 0s and 1s. It takes $n^2$ space but answers "is there an edge from $u$ to $v$?" in $O(1)$.
- An adjacency list 邻接表 stores each vertex's neighbours. It takes $O(|V| + |E|)$ space, so it suits sparse 稀疏 graphs with few edges per vertex.

خريطة طرق تحتوي على مليون تقاطع، يتصل كل منها بحوالي أربعة تقاطعات أخرى. أفضل طريقة للتخزين هي —
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
المصفوفة تحتاج 10¹² خلية، ومعظمها 0؛ بينما تخزن القائمة فقط 4 مليون تقريباً من الحواف الموجودة.
The handshake lemma
- Each edge adds 1 to the degree of each of its two ends, so $\sum_v \deg(v) = 2|E|$. This is the handshake lemma 握手引理. In the figure, $3 + 2 + 2 + 2 + 3 = 12 = 2 \times 6$.
- Corollary: the number of odd-degree vertices is always even. Seven people cannot each shake hands with exactly three others, because $7 \times 3 = 21$ is odd.
الرسم البياني يحتوي على 9 حافة. ما هو مجموع درجات جميع رؤوسه؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
كل حافة تضيف 2، لذا $2 \times 9 = 18$.
يمكن أن يحتوي الرسم البياني على ثلاث رؤوس بالضبط ذات درجة فردية.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
مجموع الدرجات زوجي، لذا يجب أن تأتي الدرجات الفردية بعدد زوجي.
Trees: connected, no cycles
- A tree 树 is connected (one piece) and acyclic 无环 (no cycles). Every tree with $|V|$ vertices has exactly $|E| = |V| - 1$ edges.
- A leaf 叶子 has degree 1. Combine the two facts with the handshake lemma to answer most tree questions.
A tree has 12 vertices, 5 of them leaves. Edges: $12 - 1 = 11$, so the degree sum is $2 \times 11 = 22$. The leaves give 5, so the other 7 vertices carry $22 - 5 = 17$.
الشجرة تحتوي على 30 رأس. كم عدد حوافها؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
كل شجرة تحتوي على $|E| = |V| - 1 = 29$.
Two ways to search
- Breadth-first search 广度优先搜索 (BFS) uses a queue and visits level by level. In an unweighted graph it finds shortest paths, counted in edges.
- Depth-first search 深度优先搜索 (DFS) uses a stack or recursion and goes as deep as it can before backing up. It finds connected pieces and detects cycles.
أي العبارات التالية عن BFS صحيحة؟ حدد كل ما ينطبق.
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
يعدّ BFS الحواف، لذا يعطي المسارات الأقصر فقط عندما تكون تكلفة كل حافة متساوية.
Marks that slip away
- Connected is not complete. Connected means one piece; complete means every pair is joined.
- BFS on weighted graphs. BFS counts edges 边, not weights; a weighted shortest path needs another method.
- Ignoring degree-0 vertices. They still count in $|V|$.
You've got it
- $G = (V, E)$, stored one of two ways:
| Adjacency matrix 邻接矩阵 | Adjacency list | |
|---|---|---|
| Space | $n^2$ | $O(\lvert V\rvert + \lvert E\rvert)$ |
| "Edge $u \to v$?" | $O(1)$ | scan $u$'s list |
| Best for | dense graphs | sparse graphs |
- $\sum \deg = 2|E|$, so the number of odd-degree vertices is even.
- Tree: connected and acyclic, $|E| = |V| - 1$.
| BFS | DFS | |
|---|---|---|
| Uses | a queue | a stack or recursion |
| Visits | level by level | as deep as it can |
| Finds | shortest paths in edges | pieces and cycles |
ما المشكلة؟ لا يرى ذلك سوى فريق الموقع.