กราฟและต้นไม้
แนะนำใหม่ในบทเรียนนี้
| 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 |
| adjacency list/əˈdʒeɪsənsi lɪst/ | รายการ adjacency |
| sparse/spɑːs/ | Describing a graph with few edges compared with the number of vertices, so an adjacency list suits it. |
| handshake lemma/ˈhændʃeɪk ˈlemə/ | ทฤษฎีบทการจับมือ |
| tree/triː/ | ต้นไม้ |
| acyclic/əˈkaɪklɪk/ | ไม่มีวงวน |
| leaf/liːf/ | ใบไม้ |
| breadth-first search/bredθ fɜːst sɜːtʃ/ | การสำรวจแบบกว้างก่อน (BFS) |
| depth-first search/depθ fɜːst sɜːtʃ/ | การสำรวจแบบลึกก่อน (DFS) |
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 |
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้