← 返回资讯屏轩教育 · 行业资讯

Applying Prim's Algorithm to a Distance Matrix | 在距离矩阵上应用普里姆算法

2026-08-29 23:32 · 本文部分内容由AI辅助创作

📚 Applying Prim's Algorithm to a Distance Matrix | 在距离矩阵上应用普里姆算法

Prim's algorithm is a greedy algorithm that finds a minimum spanning tree (MST) for a weighted undirected graph. At A-Level, Edexcel frequently assesses your ability to apply Prim's algorithm directly to a distance matrix, rather than to a drawn graph. This skill tests both your understanding of the algorithm and your accuracy with tabular data.

普里姆算法是一种贪心算法,用于为带权无向图寻找最小生成树(MST)。在 A-Level Edexcel 考试中,经常直接考查你如何在距离矩阵上应用普里姆算法,而不是在图形上操作。这项技能同时检验你对算法的理解程度以及处理表格数据的准确性。


1. Understanding the Distance Matrix | 理解距离矩阵

A distance matrix is a square table where rows and columns represent vertices. Each cell stores the weight of the edge between the vertex in its row and the vertex in its column. Cells on the main diagonal are typically left blank or marked with a dash, because a vertex has no edge to itself.

距离矩阵是一个正方形表格,行和列分别代表顶点。每个单元格存储对应行顶点与列顶点之间边的权重。主对角线上的单元格通常留空或用短横线标记,因为顶点与自身之间不存在边。

Before starting the algorithm, always identify the full set of vertices and note whether the matrix is symmetric. This will help you avoid reading errors.

在开始算法之前,务必先确定完整的顶点集合,并注意矩阵是否对称。这样可以帮助你避免读取错误。


2. Prim's Algorithm: Core Principle | 普里姆算法:核心原理

Prim's algorithm builds a minimum spanning tree by growing a single tree from an arbitrary starting vertex. At each step, it selects the edge of smallest weight that connects a vertex already in the tree to a vertex not yet in the tree.

普里姆算法通过从一个任意起始顶点开始生长单棵树来构建最小生成树。在每一步中,它选择权重最小的边,该边连接已在树中的顶点与尚未在树中的顶点。

MST = 最小生成树,即连接所有顶点且总权重最小的树

When working with a distance matrix, the "tree" is represented by two sets: the set of visited vertices and the set of unvisited vertices. You repeatedly scan the rows of visited vertices to find the smallest entry pointing to an unvisited vertex.

在距离矩阵上操作时,"树"由两个集合表示:已访问顶点集合和未访问顶点集合。你反复扫描已访问顶点的所有行,找出指向未访问顶点的最小条目。


3. Standard Algorithm Steps for a Matrix | 矩阵上的标准算法步骤

The steps below are the examination-friendly version of Prim's algorithm applied to a distance matrix. Memorise them exactly, as the wording is often quoted in mark schemes.

以下是在距离矩阵上应用普里姆算法的考试友好版本步骤。请准确记忆它们,因为评分标准中经常引用这些措辞。


4. Worked Example: Setting Up | 完整示例:初始设定

Consider the following distance matrix for five vertices A, B, C, D and E. We will apply Prim's algorithm starting from vertex A.

考虑以下包含五个顶点 A、B、C、D、E 的距离矩阵。我们将从顶点 A 开始应用普里姆算法。

VertexABCDE
A-2571
B2-384
C53-69
D786-2
E1492-

Visited set initially: {A}. We will track the selected edges in order.

初始已访问集合:{A}。我们将按顺序记录所选边。


广告 / Sponsored

5. First Iteration: From the Start Vertex | 第一次迭代:从起始顶点出发

e: B=2, C=5, D=7, E=1. The smallest value is 1, which connects A to E. So we select edge A–E with weight 1.

只查看 A 行。值为:B=2、C=5、D=7、E=1。最小值为 1,连接 A 与 E。因此我们选择边 A–E,权重为 1。


6. Second Iteration: Scanning All Visited Rows | 第二次迭代:扫描所有已访问行

Now we examine rows A and E simultaneously. For each unvisited vertex, record the smallest value from either row:

现在同时查看 A 行和 E 行。对于每个未访问顶点,记录来自任一行的最小值:

The smallest candidate is 2. There is a tie between A–B and E–D. Either one may be chosen. We will select A–B (weight 2).

最小候选值为 2。A–B 与 E–D 并列。两者均可选择。我们选择 A–B(权重 2)。


7. Third and Fourth Iterations: Completing the Tree | 第三、四次迭代:完成树

Now scan rows A, E and B. For each unvisited vertex (C and D remains):

现在扫描 A、E、B 三行。对于每个未访问顶点(C 和 D 仍未访问):

The smallest is 2, so we select E–D. Visited set: {A, E, B, D}.

最小值为 2,因此我们选择 E–D。已访问集合:{A, E, B, D}。

For the final vertex C, scan rows A, E, B and D: A–C = 5, E–C = 9, B–C = 3, D–C = 6. The minimum is 3, so we select B–C.

对于最后一个顶点 C,扫描 A、E、B、D 四行:A–C = 5,E–C = 9,B–C = 3,D–C = 6。最小值为 3,因此我们选择 B–C。


8. Recording Your Answer Correctly | 正确记录答案

In the exam, marks are awarded for the order in which edges are added, the weights, and the total. Always write edges in the order they were selected, and clearly state the total weight of the minimum spanning tree.

在考试中,得分点包括边加入的顺序、权重以及总权重。务必按选择顺序写出各条边,并清楚标明最小生成树的总权重。

Edges: A–E (1), A–B (2), E–D (2), B–C (3) → Total = 8

A common error is to report the edges without the total, or to list them out of order. Both will cost you marks on a six-mark question.

常见错误是只写边而不写总权重,或者没有按顺序列出边。这两种错误都会在 6 分题中让你失分。


9. Complexity and Choice of Start Vertex | 复杂度与起始顶点的选择

The time complexity of Prim's algorithm using a simple array-based implementation is O(n²), where n is the number of vertices. Using a binary heap priority queue improves this to O((n + m) log n), where m is the number of edges.

使用简单数组实现的普里姆算法时间复杂度为 O(n²),其中 n 为顶点数。使用二叉堆优先队列可将其改进为 O((n + m) log n),其中 m 为边数。


10. Common Mistakes and Exam Tips | 常见错误与考试提示

The following pitfalls appear frequently in Edexcel examiner reports. Avoid them to secure full marks.

以下陷阱在 Edexcel 考官报告中频繁出现。避免它们以获得满分。


Published by TutorHao | Computer Science Revision Series | aleveler.com

广告 / Sponsored
现货 Cambridge International AS & ALevel IT Coursebook剑桥国际IGC…
计算机 备考教辅 · 正版现货 · 与本文内容相关
查看详情 / View on Taobao参考价 ¥467.0

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao