Hello once again.
I wrote a program which uses Dijkstra's algorithm to give us the best weight path through an labyrinth (though every room has 2, 3 or 4 neighbours). Each room has it's weight. We simply create the labyrinth by command 'Graph graph = new Graph(n,m);'. Adding vertexes by 'graph.addVertex(position n, position m, weight);'. If you want to see a room and go through the labyrinth you must write 'graph.run(graph.vertexList[positon n][position m]);' - it should give the total weight of all visited rooms and the directions in which you should go. If you just want to go through you write: 'graph.Dijkstra(graph.vertexList[0][graph.n-1]);' since the entrance is always at [m][0] and the exit at [0][n]. To get the path in directions after command Dijkstra only 'graph.Path();'.
The problem is in the Graph.run command. It gives back good values for certain labyrinths, but for certain it does not. For example:
Works and gives back good values while:Graph graph2 = new Graph(4,4); graph2.addVertex(0,0,1); graph2.addVertex(1,0,1); graph2.addVertex(2,0,1); graph2.addVertex(3,0,1); graph2.addVertex(0,1,1); graph2.addVertex(1,1,9); graph2.addVertex(2,1,9); graph2.addVertex(3,1,9); graph2.addVertex(0,2,1); graph2.addVertex(1,2,1); graph2.addVertex(2,2,9); graph2.addVertex(3,2,9); graph2.addVertex(0,3,1); graph2.addVertex(1,3,9); graph2.addVertex(2,3,9); graph2.addVertex(3,3,9); graph2.run(graph2.vertexList[1][2]); graph2.run(graph2.vertexList[3][3]); graph2.Dijkstra(graph2.vertexList[0][graph2.n-1]); graph2.Path();
crashes at the 1st graph.run. It says that u is null. Well, I simply agree since I was debugging it and saw that it goes to null. The thing is I just can't get to the point where the problem is. I just don't get why it doesn't work.Graph graph = new Graph(7,5); graph.addVertex(0,0,0); graph.addVertex(1,0,5); graph.addVertex(2,0,25); graph.addVertex(3,0,20); graph.addVertex(4,0,75); graph.addVertex(5,0,0); graph.addVertex(6,0,50); graph.addVertex(0,1,20); graph.addVertex(1,1,80); graph.addVertex(2,1,60); graph.addVertex(3,1,10); graph.addVertex(4,1,25); graph.addVertex(5,1,35); graph.addVertex(6,1,120); graph.addVertex(0,2,10); graph.addVertex(1,2,15); graph.addVertex(2,2,75); graph.addVertex(3,2,25); graph.addVertex(4,2,40); graph.addVertex(5,2,70); graph.addVertex(6,2,100); graph.addVertex(0,3,70); graph.addVertex(1,3,25); graph.addVertex(2,3,150); graph.addVertex(3,3,10); graph.addVertex(4,3,100); graph.addVertex(5,3,5); graph.addVertex(6,3,80); graph.addVertex(0,4,50); graph.addVertex(1,4,0); graph.addVertex(2,4,170); graph.addVertex(3,4,5); graph.addVertex(4,4,0); graph.addVertex(5,4,10); graph.addVertex(6,4,30); graph.run(graph.vertexList[3][4]); graph.run(graph.vertexList[2][6]); graph.Dijkstra(graph.vertexList[0][graph.n-1]); graph.Path();
This is the whole project. It is written in NetBeans so I give you the whole folder with the classes. Rynek3.rar
I'd reallly appreciate any help since my debugging can't do anything.
//EDIT: Don't know why but Dijkstra seems to stop before it gets to the final point. Think there must be something wrong with the method Dijkstra.


LinkBack URL
About LinkBacks
Reply With Quote