Bài giải thích miễn phí · Video · Code · Trắc nghiệm
Thuật toán Kruskal
Dựng cây khung nhỏ nhất bằng cách duyệt cạnh từ nhẹ đến nặng và giữ lại mọi cạnh không tạo chu trình, kiểm tra bằng union-find. Có rừng khung, cách phá hòa và lý do bước sắp xếp chiếm phần lớn chi phí.
- Độ khó
- Trung bình
- Thời gian
- O(E log E)
- Bộ nhớ
- O(V + E)
Tự tay chạy thử
Chạy từng bước theo nhịp của bạn — đây là Kruskal Animation, chạy ngay trên trang.
Đang chạyKruskal Animation
Đang tải bản xem trước…
Cách hoạt động
Ý tưởng, một ví dụ chạy thử, và những bẫy nên tránh.
Ý tưởng
Bạn cần nối một loạt ngôi làng bằng đường. Mỗi con đường có thể xây đều có giá riêng, và bạn muốn ai cũng đi được tới mọi nơi khác với tổng chi phí thấp nhất. Lời giải là cây khung nhỏ nhất (MST): tập cạnh nối được mọi đỉnh, không chứa chu trình và có tổng trọng số nhỏ nhất có thể.
Kruskal là lời giải tham lam cho bài này. Sắp xếp mọi cạnh từ rẻ đến đắt. Duyệt lần lượt, và xây con đường nào nối hai nơi chưa thông với nhau. Nếu hai đầu đã thông rồi thì con đường đó chỉ tạo thêm một vòng lặp, bỏ qua. Không bao giờ phải gỡ lại: cạnh nào đã giữ thì ở lại trong cây.
Câu hỏi khó duy nhất là "hai đầu này đã thông với nhau chưa?", và phải trả lời một lần cho mỗi cạnh. Đó chính là việc của tập hợp rời nhau (disjoint set, hay union-find). Mỗi đỉnh bắt đầu trong một nhóm riêng. union(u, v) gộp hai nhóm và trả về false nếu u và v vốn đã cùng nhóm. Gộp nhóm và đặt câu hỏi chỉ là một lời gọi.
Chạy thử từng bước
Đồ thị vô hướng có bảy đỉnh A đến G (đánh số 0 đến 6): A–B (7), A–D (5), B–C (8), B–D (9), B–E (7), C–E (5), D–E (15), D–F (6), E–F (8), E–G (9), F–G (11).
Trước hết sắp xếp theo trọng số. Các cạnh bằng nhau giữ nguyên thứ tự đầu vào: A–D, C–E (5), D–F (6), A–B, B–E (7), B–C, E–F (8), B–D, E–G (9), F–G (11), D–E (15). Rồi:
| Bước | Cạnh | Nhóm trước đó | Quyết định |
|---|---|---|---|
| 1 | A–D (5) | A, D chưa thông | Giữ. {A, D} |
| 2 | C–E (5) | C, E chưa thông | Giữ. {C, E} |
| 3 | D–F (6) | D, F chưa thông | Giữ. {A, D, F} |
| 4 | A–B (7) | A, B chưa thông | Giữ. {A, B, D, F} |
| 5 | B–E (7) | B, E khác nhóm | Giữ. {A, B, C, D, E, F} |
| 6 | B–C (8) | B, C đã thông | Bỏ: sẽ tạo chu trình |
| 7 | E–F (8) | E, F đã thông | Bỏ |
| 8 | B–D (9) | B, D đã thông | Bỏ |
| 9 | E–G (9) | E, G chưa thông | Giữ. Chỉ còn một nhóm |
Tổng trọng số: 5 + 5 + 6 + 7 + 7 + 9 = 39. Hai cạnh F–G (11) và D–E (15) không bao giờ được xét tới. Khi chỉ còn một nhóm, mọi cạnh còn lại đều sẽ tạo chu trình, nên vòng lặp dừng sớm. Cây trên V đỉnh có đúng V − 1 cạnh, ở đây là sáu.
Tập hợp rời nhau
Tập này là một rừng các cây, gốc của mỗi cây là tên của nhóm. find(x) leo từ x lên gốc. Hai đỉnh thông nhau đúng khi gốc của chúng bằng nhau. Hai mẹo nhỏ giữ cho các cây gần như phẳng:
- Gộp theo hạng (union by rank).
ranklà cận trên của chiều cao cây. Khi gộp hai nhóm, cây thấp hơn được treo dưới gốc của cây cao hơn, nên chiều cao chỉ tăng khi hai cây cao bằng nhau gặp nhau. - Nén đường đi (path compression). Trong lúc
findleo lên, nó cho mọi đỉnh đi qua trỏ thẳng về gốc, nên lầnfindsau từ các đỉnh đó chỉ mất một bước.
Kết hợp lại, mỗi thao tác tốn O(α(V)) khấu hao, với α là nghịch đảo hàm Ackermann. Nó không bao giờ vượt quá 4 với mọi đầu vào nhét vừa trong máy tính, cứ coi như hằng số. Trang union-find của site nói riêng về cấu trúc này.
Vì sao nó đúng
Khẳng định: nếu Kruskal giữ một cạnh, thì có một cây khung nhỏ nhất chứa cạnh đó cùng mọi cạnh đã giữ trước đó.
Giả sử cạnh e nối hai nhóm khác nhau, và lấy nhóm S của một đầu. Mọi cạnh rời S mà nhẹ hơn e đã được xét từ trước, và nó đã nối S với một nhóm khác. Không có cạnh nào như vậy, nên e là một cạnh nhẹ nhất rời S. Lấy một MST T bất kỳ chứa các cạnh đã giữ. Nếu e không thuộc T, thêm nó vào sẽ tạo một chu trình, và chu trình đó phải đi ra khỏi S qua một cạnh f khác. f không nhẹ hơn e, nên đổi f lấy e cho một cây không đắt hơn, mà vẫn chứa mọi cạnh đã giữ cùng e. Theo quy nạp, cây hoàn chỉnh là nhỏ nhất.
Lập luận tương tự cho cạnh bị bỏ: cạnh tạo chu trình không nhẹ hơn bất kỳ cạnh nào khác trên chu trình đó (chúng đều đã được xét trước), và một cạnh nặng nhất của chu trình luôn có thể bỏ khỏi MST.
Đồ thị không liên thông, cạnh bằng nhau và cạnh lạ
- Đồ thị không liên thông. Không có cây nào bao trùm tất cả, nên listing trả về rừng khung nhỏ nhất: một MST cho mỗi mảnh liên thông.
componentscho biết có bao nhiêu mảnh, và rừng có V −componentscạnh. Khicomponentsbằng 1, đồ thị liên thông và rừng chính là một MST thật. - Trọng số bằng nhau. MST thường không duy nhất: tam giác có ba cạnh bằng nhau có tới ba cây. Dù vậy listing vẫn tất định: sắp xếp ổn định, cạnh bằng nhau giữ thứ tự đầu vào và cạnh đứng trước thắng. Cả bốn ngôn ngữ in ra cùng các cạnh.
- Khuyên (self-loop) và cạnh song song. Khuyên
u–uluôn thấyuđã thông với chính nó nên bị bỏ. Trong hai cạnh song song, cạnh nhẹ hơn được xét trước. - Trọng số âm. Không sao cả. Chỉ thứ tự quan trọng, dấu thì không. Đây là điểm mạnh thật sự so với Dijkstra.
- Một đỉnh, không có cạnh. Kết quả là cây rỗng nặng 0,
componentsbằng 1 (hoặc bằng số đỉnh, khi có nhiều đỉnh mà không có cạnh nào).
Độ phức tạp
Sắp xếp E cạnh tốn O(E log E). Vòng lặp sau đó gọi union tối đa E lần, mỗi lần O(α(V)), tổng cộng O(E α(V)). Sắp xếp chiếm phần lớn nên toàn thuật toán là O(E log E). Vì E ≤ V², log E ≤ 2 log V, nên nó cũng chính là O(E log V). Bộ nhớ là O(V + E): bản sao đã sắp xếp của các cạnh, tập rời nhau và cây kết quả.
Nếu các cạnh đã được sắp sẵn (hoặc trọng số là số nguyên nhỏ và bạn dùng được counting sort), chi phí giảm xuống O(E α(V)), gần như tuyến tính.
Lỗi thường gặp
- Kiểm tra thông nhau bằng cha thay vì gốc.
parent[u] == parent[v]không có nghĩa là cùng nhóm. Hãy sofind(u)vớifind(v). - Quên rằng
sortcó thể không ổn định, hoặc sắp số như chuỗi. Khi đó các cạnh bằng nhau ra thứ tự khác nhau trên mỗi nền tảng, và cây cũng khác theo. Hãy dùng sắp xếp ổn định với hàm so sánh số. - Tràn số. Tổng các trọng số int có thể vượt 2³¹. Giữ nó trong
longhoặclong long. - Mặc định kết quả là cây. Nếu đồ thị không liên thông, bạn nhận một rừng. Hãy kiểm tra
components. - Đồ thị có hướng. MST là bài toán của đồ thị vô hướng. Với đồ thị có hướng, bài tương đương là cây có hướng nhỏ nhất (Chu-Liu/Edmonds), và Kruskal không giải được.
- Đỉnh ngoài khoảng. Cạnh nhắc tới đỉnh ngoài 0..V−1 sẽ truy cập ngoài mảng trong tập rời nhau. Hãy kiểm tra đầu vào trước.
Khi nào dùng (và khi nào không)
Dùng khi đồ thị đến dưới dạng danh sách cạnh và thưa: đi cáp mạng, phân cụm (dừng ngay khi còn k nhóm để có k cụm), lời giải xấp xỉ cho bài người bán hàng rong, phân đoạn ảnh, sinh mê cung. Nó cũng là lựa chọn tự nhiên khi bạn vốn cần các cạnh theo thứ tự trọng số.
Thuật toán Prim lớn dần một cây từ một đỉnh xuất phát bằng hàng đợi ưu tiên. Nó thường tốt hơn trên đồ thị dày lưu bằng ma trận kề, nơi nó chạy O(V²) mà không cần sắp xếp. Cả hai đều cho tổng trọng số nhỏ nhất.
Các biến thể hay gặp khi phỏng vấn
- Nối các thành phố với chi phí nhỏ nhất. Chạy Kruskal. Nếu giữ được ít hơn V − 1 cạnh, trả về −1: đồ thị không thể liên thông.
- Cây khung lớn nhất. Sắp xếp cạnh từ nặng đến nhẹ, hoặc đổi dấu mọi trọng số, rồi chạy cùng vòng lặp.
- MST thứ hai, hoặc "cạnh này có nằm trong mọi MST không?". Dựng MST, rồi với mỗi cạnh ngoài cây, xem cạnh nặng nhất trên đường đi trong cây giữa hai đầu của nó. Một cạnh thuộc mọi MST chỉ khi không cạnh nào khác thay thế được nó.
- Chi phí nhỏ nhất để nối các điểm (điểm là đỉnh, khoảng cách là trọng số). Sinh mọi cặp rồi chạy Kruskal, hoặc dùng Prim để khỏi phải sắp xếp O(V²) cạnh.
- Cạnh tới hạn và giả tới hạn. Với mỗi cạnh, chạy Kruskal một lần bắt buộc có nó và một lần cấm nó, rồi so trọng số với MST gốc.
Code
Cùng một thuật toán, từng bước như nhau, bằng 4 ngôn ngữ.
// Kruskal's algorithm: the minimum spanning tree of a weighted, undirected graph — the cheapest set
// of edges that keeps every vertex connected. Look at the edges from lightest to heaviest and keep an
// edge unless it closes a cycle among the edges already kept. A disjoint set answers "would it?"
// in near-constant time. On a graph that is not connected the result is a spanning forest: one
// minimum tree per connected piece.
/** @typedef {[u: number, v: number, weight: number]} Edge an undirected edge between u and v (the weight may be negative) */
/**
* A disjoint set (union-find) over the vertices 0..n-1: every vertex starts in a group of its own,
* and groups only ever merge. Each group is a tree whose root is its name.
*/
class DisjointSet {
/** parent[x]: the next vertex up x's tree. A root is its own parent. */
#parent;
/** rank[r]: an upper bound on the height of the tree rooted at r (only read at roots). */
#rank;
/** How many separate groups there are right now. */
#count;
/** @param {number} n the number of vertices */
constructor(n) {
this.#parent = Array.from({ length: n }, (_, x) => x);
this.#rank = new Array(n).fill(0);
this.#count = n;
}
/** The number of separate groups. */
get count() {
return this.#count;
}
/**
* The root of x's tree. Two vertices are in the same group exactly when their roots are equal.
* @param {number} x
*/
find(x) {
let root = x;
while (this.#parent[root] !== root) root = this.#parent[root];
// Path compression: point every vertex on the way straight at the root.
let node = x;
while (this.#parent[node] !== root) {
const next = this.#parent[node];
this.#parent[node] = root;
node = next;
}
return root;
}
/**
* Merges the groups of a and b.
* @param {number} a
* @param {number} b
* @returns {boolean} true if two groups became one, false if a and b were already together
*/
union(a, b) {
let high = this.find(a);
let low = this.find(b);
if (high === low) return false;
// Union by rank: hang the shorter tree under the root of the taller one. On a tie, b's root
// goes under a's root and the new root grows one level, so the result is deterministic.
if (this.#rank[high] < this.#rank[low]) [high, low] = [low, high];
this.#parent[low] = high;
if (this.#rank[high] === this.#rank[low]) this.#rank[high]++;
this.#count--;
return true;
}
}
/**
* Orders edges by weight. `Array.prototype.sort` is stable, so edges of equal weight stay in the
* order they were given — that is the tie-break, and it keeps the result deterministic.
* @param {Edge} a
* @param {Edge} b
*/
function byWeight(a, b) {
return a[2] - b[2];
}
/**
* The minimum spanning forest of an undirected graph.
*
* @param {number} vertexCount the number of vertices; they are named 0..vertexCount-1
* @param {Edge[]} edges the edges, in any order. Self-loops and parallel edges are fine.
* @returns {{ tree: Edge[], weight: number, components: number }}
* tree: the kept edges, in the order they were added;
* weight: their total weight;
* components: how many connected pieces the graph has (1 means `tree` spans every vertex)
*/
export function kruskal(vertexCount, edges) {
const groups = new DisjointSet(vertexCount);
const tree = [];
let weight = 0;
for (const edge of [...edges].sort(byWeight)) {
if (groups.count === 1) break; // already one tree: every edge left would close a cycle
const [u, v, w] = edge;
// The union both asks the question and acts on the answer: false means u and v were already
// connected, so this edge would close a cycle — skip it.
if (!groups.union(u, v)) continue;
tree.push(edge);
weight += w;
}
return { tree, weight, components: groups.count };
}
# Kruskal's algorithm: the minimum spanning tree of a weighted, undirected graph — the cheapest set
# of edges that keeps every vertex connected. Look at the edges from lightest to heaviest and keep an
# edge unless it closes a cycle among the edges already kept. A disjoint set answers "would it?"
# in near-constant time. On a graph that is not connected the result is a spanning forest: one
# minimum tree per connected piece.
from typing import List, Tuple
# An undirected edge (u, v, weight) between u and v. The weight may be negative.
Edge = Tuple[int, int, int]
class DisjointSet:
"""A disjoint set (union-find) over the vertices 0..n-1.
Every vertex starts in a group of its own, and groups only ever merge. Each group is a tree
whose root is its name.
"""
def __init__(self, n: int) -> None:
# _parent[x]: the next vertex up x's tree. A root is its own parent.
self._parent: List[int] = list(range(n))
# _rank[r]: an upper bound on the height of the tree rooted at r (only read at roots).
self._rank: List[int] = [0] * n
self._count = n
@property
def count(self) -> int:
"""The number of separate groups."""
return self._count
def find(self, x: int) -> int:
"""Return the root of x's tree. Two vertices are in the same group exactly when their roots are equal."""
root = x
while self._parent[root] != root:
root = self._parent[root]
# Path compression: point every vertex on the way straight at the root.
node = x
while self._parent[node] != root:
next_node = self._parent[node]
self._parent[node] = root
node = next_node
return root
def union(self, a: int, b: int) -> bool:
"""Merge the groups of a and b.
Return True if two groups became one, False if a and b were already together.
"""
high = self.find(a)
low = self.find(b)
if high == low:
return False
# Union by rank: hang the shorter tree under the root of the taller one. On a tie, b's root
# goes under a's root and the new root grows one level, so the result is deterministic.
if self._rank[high] < self._rank[low]:
high, low = low, high
self._parent[low] = high
if self._rank[high] == self._rank[low]:
self._rank[high] += 1
self._count -= 1
return True
def by_weight(edge: Edge) -> int:
"""The sort key. `sorted` is stable, so edges of equal weight stay in the order they were
given — that is the tie-break, and it keeps the result deterministic."""
return edge[2]
def kruskal(vertex_count: int, edges: List[Edge]) -> Tuple[List[Edge], int, int]:
"""Return (tree, weight, components) for the minimum spanning forest of an undirected graph.
vertex_count: the number of vertices; they are named 0..vertex_count-1.
edges: the edges, in any order. Self-loops and parallel edges are fine.
tree: the kept edges, in the order they were added.
weight: their total weight.
components: how many connected pieces the graph has (1 means `tree` spans every vertex).
"""
groups = DisjointSet(vertex_count)
tree: List[Edge] = []
weight = 0
for edge in sorted(edges, key=by_weight):
if groups.count == 1:
break # already one tree: every edge left would close a cycle
u, v, w = edge
# The union both asks the question and acts on the answer: False means u and v were already
# connected, so this edge would close a cycle — skip it.
if not groups.union(u, v):
continue
tree.append(edge)
weight += w
return tree, weight, groups.count
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
/**
* Kruskal's algorithm: the minimum spanning tree of a weighted, undirected graph — the cheapest set
* of edges that keeps every vertex connected. Look at the edges from lightest to heaviest and keep an
* edge unless it closes a cycle among the edges already kept. A disjoint set answers "would it?"
* in near-constant time. On a graph that is not connected the result is a spanning forest: one
* minimum tree per connected piece.
*/
public final class Kruskal {
/** An undirected edge between u and v. The weight may be negative. */
public record Edge(int u, int v, int weight) {
}
/**
* tree: the kept edges, in the order they were added.
* weight: their total weight.
* components: how many connected pieces the graph has (1 means the tree spans every vertex).
*/
public record Result(List<Edge> tree, long weight, int components) {
}
/** Orders edges by weight. {@code List.sort} is stable, so edges of equal weight stay in the
* order they were given — that is the tie-break, and it keeps the result deterministic. */
private static final Comparator<Edge> BY_WEIGHT = Comparator.comparingInt(Edge::weight);
private Kruskal() {
}
/**
* A disjoint set (union-find) over the vertices 0..n-1: every vertex starts in a group of its
* own, and groups only ever merge. Each group is a tree whose root is its name.
*/
private static final class DisjointSet {
/** parent[x]: the next vertex up x's tree. A root is its own parent. */
private final int[] parent;
/** rank[r]: an upper bound on the height of the tree rooted at r (only read at roots). */
private final int[] rank;
/** How many separate groups there are right now. */
private int count;
DisjointSet(int n) {
parent = new int[n];
for (int x = 0; x < n; x++) {
parent[x] = x;
}
rank = new int[n];
count = n;
}
int count() {
return count;
}
/** The root of x's tree. Two vertices are in the same group exactly when their roots are equal. */
int find(int x) {
int root = x;
while (parent[root] != root) {
root = parent[root];
}
// Path compression: point every vertex on the way straight at the root.
int node = x;
while (parent[node] != root) {
int next = parent[node];
parent[node] = root;
node = next;
}
return root;
}
/** Merges the groups of a and b. Returns true if two groups became one, false if a and b were already together. */
boolean union(int a, int b) {
int high = find(a);
int low = find(b);
if (high == low) {
return false;
}
// Union by rank: hang the shorter tree under the root of the taller one. On a tie, b's
// root goes under a's root and the new root grows one level, so the result is deterministic.
if (rank[high] < rank[low]) {
int swap = high;
high = low;
low = swap;
}
parent[low] = high;
if (rank[high] == rank[low]) {
rank[high]++;
}
count--;
return true;
}
}
/**
* The minimum spanning forest of an undirected graph with vertices 0..vertexCount-1.
* The edges may come in any order; self-loops and parallel edges are fine.
*/
public static Result kruskal(int vertexCount, List<Edge> edges) {
DisjointSet groups = new DisjointSet(vertexCount);
List<Edge> tree = new ArrayList<>();
long weight = 0; // long: sums of int weights can overflow int
List<Edge> sorted = new ArrayList<>(edges);
sorted.sort(BY_WEIGHT);
for (Edge edge : sorted) {
if (groups.count() == 1) {
break; // already one tree: every edge left would close a cycle
}
// The union both asks the question and acts on the answer: false means u and v were
// already connected, so this edge would close a cycle — skip it.
if (!groups.union(edge.u(), edge.v())) {
continue;
}
tree.add(edge);
weight += edge.weight();
}
return new Result(tree, weight, groups.count());
}
}
// Kruskal's algorithm: the minimum spanning tree of a weighted, undirected graph — the cheapest set
// of edges that keeps every vertex connected. Look at the edges from lightest to heaviest and keep an
// edge unless it closes a cycle among the edges already kept. A disjoint set answers "would it?"
// in near-constant time. On a graph that is not connected the result is a spanning forest: one
// minimum tree per connected piece.
#include <algorithm>
#include <numeric>
#include <utility>
#include <vector>
// An undirected edge between u and v. The weight may be negative.
struct Edge {
int u;
int v;
int weight;
};
// tree: the kept edges, in the order they were added.
// weight: their total weight.
// components: how many connected pieces the graph has (1 means the tree spans every vertex).
struct SpanningForest {
std::vector<Edge> tree;
long long weight;
int components;
};
// A disjoint set (union-find) over the vertices 0..n-1: every vertex starts in a group of its own,
// and groups only ever merge. Each group is a tree whose root is its name.
class DisjointSet {
public:
explicit DisjointSet(int n) : parent_(n), rank_(n, 0), count_(n) {
std::iota(parent_.begin(), parent_.end(), 0); // parent_[x] = x: everyone is a root
}
// The number of separate groups.
int count() const {
return count_;
}
// The root of x's tree. Two vertices are in the same group exactly when their roots are equal.
int find(int x) {
int root = x;
while (parent_[root] != root) {
root = parent_[root];
}
// Path compression: point every vertex on the way straight at the root.
int node = x;
while (parent_[node] != root) {
int next = parent_[node];
parent_[node] = root;
node = next;
}
return root;
}
// Merges the groups of a and b (named unite because `union` is a C++ keyword).
// Returns true if two groups became one, false if a and b were already together.
bool unite(int a, int b) {
int high = find(a);
int low = find(b);
if (high == low) {
return false;
}
// Union by rank: hang the shorter tree under the root of the taller one. On a tie, b's root
// goes under a's root and the new root grows one level, so the result is deterministic.
if (rank_[high] < rank_[low]) {
std::swap(high, low);
}
parent_[low] = high;
if (rank_[high] == rank_[low]) {
++rank_[high];
}
--count_;
return true;
}
private:
std::vector<int> parent_; // parent_[x]: the next vertex up x's tree; a root is its own parent
std::vector<int> rank_; // rank_[r]: an upper bound on the height of the tree rooted at r (only read at roots)
int count_; // how many separate groups there are right now
};
// Orders edges by weight. The sort is stable, so edges of equal weight stay in the order they were
// given — that is the tie-break, and it keeps the result deterministic.
static bool byWeight(const Edge& a, const Edge& b) {
return a.weight < b.weight;
}
// The minimum spanning forest of an undirected graph with vertices 0..vertexCount-1.
// The edges may come in any order; self-loops and parallel edges are fine.
SpanningForest kruskal(int vertexCount, std::vector<Edge> edges) {
DisjointSet groups(vertexCount);
SpanningForest result{{}, 0, vertexCount}; // long long: sums of int weights can overflow int
std::stable_sort(edges.begin(), edges.end(), byWeight);
for (const Edge& edge : edges) {
if (groups.count() == 1) {
break; // already one tree: every edge left would close a cycle
}
// The union both asks the question and acts on the answer: false means u and v were already
// connected, so this edge would close a cycle — skip it.
if (!groups.unite(edge.u, edge.v)) {
continue;
}
result.tree.push_back(edge);
result.weight += edge.weight;
}
result.components = groups.count();
return result;
}
Tự kiểm tra
10 câu hỏi nhanh xem bạn đã nắm chưa.
Câu 1 · Đồ thị vô hướng có các đỉnh từ 0 đến 6 và các cạnh sau (u–v, trọng số): 0–1 (7), 0–3 (5), 1–2 (8), 1–3 (9), 1–4 (7), 2–4 (5), 3–4 (15), 3–5 (6), 4–5 (8), 4–6 (9), 5–6 (11). weight bằng bao nhiêu sau kruskal(7, edges)?
Dùng ngay trong dự án
- Animation
Kruskal Animation
Đồ thị phát sáng, nơi mạng lưới rẻ nhất tự dựng lên: cây khung nhỏ nhất của Kruskal với danh sách cạnh tự sắp xếp và màu của union-find, cho bài giảng và slide
Miễn phí
