An alternative way to write this recursive function? - java

I have a grid of colors (in a 2D ArrayList). I need to be able to count the number of cells that share the same color in a particular color block (they have to be adjacent on 4 edges). I can do this easily recursively, but the problem is that some images Overflow the stack since color blocks can be so big.
Here's the recursive function:
private int getBlockCount(PietCodel codel) {
if (codel.getValue() != PietCodel.DEFAULT && codel.getValue() != PietCodel.CHECKED) {
return codel.getValue();
}
ArrayList<PietCodel> list = blockCountHelper(codel);
list.add(codel);
// Use the array of codels in the block, and
// use the size to for each value in the array.
int result = list.size();
for (PietCodel item : list) item.setValue(result);
System.out.println("Block count: " + result);
return result;
}
private ArrayList<PietCodel> blockCountHelper(PietCodel codel) {
ArrayList<PietCodel> result = new ArrayList<>();
codel.setValue(PietCodel.CHECKED);
int col = codel.getCol();
int row = codel.getRow();
// Right
PietCodel ajac = get(col + 1, row);
if (ajac != null && codel.equals(ajac.getColor()) && ajac.getValue() == PietCodel.DEFAULT) {
ArrayList<PietCodel> nextCodels = blockCountHelper(ajac);
result.add(ajac);
result.addAll(nextCodels);
}
// Down
ajac = get(col, row + 1);
if (ajac != null && codel.equals(ajac.getColor()) && ajac.getValue() == PietCodel.DEFAULT) {
ArrayList<PietCodel> nextCodels = blockCountHelper(ajac);
result.add(ajac);
result.addAll(nextCodels);
}
// Left
ajac = get(col - 1, row);
if (ajac != null && codel.equals(ajac.getColor()) && ajac.getValue() == PietCodel.DEFAULT) {
ArrayList<PietCodel> nextCodels = blockCountHelper(ajac);
result.add(ajac);
result.addAll(nextCodels);
}
// Up
ajac = get(col, row - 1);
if (ajac != null && codel.equals(ajac.getColor()) && ajac.getValue() == PietCodel.DEFAULT) {
ArrayList<PietCodel> nextCodels = blockCountHelper(ajac);
result.add(ajac);
result.addAll(nextCodels);
}
return result;
}
Any thoughts on an alternative with loops or something?

The idea is to make the "stack/queue" explicit in your application code. Note that this doesn't use less memory then the recursive approach, it just
has more memory to play with by utilizing the heap. The following code is an example. Note that you can call queue.addFirst or queue.addLast, this will
not change the end result but will give you different traversals of the board which is something you may or may not care about.
private ArrayList<PietCodel> blockCountHelper(PietCodel codel) {
ArrayList<PietCodel> accumulator = new ArrayList<>();
LinkedList<PietCodel> queue = new LinkedList<>();
queue.add(codel);
while (!queue.isEmpty()) {
PietCodel ajac = queue.remove();
if (ajac != null && codel.equals(ajac.getColor()) .... ) {
accumulator.add(ajac);
}
if ( get(col + 1, row) != null ) {queue.addFirst(get(col + 1, row));}
if ( get(col , row + 1) != null ) {queue.addFirst(get(col, row + 1));}
if ( get(col - 1, row) != null ) {queue.addFirst(get(col - 1, row));}
if ( get(col , row - 1) != null ) {queue.addFirst(get(col, row- 1));}
}
return accumulator;
}

