0

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.

d.lanza38
  • 2,525
  • 7
  • 30
  • 52
  • Your problem is still unclear. Where is the problem in your code? – AJ. Dec 10 '13 at 07:03
  • This is how to create a multi dimensial `ArrayList` : http://stackoverflow.com/questions/4401850/how-to-create-a-multidimensional-arraylist-in-java You can also see this example that handle integer ArrayList matrix :http://stackoverflow.com/questions/9089752/multidimensional-arraylist-and-integer-replacement – Mehdi Karamosly Dec 10 '13 at 07:13
  • Please be more clear on your issue, eg. which array are you modifying and which gets changed? Which println() are you commenting/uncommenting? – Melquiades Dec 10 '13 at 07:28
  • I apologize for not providing this information in the first place. It was late and I was very tired. I appreciate any help. – d.lanza38 Dec 10 '13 at 17:42

1 Answers1

0

For your second question.

No need of initializing each element of int array with zero because in java by default each element of int array is zero.

So if you do

int a[] = new Integer[5];// whether it is 1D , 2D or 3D
System.out.println(a[2]);

then this will print 0

AJ.
  • 4,526
  • 5
  • 29
  • 41