Question
Given a set x = {e 1 , e 2, ..., e n} and a collection of sets S 1 , S 2, ..., S
Given a set x = {e1, e2, ..., en} and a collection of sets S1, S2, ..., Sn. Each element ei has a corresponding set Si = {ei}.
You are now given a sequence of commands of type Union(ex , ey ) interspersed with commands Query(ex , ey ). The union operation takes the two sets one containing ex , and the other containing ey (they might be the same set, but not at the beginning) and union it to a single set containing the elements of both. As we go creating sets we query command
asks whether the two elements in its argument NOW (at the time you reached it in the sequence) belong to the same set or a different set.
(a) Given a list of O(n) operations of Union and Query. Design an algorithm that processes
the sequence of commands in O(n log n) time. To process the sequence means to answer all
the queries in the sequence. (Hint: keep a set as a rooted tree with depth that is O(log n).
The Crux is how to union two trees and keep this property.)
(b) Given an undirected weighted graph G = (V, E). Show an implementation of Kruskals Minimum
Spanning Tree(MST) algorithm by using the Union and Query commands. Your implementation
should have O(|E| log |V|) time complexity. (Hint: In Kruskals algorithm, it
builds the MST by processing every edge in G and asks whether two endpoints of an edge
belong to two disjoint trees. If so, the algorithm adds the edge making two disjoint trees
into a single tree. )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started