Share kruskal algorithm c++

greenostrich778

New member
## Thuật toán Kruskal trong C ++

Thuật toán Kruskal là một thuật toán tham lam tìm thấy một cây bao trùm tối thiểu trong một biểu đồ không có trọng số.Một cây bao trùm tối thiểu là một sơ đồ con của biểu đồ kết nối tất cả các đỉnh với tổng trọng lượng tối thiểu có thể.Thuật toán Kruskal hoạt động bằng cách liên tục thêm các cạnh vào cây bao trùm, bắt đầu với các cạnh với trọng lượng thấp nhất.Mỗi cạnh được thêm vào cây kéo dài miễn là nó không tạo ra một chu kỳ.Thuật toán chấm dứt khi tất cả các đỉnh đã được kết nối bởi các cạnh trong cây bao trùm.

Thuật toán Kruskal là một thuật toán đơn giản và hiệu quả để tìm một cây bao trùm tối thiểu.Nó có độ phức tạp về thời gian của O (E log v), trong đó E là số lượng các cạnh trong biểu đồ và V là số lượng các đỉnh trong biểu đồ.

Sau đây là việc triển khai thuật toán Kruskal trong C ++:

`` `C ++
#include <Istream>
#include <Vector>

sử dụng không gian tên STD;

// một cấu trúc để thể hiện một cạnh có trọng số
Struct Edge {
int u;// Nguồn đỉnh của cạnh
trong TV;// đỉnh điểm đến của cạnh
trọng lượng int;// trọng lượng của cạnh
};

// một chức năng để so sánh hai cạnh với trọng lượng của chúng
Bool So sánh (Const Edge & E1, Const Edge & E2) {
trả về e1. weight <e2. weight;
}

// một chức năng để tìm một cây bao trùm tối thiểu trong một biểu đồ không được cân nhắc có trọng số
Vector <Gart> Kruskal (Vector <Vector <Edge >> & Graph) {
// Tạo một vectơ để lưu trữ các cạnh trong cây bao trùm tối thiểu
Vector <Ged> MST;

// Tạo cấu trúc dữ liệu phân tách để theo dõi các thành phần được kết nối trong biểu đồ
vector <int> Parent (graph.size ());
for (int i = 0; i <graph.size (); i ++) {
cha mẹ = i;
}

// Sắp xếp các cạnh trong biểu đồ theo trọng số của chúng
Sắp xếp (graph.begin (), graph.end (), so sánh);

// lặp lại các cạnh trong biểu đồ
for (int i = 0; i <graph.size (); i ++) {
// Nhận cạnh hiện tại
Cạnh cạnh = đồ thị ;

// Nhận các đỉnh nguồn và đích của cạnh
int u = edge.u;
int v = edge.v;

// Kiểm tra xem các đỉnh có ở các thành phần được kết nối khác nhau không
if (find (cha, u)! = find (cha, v)) {
// Các đỉnh nằm trong các thành phần được kết nối khác nhau, vì vậy chúng ta có thể thêm cạnh vào cây bao trùm tối thiểu
mst.push_back (cạnh);

// hợp nhất hai thành phần được kết nối
Liên minh (cha mẹ, u, v);
}
}

// Trả lại cây bao trùm tối thiểu
trả lại MST;
}

// một hàm để tìm gốc của đỉnh trong cấu trúc dữ liệu phân tách
int find (vector <int> & Parent, int u) {
if (cha mẹ ! = u) {
cha mẹ = tìm (cha, cha mẹ );
}

trả lại cha mẹ ;
}

// một hàm để hợp nhất hai thành phần được kết nối trong cấu trúc dữ liệu phân tách
Void Union (Vector <Int> & Parent, Int U, Int V) {
int rootu = find (cha, u);
int rootv = find (cha, v);

cha mẹ [rootu] = rootv;
}

int main () {
// Tạo biểu đồ
vector <vector <edge >> graph = {
{{0, 1, 4}, {0, 2, 6}, {1, 2, 3}},
{{2, 3, 8}, {3, 4, 7}}
};

// Tìm cây bao trùm tối thiểu trong biểu đồ
vector <gart> mst = kruskal (đồ thị);
=======================================
## Kruskal Algorithm in C++

The Kruskal algorithm is a greedy algorithm that finds a minimum spanning tree in a weighted undirected graph. A minimum spanning tree is a subgraph of the graph that connects all of the vertices with the minimum possible total weight. The Kruskal algorithm works by repeatedly adding edges to the spanning tree, starting with the edges with the lowest weights. Each edge is added to the spanning tree as long as it does not create a cycle. The algorithm terminates when all of the vertices have been connected by edges in the spanning tree.

The Kruskal algorithm is a simple and efficient algorithm for finding a minimum spanning tree. It has a time complexity of O(E log V), where E is the number of edges in the graph and V is the number of vertices in the graph.

The following is an implementation of the Kruskal algorithm in C++:

```c++
#include <iostream>
#include <vector>

using namespace std;

// A structure to represent a weighted edge
struct Edge {
int u; // The source vertex of the edge
int v; // The destination vertex of the edge
int weight; // The weight of the edge
};

// A function to compare two edges by their weights
bool compare(const Edge &e1, const Edge &e2) {
return e1.weight < e2.weight;
}

// A function to find a minimum spanning tree in a weighted undirected graph
vector<Edge> kruskal(vector<vector<Edge>> &graph) {
// Create a vector to store the edges in the minimum spanning tree
vector<Edge> mst;

// Create a disjoint-set data structure to keep track of the connected components in the graph
vector<int> parent(graph.size());
for (int i = 0; i < graph.size(); i++) {
parent = i;
}

// Sort the edges in the graph by their weights
sort(graph.begin(), graph.end(), compare);

// Iterate over the edges in the graph
for (int i = 0; i < graph.size(); i++) {
// Get the current edge
Edge edge = graph;

// Get the source and destination vertices of the edge
int u = edge.u;
int v = edge.v;

// Check if the vertices are in different connected components
if (find(parent, u) != find(parent, v)) {
// The vertices are in different connected components, so we can add the edge to the minimum spanning tree
mst.push_back(edge);

// Merge the two connected components
union(parent, u, v);
}
}

// Return the minimum spanning tree
return mst;
}

// A function to find the root of a vertex in a disjoint-set data structure
int find(vector<int> &parent, int u) {
if (parent != u) {
parent = find(parent, parent);
}

return parent;
}

// A function to merge two connected components in a disjoint-set data structure
void union(vector<int> &parent, int u, int v) {
int rootU = find(parent, u);
int rootV = find(parent, v);

parent[rootU] = rootV;
}

int main() {
// Create a graph
vector<vector<Edge>> graph = {
{ { 0, 1, 4 }, { 0, 2, 6 }, { 1, 2, 3 } },
{ { 2, 3, 8 }, { 3, 4, 7 } }
};

// Find the minimum spanning tree in the graph
vector<Edge> mst = kruskal(graph);
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top