


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth analysis of kruskal's algorithm, including its data structures, space complexity, time complexity, and auxiliary routines. It also includes a comparison of d-heaps and empirical comparisons with different d values.
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Data Structures from, to, cost: arrays for edge information O(3 m ) heap: array of edge indices O( m ) pred, rank: node length arrays for union/find O(2 n ) mst: indices of edges in MST [optional] O( n ) Space complexity is O(4 m + 3 n ), if G has n nodes and m edges; or O(4 m + 2 n ), if don’t store the MST but print each edge as found. Initialize using heap[k] = k; pred[j] = j, rank[j] = 0 Overall Design readgraph O( m ) initialize O( n + m ) makeheap O( m ) while MSTedges < n – 1 () edge = deletemin O( m d log d m ) root1 = findroot(from(edge)) O( m α) root2 = findroot(to(edge)) O( m α) if root1 ≠ root update mst, mincost, MSTedges O( n ) union(root1,root2) O( n ) output MST O( n ) [ Could also check that heapsize > 0, if G not connected.] Time complexity is O( m d log d m ), dominated by deletemin operation.
Auxiliary Routines minchild( x ): returns index and minimum cost of smallest child of x if none returns minimum cost = ∞ can compute the location of the last child of x using min { dx + 1, heapsize} siftdown( x ): restores heap order if cost at position x is increased avoid pairwise swaps (instead, the vacant spot moves down) copy edge indices rather than edge records make sure it works when heapsize = 1 Code Issues all routines should be iterative — not recursive siftdown: avoid special cases by having minchild return ∞ if no children makeheap: begin siftdown at last node with a child, ⎡(heapsize – 1)/ d ⎤ deletemin: returns first element of heap; copy last element of heap into the first position; reduce heapsize by 1; siftdown if heap nonempty Comparison of d - Heaps Theoretical worst-case complexity (from deletemin) is m d log d m = m d (ln m / ln d ) = ( m ln m ) ( d / ln d ) Verify that (2 / ln 2) = (4 / ln 4) > (3 / ln 3), so in theory d = 3 should dominate in general. [Continuous solution at d = e = 2.71828] This assumes that each siftdown takes the maximum # of steps. Empirical Comparisons Run for each d value, measuring total elapsed time for Kruskal (excluding reading, printing). Average CPU time is total elapsed time /# repetitions. Problem #nodes #edges last edge used MST cost CPU time 1 40 366 366 744.92 longest 2 40 375 119 635.88 shortest 3 40 475 114 634.33 near shortest
FUNCTION siftdown(p) // start at position p in heap hold = heap[p] val = cost[hold] [child, childval] = minchild(p) while val > childval heap[p] = heap[child] p = child [child, childval] = minchild(p) endwhile heap[p] = hold