I am writing a program in Java in which I can find the transitive closure of a relation using Warshall's algorithm. While I understand the algorithm I am new to java, and I am running into some odd behavior with arrays.
I am experiencing a few different behaviors.
One is (occasionally) when I change a value of an element in one array another array gets modified. I've made the mistake before of not separating my declarations and initializations, so I am pretty sure that is not the case now.
Second, Some times if I comment/uncomment System.out.println()
's all of my 2 dimensional arrays contain a value of '0', but if I revert the comment/uncomment I get values other than zero. Most of the time the content I am printing to the screen is a hard-coded string, so I believe that should have no bearing on weather it is commented out or not (IE: Not a function call).
EDIT (Forgot to add the code):
Class:
package warshall;
import java.util.ArrayList;
public class Warshall {
private int[][][] relation;
public void setRelation(int[][][] relation){
this.relation = relation;
}
public void setRelationSet(int i, int j, int k, int value){
this.relation[i][j][k] = value;
}
public int[][][] getRelation(){
return this.relation;
}
public int getRelationSet(int i, int j, int k){
return this.relation[i][j][k];
}
private int size;
public void setSize(int size){
this.size = size;
}
public int getSize(){
return this.size;
}
private int[][] tempArray;
public void process() {
for(int k = 1; k < this.size; k++){
tempArray = new int[this.size][this.size];
for(int i = 0; i < this.size; i++){
for(int j = 0; j < this.size; j++){
for(int node = 0; node < k; node++){
if(this.relation[i][j][k-1] == 1 || (this.relation[i][node][k-1] == 1 && this.relation[node][j][k-1] == 1)){
System.out.println("i = "+i+": j = "+j+": k = "+k+": node = "+node);
tempArray[i][j] = 1;
}
}
}
}
System.out.println("Kth -1:");
this.outputRelationSet(k-1);
System.out.println("Kth: ");
for(int m = 0; m < this.size; m++){
for(int n = 0; n < this.size; n++){
this.relation[m][n][k] = tempArray[m][n];
//System.out.println(tempArray[m][n]);
if(n == this.size-1){
System.out.println("'"+tempArray[m][n]+"'");
}else{
System.out.print("'"+tempArray[m][n]+"'");
}
}
}
}
}
public void outputRelationSet(int set){
for (int i=0; i < this.size; i++){
for(int j=0; j < this.size; j++){
if(j == this.size-1){
System.out.println("'"+this.relation[i][j][set]+"'");
}else{
System.out.print("'"+this.relation[i][j][set]+"'");
}
}
}
}
}
Main
package warshall;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
int size = 4;
int[][][] theArray = new int[size][size][size];
for (int k=0; k < size; k++){
for (int i=0; i < size; i++){
for(int j=0; j < size; j++){
theArray[i][j][k] = 0;
}
}
}
theArray[0][3][0] = 1;
theArray[1][2][0] = 1;
theArray[2][0][0] = 1;
theArray[3][1][0] = 1;
Warshall thisWarshall = new Warshall();
thisWarshall.setSize(size);
thisWarshall.setRelation(theArray);
thisWarshall.outputRelationSet(0);
System.out.println();
thisWarshall.process();
System.out.println("Final: ");
thisWarshall.outputRelationSet(3);
}
}
EDIT 2:
To elaborate on item one. In the Warshall.process() method when I hit this line tempArray[i][j] = 1;
it seems to also change the associated value of the Warshall.relation[i][j][k]
array. What I am trying to do is use tempArray[][]
to collect the results without modifying the Warshall.relation[][][]
array which is what I am basing my calculations on. Then finally after looping through all vertices I sync tempArray[][]
with Warshall.relation[][][k]
. But because Warshall.relation[i][j][k]
is being modified as I loop through the vertices my results are getting skewed.
To elaborate on item two. I removed most of all System.out.println()
's which I was using for debugging so with the code posted I can not give a recreation of the issue. I was more asking if there was a common reason for that behavior in Java. When I get home I will get a portion of code together which recreates it and post another question specific to that issue. I will add a link to it from this question for anyone who comes across in the future.