Program 6 Union Find


Rate this product

Program 6 – 20 points
Union Find
Part 1: Union Find
Write the code to perform union find (using path compression) on a set of 121 (or more) elements. Use a smart union (by size). Create a series of carefully constructed tests so that you can verify union/find is working properly and that path compression works. Print out the contents of your array.
Everyone’s tests will be different. You will be graded on how well your tests demonstrate proper functioning. This ability to test your code (and dig into the details) is critical.
The Union/Find you create should work for any problem with integer items, and not be coded to only work for this assignment.
Part 2: Hex The game of Hex is played on a grid of hexagons. Hex is a strategy board game for two players played on a grid, theoretically of any size and several possible shapes, but traditionally as an 11×11 rhombus. Players (red and blue) alternate placing markers or stones on unoccupied spaces in an attempt to link their opposite sides of the board in an unbroken chain. In the figure below, the blue player is attempting to link the two blue edges, while the red player is attempting to link the red edges. Write the code to detect when red or blue has won the game. Use the union-find data structure. We will assume that the hexagons are numbered by rows, with the first row being 1-11, the next row being 12-22 and so on. With this numbering system, the neighbors of a non-edge node x are items x-1, x+1,x-11, x-10, x+10, x+11,

1. Keep track of which cells have been selected by each player.
2. Use the smart union find (with path compression) to keep track of the set of cells that are connected.
3. You will need a plan to decide how you can tell if the edges are linked. Our goal with union/find is to have an almost constant time operation. Thus, anything you add has to maintain this goal.

a. One way to do this would be to have extra elements in the union/find associated with the TOP, BOTTOM, LEFT, and RIGHT. For the red player, when any item on the top row is selected, union it with item TOP. When any item on the bottom row is selected, union it with item BOTTOM. The game is won for the red player when find(TOP)=find(BOTTOM).

b. Do not check to see if the boundaries are reached by seeing if any item in row 1 is in the same group as any item in row 11 as this would be very slow.
The input is a series of moves indicated by the board location selected. Assume that the blue player always goes first. Indicate who wins and how many moves were made. If a player attempts to select a cell that has already been played, flag this as an error. Test your code with the supplied input files.

Print out the board, who wins, and the number of moves required. You can print in color by
public static final String ANSI_RESET = “\u001B[0m”;
public static final String ANSI_BLACK = “\u001B[30m”;
public static final String ANSI_RED = “\u001B[31m”;
public static final String ANSI_GREEN = “\u001B[32m”;
public static final String ANSI_YELLOW = “\u001B[33m”;
public static final String ANSI_BLUE = “\u001B[34m”;
public static final String ANSI_PURPLE = “\u001B[35m”;
public static final String ANSI_CYAN = “\u001B[36m”;
public static final String ANSI_WHITE = “\u001B[37m”;

System.out.println(ANSI_RED + “This text is red!” + ANSI_RESET);

Hint: The awkward part is deciding who the neighbors of a node are. If a cell is in the center, the neighbors are found by adding the following offsets to item: int[] inc= {-11, -10, -1, 1, 10, 11}; These offsets correspond to neighbors in the following directions: NW,NE,W,E,SW,SE.
The tricky part is that the corners only have neighbors in two of the six direction. All other edges have neighbors in four directions, but each edge is different.
I ended up creating a getNeighbors method which returns only the legal neighbors. If they neighbor was at a border, I returned the border number as a neighbor
int RIGHT=122;
int LEFT = 123
int TOP=124;
int BOTTOM=125;

private int[] getNeighbors( int item, int player)

Output from this routine may look like:
Neighbors of 83 [72,73,82,84,93,94]
Neighbors of 121 [110,120,125] if red player
Neighbors of 62 [51,52,61,63,72,73]
Neighbors of 84 [73,74,83,85,94,95]
Neighbors of 112 [101,102,111,113] if blue player

Open chat
Need help?
Can we help you?