0

I want to find all cycles in graph and using this solution Finding all cycles in undirected graphs

algorithm works fine, but I want to pass two dimensional array as argument, but there comes in a problem:

the given graph is declared like

    static int[][] graph =
    {
        {1, 7}, {1, 8}, {7, 8}, {2, 3},
        {3, 4}, {6, 4},
        {7, 6}, {8, 7}
    };

and it works, but I used

int[][] intGraph = new int[500][4];
 .....
intGraph[j][0] = Integer.parseInt(edge[i]);
intGraph[j][1] = Integer.parseInt(edge[i+1]);
intGraph[j][2] = Integer.parseInt(edge[i+2]);
....

both array values I can accsess via arrayname[x][y]

but when I'm trying to pass it as an argument in: (all algorithm in link above)

    public GraphCycleFinder(int[][] graphs) {
    graph = graphs;

    for (int i = 0; i < graph.length; i++){
        System.out.println(i);             //added to check itterations
        for (int j = 0; j <  graph[i].length; j++)
        {
            //System.out.print(graph[i][j]);
            findNewCycles(new int[] {graph[i][j]});
        }
}

I get no result, no errors. When I print out i values (to check how far it iterates), it gives me 0 when it loops through my array (but should be from 0 to 500), but prints from 0 to array length this hard-coded array.

I suppose that there is something with array declarations because I tried

  int[][] intGraph;

and then put values like

  intGraph[j][0] = Integer.parseInt(edge[i]);

but I got java.lang.NullPointerException

Any ideas?

P.s. sorry if too stupid question, I'm new in java.

UPDATE

Problem is not with array declaration. I commented out findNewCycles(new int[] {graph[i][j]}); and bouth cycles gave necessary result. Probably method findNewCycles in this algorithm can't handle this big array. :( although only 200 vertex and 500 edges. Not much.

Community
  • 1
  • 1
Kristīne Glode
  • 1,409
  • 4
  • 16
  • 22
  • Sounds like either `edge` itself or `edge[i]` are `null`, but we can't tell without a stack trace. – CodeBlind Dec 24 '13 at 20:16
  • Did you initialize `intGraph[j]`? Arrays in Java are objects, so `int[][]` is an array of objects, each of which is itself an array of integer primitives. – shoover Dec 24 '13 at 20:20
  • This intGraph array is full with values so I suppose edge or edge[i] is not null. And I'll try to figure out how to get stack trace :D – Kristīne Glode Dec 24 '13 at 20:22
  • You are getting `NPE` from which line? – Anirban Nag 'tintinmj' Dec 24 '13 at 20:25
  • Have can I initialize intGraph[j]? int[][] intGraph = new int[500][4]; - int's not initialization? – Kristīne Glode Dec 24 '13 at 20:25
  • tintinmj - I got it when trying int[][] intGraph; insted of int[][] intGraph = new int[500][4]; – Kristīne Glode Dec 24 '13 at 20:27
  • No it's not possible. No one can get `NPE` on declaration. Please check the line, where you are getting `NPE`. – Anirban Nag 'tintinmj' Dec 24 '13 at 20:30
  • NPE points to `intGraph[j][0] = Integer.parseInt(edge[i]);`. But only when above is `int[][] intGraph;` not getting if `int[][] intGraph = new int[500][4];` It must be connected with _shoover_ comment about initialization. – Kristīne Glode Dec 24 '13 at 20:36
  • When you declare `intGraph` as `int[][] intGraph;`, in what lines of code are you initializing it? There is some discussion of initializing 2D arrays in [another SO question](http://stackoverflow.com/questions/17263691/initializing-a-2d-array-in-java). – shoover Dec 24 '13 at 21:05
  • That is, not only does the 2D array `intGraph` need to be initialized, but also each of the 1D arrays `intGraph[j]` need to be initialized too. – shoover Dec 24 '13 at 21:06
  • _shoover_ - thanks for tips, I'll read more about 2D initialization! – Kristīne Glode Dec 24 '13 at 21:35
  • As far as I understood I have to set array dimensions and 2D initialization is done, like here `intGraph = new int[500][4];`. And there is no errors, the only problem that class `GraphCycleFinder` doesn't work as supposed to :D – Kristīne Glode Dec 24 '13 at 21:47

1 Answers1

1

Here are some points that may help you(I hope they do somehow)

Array Declarations as for the title of the question

In java you can declare array in two ways

  1. Variable of array datatype(like int array type)

    int[] a,b; //both a and b are of integer array type

  2. Array variable of some datatype (like int)

    int c[],d; //both c and d are of int type but only c is array

Initialization since it is required We can declare and initialize array as follows

int[] a = {1, 2, 3, 4, 5};
      // or
int b[][] = {{1, 2, 3}, {4, 5, 6}, {1, 3, 5}, {2, 4, 6}};

We can initialize the declared array as follows

a[0] = 1;
a[1] = 2*a[0]; //or any other expression

Things you missed in question or maybe missing in your code/problem

  • graph is array of edges and edges have two vertex and hence intGraph[500][4] should be intGraph[500][2]
  • public GraphCycleFinder(int[][] graphs) {...} should be static if you're calling it in main()and at last closing } is missing as described above.
  • As shoover is saying you must initialize intGraph[j] also.

By this we mean in code

.....
intGraph[j][0] = Integer.parseInt(edge[i]);
intGraph[j][1] = Integer.parseInt(edge[i+1]);
intGraph[j][2] = Integer.parseInt(edge[i+2]);
....

you are only initializing one dimension within same row or for example if j=1 then you're initializing intGraph[1][0] to intGraph[1][3] only not for all j=0 to 499 except 1.

and most probably NPE is encountered because with int [][]intGraph; you're not initializing it all elements and trying to copy uninitialized array in function public GraphCycleFinder(int[][] graphs){graph=graphs;...} and you're not getting it while using int [][]intGraph=new int[500][4]; because you're initializing all elements and no NULL value is there.

Hope this helps!!

learning
  • 47
  • 7
  • Thanks for explaining arrays! It's much clearer now! But anyways `intGraph[j][0] = Integer.parseInt(edge[i]);` i loop throuht and `j` is from 0 to 500 and values is assigned to all elements. and here `intGraph[500][2]` i use 4 insted of 2 because i have to save edge weight value and 4th is 0 or 1 (main task details). And 'public GraphCycleFinder(int[][] graphs) {...}' i'm not calling in main(), it's in other class constructor. :) And thanks once more for detailed information about arrays! – Kristīne Glode Dec 24 '13 at 22:43
  • you're welcome. And if you can share some more code, then maybe we'll understand where's the problem. – learning Dec 24 '13 at 23:33
  • It's really messy right now, I'll try clean it up, maybe will find the problem. If I don't then I will share clean code :) – Kristīne Glode Dec 24 '13 at 23:57