Download Kruskal's Algorithm Assignment - Data Structures and Complexity Analysis - Prof. Douglas R and more Assignments Mathematics in PDF only on Docsity! MthSc 816 — Assignment #2 (Kruskal’s Algorithm) Data Structures from, to, cost: arrays for edge information O(3m) heap: array of edge indices O(m) pred, rank: node length arrays for union/find O(2n) mst: indices of edges in MST [optional] O(n) Space complexity is O(4m + 3n), if G has n nodes and m edges; or O(4m + 2n), 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 logd m) root1 = findroot(from(edge)) O(mα) root2 = findroot(to(edge)) O(mα) if root1 ≠ root2 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 logd 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 logd 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