Share kruskal c++,

truongvinhly

New member
#Kruskal, #C ++, #algorithms, #graph, #DatScature ## Kruskal's Tatchit trong C ++

Thuật toán của Kruskal là một thuật toán tham lam để tìm một cây bao trùm tối thiểu (MST) trong một biểu đồ 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 cùng với tổng trọng lượng tối thiểu có thể của các cạnh.Thuật toán của Kruskal hoạt động bằng cách lặp đi lặp lại các cạnh vào MST, 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 MST nếu 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 MST.

Thuật toán của Kruskal là một thuật toán rất hiệu quả và nó thường được sử dụng để tìm MST trong các biểu đồ lớn.Độ phức tạp về thời gian của thuật toán của Kruskal là O (E log v), trong đó E là số lượng các cạnh trong biểu đồ và V là số lượng đỉnh trong biểu đồ.

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

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

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

// Một cấu trúc để biểu thị một cạnh trong một biểu đồ.
Struct Edge {
int u;// đỉnh bắt đầu của cạnh.
trong TV;// đỉnh kết thúc của cạnh.
trọng lượng int;// trọng lượng của cạnh.
};

// một cấu trúc để biểu thị một biểu đồ.
Biểu đồ cấu trúc {
trong TV;// Số lượng các đỉnh trong biểu đồ.
Vector <Gart> cạnh;// Các cạnh trong biểu đồ.
};

// một chức năng để tìm cạnh trọng lượng tối thiểu trong một tập hợp các cạnh.
Edge findMinedge (Vector <gort> cạnh) {
// Sắp xếp các cạnh theo trọng lượng.
Sắp xếp (cạnh.begin (), cạnh.end (), [] (Edge E1, Edge E2) {
trả về e1. weight <e2. weight;
});

// Tìm cạnh đầu tiên không tạo ra một chu kỳ.
for (int i = 0; i <retges.size (); i ++) {
int u = cạnh .u;
int v = cạnh .v;

// Kiểm tra nếu cạnh tạo ra một chu kỳ.
if (! iscycle (u, v, cạnh)) {
Các cạnh trả lại ;
}
}

// Không tìm thấy cạnh nào không tạo ra một chu kỳ.
return cạnh ();
}

// Một chức năng để kiểm tra xem một cạnh có tạo ra một chu kỳ trong biểu đồ không.
bool iscycle (int u, int v, vector <gort> cạnh) {
// Tạo một bộ để theo dõi các đỉnh đã được truy cập.
Đặt <Int> đã truy cập;

// Đánh dấu đỉnh bắt đầu như đã truy cập.
đã truy cập.insert (u);

// Kiểm tra đệ quy xem đỉnh kết thúc có thể truy cập được từ đỉnh bắt đầu mà không tạo ra một chu kỳ.
trả lại dfs (u, v, cạnh, truy cập);
}

// Một hàm đệ quy để kiểm tra xem đỉnh có thể tiếp cận được từ các đỉnh khác mà không tạo một chu kỳ không.
bool dfs (int u, int v, vector <gort> cạnh, set <int> đã truy cập) {
// Nếu đỉnh kết thúc đã được truy cập, thì có một chu kỳ.
if (đã truy cập.Count (v)> 0) {
trả lại đúng;
}

// Đánh dấu đỉnh hiện tại như đã truy cập.
đã truy cập.insert (u);

// Kiểm tra đệ quy xem đỉnh kết thúc có thể đạt được từ bất kỳ đỉnh liền kề nào không.
for (int i = 0; i <retges.size (); i ++) {
int w = cạnh .v;

if (w! = u && dfs (w, v, cạnh, truy cập)) {
trả lại đúng;
}
}

// đỉnh kết thúc không thể truy cập được từ đỉnh bắt đầu mà không tạo ra một chu kỳ.
trả lại sai;
}

// một chức năng để tìm cây bao trùm tối thiểu trong biểu đồ.
Đồ thị findmst (đồ thị
=======================================
#Kruskal, #C++, #algorithms, #graph, #datastructures ## Kruskal's Algorithm in C++

Kruskal's algorithm is a greedy algorithm for finding a minimum spanning tree (MST) in a weighted graph. A minimum spanning tree is a subgraph of the graph that connects all of the vertices together with the minimum possible total weight of edges. Kruskal's algorithm works by iteratively adding edges to the MST, starting with the edges with the lowest weights. Each edge is added to the MST if it does not create a cycle. The algorithm terminates when all of the vertices have been connected by edges in the MST.

Kruskal's algorithm is a very efficient algorithm, and it is often used to find MSTs in large graphs. The time complexity of Kruskal's algorithm is 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 Kruskal's algorithm in C++:

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

using namespace std;

// A struct to represent an edge in a graph.
struct Edge {
int u; // The starting vertex of the edge.
int v; // The ending vertex of the edge.
int weight; // The weight of the edge.
};

// A struct to represent a graph.
struct Graph {
int V; // The number of vertices in the graph.
vector<Edge> edges; // The edges in the graph.
};

// A function to find the minimum weight edge in a set of edges.
Edge findMinEdge(vector<Edge> edges) {
// Sort the edges by weight.
sort(edges.begin(), edges.end(), [](Edge e1, Edge e2) {
return e1.weight < e2.weight;
});

// Find the first edge that does not create a cycle.
for (int i = 0; i < edges.size(); i++) {
int u = edges.u;
int v = edges.v;

// Check if the edge creates a cycle.
if (!isCycle(u, v, edges)) {
return edges;
}
}

// No edge was found that does not create a cycle.
return Edge();
}

// A function to check if an edge creates a cycle in a graph.
bool isCycle(int u, int v, vector<Edge> edges) {
// Create a set to keep track of the vertices that have already been visited.
set<int> visited;

// Mark the starting vertex as visited.
visited.insert(u);

// Recursively check if the ending vertex is reachable from the starting vertex without creating a cycle.
return dfs(u, v, edges, visited);
}

// A recursive function to check if a vertex is reachable from another vertex without creating a cycle.
bool dfs(int u, int v, vector<Edge> edges, set<int> visited) {
// If the ending vertex has already been visited, then there is a cycle.
if (visited.count(v) > 0) {
return true;
}

// Mark the current vertex as visited.
visited.insert(u);

// Recursively check if the ending vertex is reachable from any of the adjacent vertices.
for (int i = 0; i < edges.size(); i++) {
int w = edges.v;

if (w != u && dfs(w, v, edges, visited)) {
return true;
}
}

// The ending vertex is not reachable from the starting vertex without creating a cycle.
return false;
}

// A function to find the minimum spanning tree in a graph.
Graph findMST(Graph
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top