Kruskal's Algorithm: Difference between revisions
From Rice Wiki
No edit summary |
|||
Line 1: | Line 1: | ||
{{Infobox Algorithm|runtime=Sort + E cycle? + (V - 1) adding-edge|class=[[Minimum Spanning Tree]], [[Greedy Algorithm]]}} | {{Infobox Algorithm|runtime=Sort + E cycle? + (V - 1) adding-edge|class=[[Minimum Spanning Tree]], [[Greedy Algorithm]]}} | ||
= Approach: Greedy = | |||
The approach is to try to add the smallest edges as long as they do not create a cycle; add an edge to the tree that is minimum across the cut of <math>T</math> vs. <math>V - T</math> | |||
Given the MST of <math>V_{n - m} = v_1, v_2, \ldots, v_{ n - m} </math>, the MST of <math>V</math> should be that of <math>V_{n-1}</math> plus the edge that connects to <math>v_n</math> that is the shortest. | |||
<math> | |||
OPT(n) = OPT(n-1) + min( (v_i, v_n) \in E ) | |||
</math> | |||
= Analysis = | = Analysis = |
Revision as of 17:42, 20 March 2024
Approach: Greedy
The approach is to try to add the smallest edges as long as they do not create a cycle; add an edge to the tree that is minimum across the cut of vs.
Given the MST of , the MST of should be that of plus the edge that connects to that is the shortest.
Analysis
Sort edges + E (cycle?) + (V - 1) adding edge
Sorting takes E log E
For weighted disjoint set, checking cycle takes log V, and adding edge takes log V
For fast-find, where all members have the same ID, fast-set-id needs O(1) and union needs O(n)