Standard way to get rid of recursion is to use Stack data structure, as recursion is essentially a stack manipulation. But in your concrete situation you can use breadth-first search. You can implement it using queue:
int rows = 10;
int cols = 10;
PietCodel codels[][] = new PietCodel[rows][cols];
boolean used[][] = new boolean[rows][cols];
private void test() {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < rows; ++j) {
int color = (int) (Math.random() * 3);
PietCodel codel = new PietCodel(i, j, color);
codels[i][j] = codel;
System.out.print(color + " ");
}
System.out.println();
}
System.out.println();
System.out.println(getBlockCount(get(0, 0)));
}
private int getBlockCount(PietCodel codel) {
used = new boolean[rows][cols];
Queue<PietCodel> q = new LinkedList<>();
q.add(codel);
used[codel.getRow()][codel.getCol()] = true;
int color = codel.getColor();
int count = 0;
while (!q.isEmpty()) {
PietCodel ajacent = q.poll();
int col = ajacent.getCol();
int row = ajacent.getRow();
++count;
addColored(q, col + 1, row, color);
addColored(q, col - 1, row, color);
addColored(q, col, row + 1, color);
addColored(q, col, row - 1, color);
}
return count;
}
private PietCodel get(int col, int row) {
return col < 0 || col >= cols || row < 0 || row >= rows ? null : codels[row][col];
}
private void addColored(Queue<PietCodel> q, int col, int row, int color) {
if (col < 0 || col >= cols || row < 0 || row >= rows) {
return;
}
PietCodel codel = codels[row][col];
if (codel.getColor() != color || used[row][col]) {
return;
}
used[row][col] = true;
q.add(codel);
}
static class PietCodel {
static final int DEFAULT = 0;
static final int CHECKED = -1;
static final int USED = -2;
final int row;
final int col;
final int color;
int value;
public PietCodel(int row, int col, int color) {
this.col = col;
this.row = row;
this.color = color;
}
public int getCol() {
return col;
}
public int getRow() {
return row;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getColor() {
return color;
}
public boolean same(PietCodel ajac) {
return color == ajac.getColor();
}
}

Related

DFS function for finding the clusters of same symbol in 2D matrix

I am trying to write a DFS code for finding the clusters of size greater than or equal to 2 inside a 2D matrix of integers. I am trying to write the DFS for that. Here I am unable to get any output inside the win cluster list of Cluster Object. Can anybody suggest what is going wrong. I have written a DFS function that calls for all neighbouring locations(horizontally and vertically).
Also checking for the valid condition for checking whether the indices are inside the row and column constraints. When the DFS is terminating I am checking for the condition if count of cluster is greater than 2 then I'll add it to the win clusters list else simply return.Please suggest why am I not getting any value in the win_clusters list.
import java.util.*;
public class sample {
static int grid[][]= {{1,3,0,4,0},
{3,2,0,0,1},
{0,4,1,0,1},
{4,0,0,0,2},
{2,4,0,2,4}};
static int ROW;
static int COLUMN;
public static int[][] grid_creation(int numRows, int numCols){
ROW= numRows;
COLUMN=numCols;
int [][]grid = new int[numRows][numCols];
Random rand = new Random();
for(int i = 0; i<numRows ; i++){
for(int j = 0; j<numCols; j++){
grid[i][j]= rand.nextInt(5);
}
}
return grid;
}
public static void print_matrix(int[][]matrix){
for(int i = 0; i< matrix.length;i++){
for(int j = 0; j<matrix[0].length; j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
public static boolean isValid(boolean[][] visited, int row, int col){
if(row<0 ||col<0 || row>=ROW || col>=COLUMN || visited[row][col] )
return false;
return true;
}
public static class Cluster{
List<Integer> cluster_symbol_positions = new ArrayList<Integer>();
int symbol = -1;
int count = 0;
int win = 0;
public void create_new_Cluster(){
cluster_symbol_positions = new ArrayList<>();
symbol = -1;
count = 0;
win= 0;
}
public List<Integer> get_cluster_symbol_positions(){
return cluster_symbol_positions;
}
public int getSymbol(){return symbol;}
public void setSymbol(int a){symbol = a;}
public int getCount(){return count;}
public void add_to_cluster(int row1, int col1 ){
int row = row1;
int col = col1;
cluster_symbol_positions.add(getReelPositionNumber(row,col));
count++;
}
public int cluster_win(Cluster cluster){
int win_amount = 0;
//return win amount from the paytable
return win_amount;
}
}
public static void DFS(int[][] grid,List<Cluster> win_clusters) {
int h = grid.length;
if (h == 0)
return;
int l = grid[0].length;
//created visited array
boolean [][] visited = new boolean[h][l];
System.out.println("Depth-First Search: ");
for( int i =0; i< ROW ; i++){
for(int j = 0 ;j<COLUMN; j++){
if(!visited[i][j]) {
DFSUtil(grid, i, j, visited, win_clusters);
}
}
}
}
static Cluster global_cluster= new Cluster();
static int cluster_length =0;
public static void DFSUtil(int[][]grid, int row, int col, boolean[][] visited, List<Cluster>win_clusters){
if (row < 0 || col < 0 || row >= ROW|| col >= COLUMN || visited[row][col] ) {
if(global_cluster.getCount()>=2) win_clusters.add(global_cluster);
return;
}
int symbol = grid[row][col];
visited[row][col] = true;
cluster_length++;
global_cluster.add_to_cluster(row,col);
global_cluster.setSymbol(symbol);
if(isValid(visited, row+1, col) && grid[row+1][col]==symbol){
DFSUtil(grid, row+ 1, col,visited,win_clusters); // go down
}
if(isValid(visited, row-1, col) && grid[row-1][col]==symbol) {
DFSUtil(grid, row - 1, col, visited, win_clusters); //go up
}
if(isValid(visited, row, col+1) && grid[row][col+1]==symbol){
DFSUtil(grid, row, col + 1,visited,win_clusters); //go right
}
if(isValid(visited, row, col-1) && grid[row][col-1]==symbol){
DFSUtil(grid, row, col - 1,visited,win_clusters); // go left
}
}
public static int getReelPositionNumber(int row, int col) {
return (row*COLUMN) + col;
}
public static List<Integer> getReelPositionsIndex(int positionNumber) {
List<Integer> tempData = new ArrayList<Integer>();
int numReels =COLUMN;
int display = ROW;
if(positionNumber >= numReels*display)
throw new RuntimeException( positionNumber+" is out of range "+ numReels*display +"(rows*cols) in the game." );
if(positionNumber < numReels) {
tempData.add(0);
tempData.add(positionNumber);
} else if(positionNumber == (numReels*display)) {
tempData.add(display - 1);
tempData.add(numReels - 1);
} else {
int row = positionNumber / numReels;
int col = positionNumber % numReels;
tempData.add(row);
tempData.add(col);
}
return tempData;
}
public static void main(String args[]){
System.out.println("Modified function for cluster win ");
List<Cluster> win_clusters = new ArrayList<Cluster>();
DFS(grid,win_clusters);
print_matrix(grid);
System.out.println(win_clusters.size());
}
}

Finding path in a boolean matrix

The problem I'm trying to solve is a standard interview question. Given a boolean matrix find the path from the starting point to the finishing point.
The start point is assumed the left top corner
The finishing point the right bottom corner.
Only grids with 0 can be moved into.
No diagonal moves are allowed.
Here's my code.
public class PathFinder {
public static ArrayList<Pair> dfs(int[][] arr, int row, int col, Pair sp, Pair fp){
int[][] check = new int[row][col];
ArrayList<Pair> path = new ArrayList<>();
dfs(arr, row, col, path, check, sp, fp);
return path;
}
private static void dfs(int[][] arr, int row, int col, ArrayList<Pair> path, int[][] check, Pair sp, Pair fp){
if(sp.getRow() == fp.getRow() && sp.getCol() == fp.getCol()) return;
if((sp.getRow() +1 < row) &&(arr[sp.getRow() +1][sp.getCol()] == 0) && (check[sp.getRow()+1][sp.getCol()] == 0)){
check[sp.getRow()+1][sp.getCol()] = 1;
path.add(new Pair(sp.getRow()+1, sp.getCol()));
dfs(arr, row, col, path, check, new Pair(sp.getRow()+1, sp.getCol()), fp);
}else if((sp.getRow() -1 >= 0) &&(arr[sp.getRow() -1][sp.getCol()] == 0) && (check[sp.getRow()-1][sp.getCol()] == 0)){
check[sp.getRow()-1][sp.getCol()] = 1;
path.add(new Pair(sp.getRow()-1, sp.getCol()));
dfs(arr, row, col, path, check, new Pair(sp.getRow()-1, sp.getCol()), fp);
}else if((sp.getCol() +1 < col) &&(arr[sp.getRow()][sp.getCol() +1] == 0) && (check[sp.getRow()][sp.getCol()+1] == 0)){
check[sp.getRow()][sp.getCol()+1] = 1;
path.add(new Pair(sp.getRow(), sp.getCol()+1));
dfs(arr, row, col, path, check, new Pair(sp.getRow(), sp.getCol()+1), fp);
}else if((sp.getCol() -1 >= 0) &&(arr[sp.getRow()][sp.getCol() -1] == 0) && (check[sp.getRow()][sp.getCol()-1] == 0)) {
check[sp.getRow()][sp.getCol() - 1] = 1;
path.add(new Pair(sp.getRow(), sp.getCol() - 1));
dfs(arr, row, col, path, check, new Pair(sp.getRow(), sp.getCol() - 1), fp);
}
}
public static void printPath(ArrayList<Pair> list){
for(Iterator itr = list.iterator(); itr.hasNext();){
Pair p = (Pair) itr.next();
System.out.println(p.getRow()+","+p.getCol());
}
}
}
Here's my Pair
public class Pair {
private int row;
private int col;
public Pair(int row, int col){
this.row = row;
this.col = col;
}
public int getRow(){
return row;
}
public int getCol(){
return col;
}
}
And here's my calling code.
public class Main {
public static void printArray(int[][] arr, int row, int col){
for (int i = 0; i < row; i++) {
for (int j = 0; j <col ; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
// write your code here
int row = 5;
int col = 7;
int[][] matrix = new int[row][col];
matrix[0][1] = 1;
matrix[0][3] = 1;
matrix[0][5] = 1;
matrix[1][1] = 1;
matrix[1][3] = 1;
matrix[1][6] = 1;
matrix[2][1] = 1;
matrix[2][2] = 1;
matrix[2][6] = 1;
matrix[3][3] = 1;
matrix[3][5] = 1;
matrix[3][6] = 1;
matrix[4][0] = 1;
printArray(matrix, row, col);
ArrayList<Pair> list = PathFinder.dfs(matrix, row, col, new Pair(0,0), new Pair(row-1, col-1));
PathFinder.printPath(list);
}
}
The issue is that this depth-first-search only works for specific cases. Can someone help me modify the code so that it works for all cases. Please bear in mind I don't want a breath-first search.
Here is a solution with the use of a Stack containing subpaths between junctions and a self implemented linked list of Pairs. The already visited fields are saved in the matrix. At the end the matrix is printed again, where the result-fields (found path) have the value 3 and the other visited fields have the value 2.
public class Pair {
private int row;
private int col;
private Pair next;
public Pair(int row, int col){
this.row = row;
this.col = col;
}
public int getRow(){
return row;
}
public int getCol(){
return col;
}
public Pair getNext() {
return next;
}
public void setNext(Pair next) {
this.next = next;
}
}
///////////////////////
import java.util.*;
public class PathFinder {
private int[][] arr;
private int rowCount;
private int colCount;
private Stack<Pair> junctions = new Stack<>();
public PathFinder(int[][] arr){
this.arr = arr;
this.rowCount = arr.length;
if(rowCount > 0) {
this.colCount = arr[0].length;
}
}
public Pair dfs(Pair sp){
int actualRow = sp.getRow();
int actualCol = sp.getCol();
//we where already here
arr[actualRow][actualCol] = 2;
if(actualRow >= rowCount - 1 && actualCol >= colCount - 1) {
//ready
return sp;
}
boolean deeper = actualRow +1 < rowCount && arr[actualRow +1][actualCol] == 0;
boolean left = actualCol -1 >= 0 && arr[actualRow][actualCol -1] == 0;
boolean right = actualCol +1 < colCount && arr[actualRow][actualCol +1] == 0;
boolean up = actualRow -1 >= 0 && arr[actualRow-1][actualCol] == 0;
//test for junctions
int possibilities = 0;
if(left){
possibilities++;
}
if(right) {
possibilities++;
}
if(deeper){
possibilities++;
}
if(up){
possibilities++;
}
if(possibilities > 1) {
this.junctions.push(sp);
}
Pair nextPair;
if(deeper){
nextPair = new Pair(actualRow + 1, actualCol);
} else if(left) {
nextPair = new Pair(actualRow, actualCol-1);
} else if(right) {
nextPair = new Pair(actualRow, actualCol+1);
} else if(up) {
nextPair = new Pair(actualRow-1, actualCol);
} else {
if(!this.junctions.empty()) {
Pair lastJunction = this.junctions.pop();
lastJunction.setNext(null);
return dfs(lastJunction);
}
return sp;
}
sp.setNext(nextPair);
return dfs(nextPair);
}
}
/////////////////////
public class Main {
public static void printArray(int[][] arr, int row, int col){
for (int i = 0; i < row; i++) {
for (int j = 0; j <col ; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int rowCount = 6;
int colCount = 8;
int[][] matrix = new int[rowCount][colCount];
matrix[0] = new int[]{0, 1, 0, 1, 0, 0, 0, 1};
matrix[1] = new int[]{0, 1, 0, 0, 0, 1, 0, 0};
matrix[2] = new int[]{0, 0, 0, 1, 0, 1, 0, 0};
matrix[3] = new int[]{0, 1, 1, 1, 1, 0, 0, 1};
matrix[4] = new int[]{0, 0, 0, 0, 0, 1, 0, 0};
matrix[5] = new int[]{0, 1, 0, 1, 0, 0, 1, 0};
printArray(matrix, rowCount, colCount);
Pair pair = new Pair(0,0);
PathFinder finder = new PathFinder(matrix);
Pair finish = finder.dfs(pair);
if(finish.getRow() == rowCount-1 && finish.getCol() == colCount -1) {
while( pair != null){
System.out.println(pair.getRow()+","+pair.getCol());
matrix[pair.getRow()][pair.getCol()] = 3;
pair = pair.getNext();
}
} else {
System.out.println("no path found");
}
printArray(matrix, rowCount, colCount);
}
}

IF Statement Checking (Not Working Properly)

randomEmpty() returns a random coordinate on the n x n grid that is empty (Method works). randomAdjacent() uses randomEmpty() to select an EMPTY coordinate on the map. Comparisons are then made to see if this coordinate has an VALID adjacent coordinate that is NON-EMPTY. The PROBLEM is that randomAdjacent does not always return the coordinates of space with an adjacent NON-EMPTY space. It will always return valid coordinates but not the latter. I can't spot the problem. Can someone help me identify the problem?
public int[] randomEmpty()
{
Random r = new Random();
int[] random = new int[2];
int row = r.nextInt(array.length);
int column = r.nextInt(array.length);
while(!(isEmpty(row,column)))
{
row = r.nextInt(array.length);
column = r.nextInt(array.length);
}
random[0] = row+1;
random[1] = column+1;
return random;
}
public int[] randomAdjacent()
{
int[] adjacentToX = new int[8];
int[] adjacentToY = new int[8];
int[] adjacentFrom = randomEmpty();
int count;
boolean isTrue = false;
boolean oneAdjacentNotEmpty = false;
while(!(oneAdjacentNotEmpty))
{
count = 0;
if(validIndex(adjacentFrom,1,-1))
{
adjacentToX[count] = adjacentFrom[0]+1;
adjacentToY[count] = adjacentFrom[1]-1;
count++;
}
if(validIndex(adjacentFrom,0,-1))
{
adjacentToX[count] = adjacentFrom[0];
adjacentToY[count] = adjacentFrom[1]-1;
count++;
}
if(validIndex(adjacentFrom,-1,-1))
{
adjacentToX[count] = adjacentFrom[0]-1;
adjacentToY[count] = adjacentFrom[1]-1;
count++;
}
if(validIndex(adjacentFrom,-1,0))
{
adjacentToX[count] = adjacentFrom[0]-1;
adjacentToY[count] = adjacentFrom[1];
count++;
}
if(validIndex(adjacentFrom,-1,1))
{
adjacentToX[count] = adjacentFrom[0]-1;
adjacentToY[count] = adjacentFrom[1]+1;
count++;
}
if(validIndex(adjacentFrom,0,1))
{
adjacentToX[count] = adjacentFrom[0];
adjacentToY[count] = adjacentFrom[1]+1;
count++;
}
if(validIndex(adjacentFrom,1,1))
{
adjacentToX[count] = adjacentFrom[0]+1;
adjacentToY[count] = adjacentFrom[1]+1;
count++;
}
if(validIndex(adjacentFrom,1,0))
{
adjacentToX[count] = adjacentFrom[0]+1;
adjacentToY[count] = adjacentFrom[1];
count++;
}
for(int i = 0; i < count; i++)
{
if(!(isEmpty(adjacentToX[i],adjacentToY[i])))
{
oneAdjacentNotEmpty = true;
isTrue = true;
}
}
if(isTrue)
break;
else
adjacentFrom = randomEmpty();
}
return adjacentFrom;
}
public boolean validIndex(int[] a,int i, int j)
{
try
{
Pebble aPebble = array[a[0]+i][a[1]+j];
return true;
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
}
public void setCell(int xPos, int yPos, Pebble aPebble)
{
array[xPos-1][yPos-1] = aPebble;
}
public Pebble getCell(int xPos, int yPos)
{
return array[xPos-1][yPos-1];
}
JUNIT Test Performed:
#Test
public void testRandomAdjacent() {
final int size = 5;
final Board board2 = new Board(size);
board2.setCell(1, 1, Pebble.O);
board2.setCell(5, 5, Pebble.O);
int[] idx = board2.randomAdjacent();
int x = idx[0];
int y = idx[1];
boolean empty = true;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if ((i == x && j == y) || i < 1 || j < 1 || i > size || j > size) {
continue;
}
if (board2.getCell(i, j) != Pebble.EMPTY)
empty = false;
}
}
assertFalse(empty);// NEVER gets SET TO FALSE
assertEquals(Pebble.EMPTY, board2.getCell(x, y));
}
As for the answer: I got carried away optimizing your code for readability. I'd think it's most likely
if (board2.getCell(i, j) != Pebble.EMPTY)
empty = false;
causing the problem as getCell operates in 1-based coordinates, but i, j are in 0-based.
You should think about your logic overall. The way I see it, your code might never terminate as randomEmpty() could keep returning the same field over and over again for an undetermined period of time.
I took the liberty to recode your if-if-if cascade into utility method easier to read:
public boolean hasNonEmptyNeighbor(int[] adjacentFrom) {
for(int i = -1; i <= 1; ++i) {
for(int j = -1; j <= 1; ++j) {
if(validIndex(adjacentFrom, i, j) //Still inside the board
&& // AND
!isEmpty(adjacentFrom[0]+i //not empty
,adjacentFrom[1]+j)) {
return true;
}
}
}
return false;
}
Given my previous comment about random() being not the best of choices if you need to cover the full board, your main check (give me an empty cell with a non-empty neighbor) could be rewritten like this:
public void find() {
List<Point> foundPoints = new ArrayList<Point>();
for(int i = 0; i < Board.height; ++i) { //Assumes you have stored your height
for(int j = 0; j < Board.width; ++j) { //and your width
if(isEmpty(i, j) && hasNonEmptyNeighbor(new int[]{i,j})) {
//Found one.
foundPoints.add(new Point(i, j));
}
}
}
//If you need to return a RANDOM empty field with non-empty neighbor
//you could randomize over length of foundPoints here and select from that list.
}

Keeping track of Collisions per index in an array-based hash table, as well as which values resulted in a collision using OPEN ADDRESSING ONLY

Sorry for the wordy title but it explains my question pretty well.
I am working on an assignment in Java where I need to create my own Hash Table.
The specifications are such that I must use an Array, as well as open-addressing for collision handling (with both double hashing and quadratic hashing implementations).
My implementation works quite well, and using over 200,000 randomly generated Strings I end up with only ~1400 Collisions with both types of collision handling mentioned about (keeping my load factor at 0.6 and increasing my Array by 2.1 when it goes over).
Here is where I'm stumped, however... My assignment calls for 2 specifications that I cannot figure out.
1) Have an option which, when removing a value form the table, instead of using "AVAILABLE" (replacing the index in the array with a junk value that indicates it is empty), I must find another value that previously hashed to this index and resulted in a collision. For example, if value A hashed to index 2 and value B also hashed to index 2 (and was later re-hashed to index 5 using my collision handling hash function), then removing value A will actually replace it with Value B.
2) Keep track of the maximum number of collisions in a single array index. I currently keep track of all the collisions, but there's no way for me to keep track of the collisions at an individual cell.
I was able to solve this problem using Separate Chaining by having each Array Index hold a linked list of all values that have hashed to this index, so that only the first one is retrieved when I call my get(value) method, but upon removal I can easily replace it with the next value that hashed to this index. It's also an easy way to get the max number of collisions per index.
But we were specifically told not to use separate chaining... I'm actually wondering if this is even possible without completely ruining the complexity of the hash table.
Any advice would be appreciated.
edit:
Here are some examples to give you an idea of my class structure:
public class daveHash {
//Attributes
public String[] dTable;
private double loadFactor, rehashFactor;
private int size = 0;
private String emptyMarkerScheme;
private String collisionHandlingType;
private int collisionsTotal = 0;
private int collisionsCurrent = 0;
//Constructors
public daveHash()
{
dTable = new String[17];
rehashFactor = 2.1;
loadFactor = 0.6;
emptyMarkerScheme = "A";
collisionHandlingType = "D";
}
public daveHash(int size)
{
dTable = new String[size];
rehashFactor = 2.1;
loadFactor = 0.6;
emptyMarkerScheme = "A";
collisionHandlingType = "D";
}
My hashing functions:
public long getHashCode(String s, int index)
{
if (index > s.length() - 1)
return 0;
if (index == s.length()-1)
return (long)s.charAt(index);
if (s.length() >= 20)
return ((long)s.charAt(index) + 37 * getHashCode(s, index+3));
return ((long)s.charAt(index) + 37 * getHashCode(s, index+1));
}
public int compressHashCode(long hash, int arraySize)
{
int b = nextPrime(arraySize);
int index = ((int)((7*hash) % b) % arraySize);
if (index < 0)
return index*-1;
else
return index;
}
Collision handling:
private int collisionDoubleHash(int index, long hashCode, String value, String[] table)
{
int newIndex = 0;
int q = previousPrime(table.length);
int secondFunction = (q - (int)hashCode) % q;
if (secondFunction < 0)
secondFunction = secondFunction*-1;
for (int i = 0; i < table.length; i++)
{
newIndex = (index + i*secondFunction) % table.length;
//System.out.println(newIndex);
if (isAvailable(newIndex, table))
{
table[newIndex] = value;
return newIndex;
}
}
return -1;
}
private int collisionQuadraticHash(int index, long hashCode, String value, String[] table)
{
int newIndex = 0;
for (int i = 0; i < table.length; i ++)
{
newIndex = (index + i*i) % table.length;
if (isAvailable(newIndex, table))
{
table[newIndex] = value;
return newIndex;
}
}
return -1;
}
public int collisionHandling(int index, long hashCode, String value, String[] table)
{
collisionsTotal++;
collisionsCurrent++;
if (this.collisionHandlingType.equals("D"))
return collisionDoubleHash(index, hashCode, value, table);
else if (this.collisionHandlingType.equals("Q"))
return collisionQuadraticHash(index, hashCode, value, table);
else
return -1;
}
Get, Put and Remove:
private int getIndex(String k)
{
long hashCode = getHashCode(k, 0);
int index = compressHashCode(hashCode, dTable.length);
if (dTable[index] != null && dTable[index].equals(k))
return index;
else
{
if (this.collisionHandlingType.equals("D"))
{
int newIndex = 0;
int q = previousPrime(dTable.length);
int secondFunction = (q - (int)hashCode) % q;
if (secondFunction < 0)
secondFunction = secondFunction*-1;
for (int i = 0; i < dTable.length; i++)
{
newIndex = (index + i*secondFunction) % dTable.length;
if (dTable[index] != null && dTable[newIndex].equals(k))
{
return newIndex;
}
}
}
else if (this.collisionHandlingType.equals("Q"))
{
int newIndex = 0;
for (int i = 0; i < dTable.length; i ++)
{
newIndex = (index + i*i) % dTable.length;
if (dTable[index] != null && dTable[newIndex].equals(k))
{
return newIndex;
}
}
}
return -1;
}
}
public String get(String k)
{
long hashCode = getHashCode(k, 0);
int index = compressHashCode(hashCode, dTable.length);
if (dTable[index] != null && dTable[index].equals(k))
return dTable[index];
else
{
if (this.collisionHandlingType.equals("D"))
{
int newIndex = 0;
int q = previousPrime(dTable.length);
int secondFunction = (q - (int)hashCode) % q;
if (secondFunction < 0)
secondFunction = secondFunction*-1;
for (int i = 0; i < dTable.length; i++)
{
newIndex = (index + i*secondFunction) % dTable.length;
if (dTable[index] != null && dTable[newIndex].equals(k))
{
return dTable[newIndex];
}
}
}
else if (this.collisionHandlingType.equals("Q"))
{
int newIndex = 0;
for (int i = 0; i < dTable.length; i ++)
{
newIndex = (index + i*i) % dTable.length;
if (dTable[index] != null && dTable[newIndex].equals(k))
{
return dTable[newIndex];
}
}
}
return null;
}
}
public void put(String k, String v)
{
double fullFactor = (double)this.size / (double)dTable.length;
if (fullFactor >= loadFactor)
resizeTable();
long hashCode = getHashCode(k, 0);
int index = compressHashCode(hashCode, dTable.length);
if (isAvailable(index, dTable))
{
dTable[index] = v;
size++;
}
else
{
collisionHandling(index, hashCode, v, dTable);
size++;
}
}
public String remove(String k)
{
int index = getIndex(k);
if (dTable[index] == null || dTable[index].equals("AVAILABLE") || dTable[index].charAt(0) == '-')
return null;
else
{
if (this.emptyMarkerScheme.equals("A"))
{
String val = dTable[index];
dTable[index] = "AVAILABLE";
size--;
return val;
}
else if (this.emptyMarkerScheme.equals("N"))
{
String val = dTable[index];
dTable[index] = "-" + val;
size--;
return val;
}
}
return null;
}
Hopefully this can give you an idea of my approach. This does not include the Separate Chaining implementation I mentioned above. For this, I had the following inner classes:
private class hashList
{
private class hashNode
{
private String element;
private hashNode next;
public hashNode(String element, hashNode n)
{
this.element = element;
this.next = n;
}
}
private hashNode head;
private int length = 0;
public hashList()
{
head = null;
}
public void addToStart(String s)
{
head = new hashNode(s, head);
length++;
}
public int getLength()
{
return length;
}
}
And my methods were modified appropriate to access the element in the head node vs the element in the Array. I took this out, however, since we are not supposed to use Separate Chaining to solve the problem.
Thanks!!

Scan a Matrix for a number

I am currently looking for a way of scanning a 2D matrix in Java for a number. Precisely, if in my matrix, there are numbers from 0 to 9, how do I "locate" the 0? This is intended for creating a Minesweeper game.
Here is what I have written so far. It is not complete. All I want is a clue on how to complete it.
class DemineurStackOverflow {
public static void minesweeper () {
int champDeMine[][];
boolean résultat[][];
int mine;
char réponse;
champDeMine = new int[longueur][largeur]; //Differenf from the matrix "champDeMines" from the second method
Arrays.asList(champDeMine).contains(0);
mine = (int) Math.floor(Math.random()*nbMines + 1);
System.out.println("Ajustement du nombre de mines en cours...");
if (mine < nbMines) {
for (mine = (int) Math.floor(Math.random()*nbMines + 1); mine < nbMines; mine++);
} else {
for (mine = (int) Math.floor(Math.random()*nbMines + 1); mine > nbMines; mine--);
}
if (mine == nbMines){
System.out.println("Chargement des mines OK.");
}
}
public static int [][] calculeProximité ( boolean [][] champDeMines ){
int row; //row index for prescence of 0, same value as longueur
int col; //column index for presence of 0, same value as largeur
int mine;
champDeMines = new boolean[row][col];
if (champDeMines = 0) {
champDeMines = mine;
}
//Here I am trying to figure a way of finding the 0s in this champDeMines matrix.
return (new int[champDeMines.length][champDeMines[0].length]);
}
}
The first method consists in generating an array from variables "longueur" and "largeur". The number of "mines" is supposed to represent the number 0 (which is why I want to scan for a 0), at random places. The second method consists in finding the "mines" in the array created. That is what I have trouble doing. Do you have any clues for completing the second method? I am simply looking for clues because I am learning to program in Java!
Thank you very much, your help is most certainly appreciated!
This is how minesweeper field population could look from the code provided. I hope you get the clue from the comments and do not hesitate to ask if anything is unclear.
import java.util.Random;
public class Demineur
{
// Here come CONSTANTS
public static final int MAX_MINES = 30;
public static final boolean MINE = true;
// Let's have a field 12x12 size
public static final int LONGEUR = 12;
public static final int LARGEUR = 12;
// each field contains number of mines it has access to
public static int champDeMine[][] = new int[LONGEUR][LARGEUR];
// each field can contain mine or be empty
public static boolean champDeMines[][] = new boolean[LONGEUR][LARGEUR];
public static void minesweeper()
{
Random random = new Random();
int mine ;
System.out.println("Ajustement du nombre de mines en cours...");
int nbMines = random.nextInt(MAX_MINES);
/**
* Let's plant mines. :-E
* Unoptimal but will suffice for demonstration purpose.
*/
int minesLeftToPlant = nbMines;
int skip = 0;
boolean planted = false;
while (minesLeftToPlant > 0)
{
skip = random.nextInt(LONGEUR*LARGEUR);
planted = false;
while (!planted && minesLeftToPlant > 0 && skip > 0)
{
for (int y = 0; !planted && minesLeftToPlant > 0 && y < LONGEUR; y++)
{
for (int x = 0; !planted && minesLeftToPlant > 0 && x < LARGEUR; x++)
{
if ( !MINE == champDeMines[y][x]
&& 0 == skip)
{
champDeMines[y][x] = MINE;
minesLeftToPlant--;
planted = true;
}
else
{
skip--;
}
}
}
}
}
System.out.println("Chargement des "+ nbMines +" mines OK.");
}
public static void calculeProximite()
{
int row ; //row index for prescence of 0, same value as longueur
int col ; //column index for presence of 0, same value as largeur
int mine;
//Check for each field it's neighbors and calculate which of them are mines
for (row = 0; row < LONGEUR; row++)
{
for (col = 0; col < LARGEUR; col++)
{
champDeMine[row][col] = numberOfMines(row,col);
}
}
}
public static void printChampDeMine()
{
for (int row = 0; row < LONGEUR; row++)
{
for (int col = 0; col < LARGEUR; col++)
{
System.out.print("'" + champDeMine[row][col] + "' ");
}
System.out.println();
}
}
public static void printChampDemines()
{
for (int row = 0; row < LONGEUR; row++)
{
for (int col = 0; col < LARGEUR; col++)
{
System.out.print("'" + (champDeMines[row][col] ? "m" : "e") + "' ");
}
System.out.println();
}
}
public static int numberOfMines(int row, int col)
{
return add(hasMine(row , col + 1))
+ add(hasMine(row - 1, col + 1))
+ add(hasMine(row - 1, col ))
+ add(hasMine(row - 1, col - 1))
+ add(hasMine(row , col - 1))
+ add(hasMine(row + 1, col - 1))
+ add(hasMine(row + 1, col ))
+ add(hasMine(row + 1, col + 1));
}
public static boolean hasMine(int row, int col)
{
return row >= 0 && col >= 0 && row < LONGEUR && col < LARGEUR
&& isMine(champDeMines[row][col]);
}
public static boolean isMine(boolean x)
{
return MINE == x;
}
public static int add(boolean c)
{
return c ? 1 : 0;
}
public static void main(String[] args)
{
minesweeper();
System.out.println("Champ de mines");
printChampDemines();
System.out.println("Champ de mine");
calculeProximite();
printChampDeMine();
}
}

Categories

Resources