You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/1001-2000/1584-min-cost-to-connect-all-points-2.md
+30-14Lines changed: 30 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,43 +61,59 @@ This page, I will only talk about the solution of **Kruskal's Algorithm**.
61
61
- Traverse all edges once, add up the lengths of the edges and return the sum as the result.
62
62
- If you are familiar with the **Union-Find** algorithm, it is easy to solve the problem with _Kruskal's algorithm_. However, this problem does not directly give the `edges` information, and we need to calculate it through the vertex information, which is not difficult, but this causes the algorithm to run slower than _Prim's Algorithm_ because there are too many edges. The more edges, the slower _Kruskal's Algorithm_.
63
63
64
-
## Complexity
65
-
`E` is the `edges.length`.
64
+
#### Compare to Prim's Algorithm
65
+
For this problem, `points` are given, so using `Prim's Algorithm` would be faster and easier to understand.
66
+
67
+
Because if we use `Kruskal's Algorithm`, we have to convert `points` into `edges`, and we will get a fully dense graph.
66
68
67
-
`N` is the `points.length`.
69
+
The more edges there are, the worse the performance of `Kruskal's Algorithm` will be.
68
70
69
-
* Time: `O(E * logE)`.
70
-
* Space: `O(N * N)`.
71
+
For sparse graphs, `Kruskal's Algorithm` is much more efficient than `Prim's Algorithm`.
72
+
73
+
For problems of `Minimum Spanning Tree` which `edges` are given, we can give priority to using _Kruskal's Algorithm_.
74
+
75
+
## Complexity
76
+
> `V` is the `points.length`.
77
+
> `E` is the `edges.length`. In this problem, the `E` is `V * V`.
78
+
79
+
* Time: `O(E * log(E))`.
80
+
* Space: `O(V * V)`.
71
81
72
82
## Python
83
+
The following code can also be implemented by using `heap sort`, but it would be slower.
Copy file name to clipboardExpand all lines: zh/1001-2000/1584-min-cost-to-connect-all-points-2.md
+26-14Lines changed: 26 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,43 +61,55 @@ This page, I will only talk about the solution of **Kruskal's Algorithm**.
61
61
- Traverse all edges once, add up the lengths of the edges and return the sum as the result.
62
62
- If you are familiar with the **Union-Find** algorithm, it is easy to solve the problem with _Kruskal's algorithm_. However, this problem does not directly give the `edges` information, and we need to calculate it through the vertex information, which is not difficult, but this causes the algorithm to run slower than _Prim's Algorithm_ because there are too many edges. The more edges, the slower _Kruskal's Algorithm_.
63
63
64
-
## Complexity
65
-
`E` is the `edges.length`.
64
+
#### Compare to Prim's Algorithm
65
+
For this problem, `points` are given, so using `Prim's Algorithm` would be faster and easier to understand.
66
+
67
+
Because if we use `Kruskal's Algorithm`, we have to convert `points` into `edges`, and we will get a fully dense graph.
68
+
69
+
For sparse graphs, `Kruskal's Algorithm` is more efficient than `Prim's Algorithm`.
66
70
67
-
`N` is the `points.length`.
71
+
## Complexity
72
+
-`V` is the `points.length`.
73
+
-`E` is the `edges.length`. In this problem, the `E` is `V * V`.
68
74
69
-
* Time: `O(E * logE)`.
70
-
* Space: `O(N * N)`.
75
+
* Time: `O(E * log(E))`.
76
+
* Space: `O(V * V)`.
71
77
72
78
## Python
79
+
The following code can also be implemented by using `heap sort`, but it would be slower.
0 commit comments