Lecture 6: Kruskal’s runtime
Kruskal’s Algorithm: Runtime:
m is the number of edges, n is the number of vertices.
- Sort edges: O(m log m) -> O(m log n) because log m <= log n^2 = 2 log n
- Need find(i) which returns the name of the set that contains i, and union(i,j) which combines the sets i and j.
Method 1:
Sets are linked lists with head, tail pointers. Find() takes O(n) time, while union() can be performed in O(1). You can also improve the runtime by adding name pointers to each list element and sizes to the lists.
Method 2:
Use trees formed from just parent pointers. Build an array called “parent.” Then, union() runs in O(1) and find() in O(n). Adding a size column to the array will reduce the find() time to O(log n). Make sure you always attach the smaller tree to the larger one in your union method.
“Given trees and weighted union, tree height is O(log n).”
Every time a node changes its tree, its new tree is >= twice the size of its old tree. The deepest node in a tree can double its tree size O(log n) times.
Runtime: m edges * (O(1) find + O(log n) union) = O(m log n).
Additional optimizations:
Change every pointer along the path to root to directly point to the root, simplifying future calls to find(). This is called “compressing finds.” Also, weighted unions.
A series of n union-find operations can be done in time O(n alpha(n)). Alpha(n) is the inverse Ackermann’s function, which about 4 or 5 in normal practice.
Ackermann’s Function:
F(1,0) = 2
F(2,0) = 0
F(x,0) = 1 for all x > 2
F(0,y) = y + 1 for all y > 2
F(x,y) = F(x-1, F(x, y-1))
For example, F(1,y) = F(0, F(1, y-1)) = 1 + F(1, y-1) = … = y + F(1,0) = y + 2. Also, F(2,y) = 2 * y. F(3,y) = 2 ^ y, and more surprisingly, F(4,y) = 2^2^2^…^2 y times.
Inverse Ackermann’s Function:
Alpha(n) = min {i | F(i,i) >= n }
For any representable number in a computer, F(5,5) is far bigger. All the bounds we gave as O(n alpha(n)) are actually Theta, a tight bound.
This entry was posted on Friday, February 4th, 2005 at 12:30 am and is tagged with kruskal s algorithm, parent pointers, size column, n times, ackermann, node, pointer, array, lt, unions, element, trees. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.
2 Responses to 'Lecture 6: Kruskal’s runtime'
Leave a Reply
Please take time to enjoy the archives: May 2005 (1) April 2005 (11) March 2005 (11) February 2005 (15) January 2005 (7)
Fresh, related resources:
- There were no results.
on May 14th, 2005 at 9:42 pm
[…] , lecture 5 proves the same for Prim’s algorithm, and lecture 6 carefully proves the time complexity of Kruskal’s algorithm. Two intereseting MST […]
on May 9th, 2008 at 3:27 am
6e1377fcf8b1…
6e1377fcf8b1fabb62ab…