Comp 251: Assignment 2 Solution

$24.99 $18.99

Exercise 1 (Disjoint sets (24 points)) We want to implement a disjoint set data structure with union and find operations. The template for this program is available on the course website and named DisjointSets.java. In this question, we model a partition of n elements with distinct integers ranging from 0 to n 1 (i.e. each…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:

Description

5/5 – (2 votes)

Exercise 1 (Disjoint sets (24 points)) We want to implement a disjoint set data structure with union and find operations. The template for this program is available on the course website and named DisjointSets.java.

In this question, we model a partition of n elements with distinct integers ranging from 0 to n 1 (i.e. each element is represented by an integer in [0; ; n 1], and each integer in [0; ; n 1] represent one element). We choose to represent the disjoint sets with trees, and to implement the forest of trees with an array named par. More precisely, the value stored in par[i] is parent of the element i, and par[i]==i when i is the root of the tree and thus the representative of the disjoint set.

You will implement union by rank and the path compression technique seen in class. The rank is an integer associated with each node. Initially (i.e. when the set contains one single object) its value is 0. Union operations link the root of the tree with smaller rank to the root of the tree with larger rank. In the case where the rank of both trees is the same, the rank of the new root increases by 1. You can implement the rank with a specific array (called rank) that has been added to the template, or use the array par (this is tricky). Note that path compression does not change the rank of a node.

Download the file DisjointSets.java, and complete the methods find(int i) as well as union(int i, int j). The constructor takes one argument n (a strictly positive integer) that indicates the number of elements in the partition, and initializes it by assigning a separate set to each element.

The method find(int i) will return the representative of the disjoint set that contains i (do not forget to implement path compression here!). The method union(int i, int j) will merge the set with smaller rank (for instance i) in the disjoint set with larger rank (in that case j). In that case, the root of the tree containing i will become a child of the root of the tree containing j), and return the representative (as an integer) of the new merged set. Do not forget to update the ranks. In the case where the ranks are identical, you will merge i into j.

Once completed, compile and run the file DisjointSets.java. It should produce the output avail-able in the file unionfind.txt available on the course website. Note: You will need to complete this question to implement Exercise 2.

Exercise 2 (Minimum Spanning trees (24 points)) We will implement the Kruskal algorithm to cal-culate the minimum spanning tree (MST) of an undirected weighted graph. Here you will use the file DisjointSets.java completed in the previous question and two other files: WGraph.java and Kruskal.java available on the course website. You will need the classes DisjointSets and WGraph to execute Kruskal.java. Your role will be to complete two methods in the template Kruskal.java.

The file WGraph.java implements two classes WGraph and Edge. An Edge object stores all infor-mations about edges (i.e. the two vertices and the weight of the edge), which are used to build graphs. The class WGraph has two constructors WGraph() and WGraph(String file). The first one creates an empty graph and the second uses a file to initialize a graph. Graphs are encoded using the following format. The first line of this file is a single integer n that indicates the number of nodes in the graph. Each vertex is labelled with an number in [0; ; n 1], and each integer in [0; ; n 1] represents one and only one vertex. The following lines respect the syntax “n1 n2 w”, where n1 and n2 are integers representing the nodes connected by an edge, and w the weight of this edge. n1, n2, and w must be separated by space(s). An example of such file can be found on the course website with the file g1.txt. These files will be used as an input in the program Kruskal.java to initialize the graphs. Thus, an example of a command line is java Kruskal g1.txt.

The class WGraph also provide a method addEdge(Edge e) that adds an edge to a graph (i.e. an object of this class). Another method listOfEdgesSorted() allows you to access the list of edges

5

Comp 251: Assignment 2 Solution
$24.99 $18.99