A little help needed with Dijkstra's algorithim

Hi I'm trying to implement Dijkstra's algorithm. I have a class GraphMaker

which creates the graph, and is the main console. And then a second class for Dijkstra's algorithm. Overall it works alright but, I can't to figure out how to get it to find the shortest path for all the cities.

Here's a sample output:

BOSTON To NEW YORK: Cost of: 22 MIAMI: Cost of: 122

NEW YORK To MIAMI: Cost of: 50 HONOLULU : Cost of: 422

DETROIT To CHICAGO: Cost of: 22 PHOENIX: Cost of: 62

MIAMI To HONOLULU : Cost of: 402

CHICAGO To

PHOENIX To

HONOLULU To DETROIT: Cost of: 302 PHOENIX: Cost of: 102

[BOSTON -> ]

[BOSTON -> , NEW YORK>]

[BOSTON -> , NEW YORK>, HAWAII>, DETROIT>]

[BOSTON -> , NEW YORK>, MIAMI>]

[BOSTON -> , NEW YORK>, HAWAII>,CHICAGO>]

[BOSTON -> , NEW YORK>, HAWAII>, PHOENIX>]

[BOSTON -> , NEW YORK>, HAWAII>]

I know its not the most efficient looking output, I'm intermediate coder at best. But at the top you have 7 cities from BOSTON to HONOLULU, and the cities they connect to. Where I'm really having the problem is at the bottom part. It shows the shortest paths from Boston to other the cities. But I want it show not only the shortest paths from Boston but also the shortest paths from the other cities, to their destination.

Here's my code:

The GraphMaker Class:

publicclass GraphMaker{

privateint [][] edges;// The adjacency matrix

private Object [] Cities;// Cities

publicstaticint count;

public GraphMaker (int n){

edges =newint [n][n];

Cities =new Object[n];

}

publicint GetSize()

{

return Cities.length;

}

publicvoidSetCities (int vertex, Object label)

{

Cities[vertex]=label;

}

public Object GetCities (int vertex)

{

return Cities[vertex];

}

publicvoid AddEdge (int source,int target,int w)

{

edges[source][target] = w;

}

publicboolean IsEdge (int source,int target)

{

return edges[source][target]>0;

}

publicvoid RemoveEdge (int source,int target)

{

edges[source][target] = 0;

}

publicint GetWeight (int source,int target)

{

return edges[source][target];

}

publicint [] Neighbors (int vertex){

count = 0;

for (int i=0; i<edges[vertex].length; i++){

if (edges[vertex][i]>0) count++;

}

finalint[]answer=newint[count];

count = 0;

for (int i=0; i<edges[vertex].length; i++){

if (edges[vertex][i]>0) answer[count++]=i;

}

return answer;

}

publicvoid print (){

for (int j=0; j<edges.length; j++){

System.out.print (Cities[j]+"To");

for (int i=0; i><edges[j].length; i++){

if (edges[j][i]>0) System.out.print (Cities[i]+": Cost of: " +

"" + edges[j][i]+"");

}

System.out.println (" ");

}

}

publicstaticvoid main (String args[]){

GraphMaker t =new GraphMaker (7);

t.SetCities (0,"BOSTON");

t.SetCities(1,"NEW YORK");

t.SetCities (2,"DETROIT");

t.SetCities (3,"MIAMI");

t.SetCities (4,"CHICAGO");

t.SetCities (5,"PHOENIX");

t.SetCities (6,"HAWAII");

t.AddEdge (0,1,22);

t.AddEdge (1,6,422);

t.AddEdge (0,3,122);

t.AddEdge (2,4,22);

t.AddEdge (2,5,62);

t.AddEdge (6,5,102);

t.AddEdge (3,6,402);

t.AddEdge (6,2,302);

t.AddEdge (1,3,50);

t.print();

finalint [] pred = Dijkstra.dijkstra (t, 0);

for (int n=0; n<7; n++){

Dijkstra.PrintPath (t, pred, 0, n);

}

}

}

And my Dijkstra class

import java.util.LinkedList;

publicclass Dijkstra{

publicstaticint [] dijkstra (GraphMaker G,int s){

finalint [] dist =newint [G.GetSize()];// shortest known distance from "s"

finalint [] pred =newint [G.GetSize()];// the node that preceeds it in the path

finalboolean [] visited =newboolean [G.GetSize()];// all false initially

for (int i=0; i<dist.length; i++){

dist[i] = Integer.MAX_VALUE;

}

dist[s] = 0;

for (int i=0; i><dist.length; i++){

finalint next = SmallestVertex (dist, visited);

visited[next] =true;

finalint [] n = G.Neighbors (next);

for (int j=0; j><n.length; j++){

finalint v = n[j];

finalint d = dist[next] + G.GetWeight(next,v);

if (dist[v] > d){

dist[v] = d;

pred[v] = next;

}

}

}

return pred;

}

privatestaticint SmallestVertex (int [] dist,boolean [] v){

int x = Integer.MAX_VALUE;

int y = -1;

for (int i=0; i<dist.length; i++){

if (!v[i] && dist[i]><x){y=i; x=dist[i];}

}

return y;

}

publicstaticvoid PrintPath (GraphMaker G,int [] pred,int s,int e){

final LinkedList path =new LinkedList();

int x = e;

while (x!=s){

path.add (0, G.GetCities(x) +">");

x = pred[x];

}

path.add (0, G.GetCities(s) +" -> ");

System.out.println (path);

}

}

[11573 byte] By [Dylancougara] at [2007-11-27 4:47:26]
# 1
Your implementation seems to be working when I compile your code with my brains! Could you show some test case with the expected results and the actual results you get so that I can figure out the problem?
Dalzhima at 2007-7-12 10:00:10 > top of Java-index,Java Essentials,New To Java...
# 2

Okay I think I finally understood your problem. If you want to know the paths from other source cities, then all you need is to start the dijkstra search again with a different source city. So basically you just need to call:

final int [] pred = Dijkstra.dijkstra (t, 0);

and replace 0 by any city's index.

Finally, it'd be interesting if you also recorded the costs from moving from one city to another in the result you are returning after performing the dijkstra algorithm :)

Dalzhima at 2007-7-12 10:00:10 > top of Java-index,Java Essentials,New To Java...
# 3

These are the actual results I get.:

BOSTON To NEW YORK: Cost of: 22 MIAMI: Cost of: 122

NEW YORK To MIAMI: Cost of: 50 HONOLULU : Cost of: 422

DETROIT To CHICAGO: Cost of: 22 PHOENIX: Cost of: 62

MIAMI To HONOLULU : Cost of: 402

CHICAGO To

PHOENIX To

HONOLULU To DETROIT: Cost of: 302 PHOENIX: Cost of: 102

[BOSTON -> ]

[BOSTON -> , NEW YORK>]

[BOSTON -> , NEW YORK>, HONOLULU>, DETROIT>]

[BOSTON -> , NEW YORK>, MIAMI>]

[BOSTON -> , NEW YORK>, HONOLULU>,CHICAGO>]

[BOSTON -> , NEW YORK>, HONOLULU>, PHOENIX>]

[BOSTON -> , NEW YORK>, HONOLULU>]

BOSTON To NEW YORK: Cost of: 22 MIAMI: Cost of: 122

NEW YORK To MIAMI: Cost of: 50 HONOLULU : Cost of: 422

DETROIT To CHICAGO: Cost of: 22 PHOENIX: Cost of: 62

MIAMI To HONOLULU : Cost of: 402

CHICAGO To

PHOENIX To

HONOLULU To DETROIT: Cost of: 302 PHOENIX: Cost of: 102

^

^

^

This part is what I expected I'm fine with that.

[BOSTON -> ]

[BOSTON -> , NEW YORK>]

[BOSTON -> , NEW YORK>, HONOLULU>, DETROIT>]

[BOSTON -> , NEW YORK>, MIAMI>]

[BOSTON -> , NEW YORK>, HONOLULU>,CHICAGO>]

[BOSTON -> , NEW YORK>, HONOLULU>, PHOENIX>]

[BOSTON -> , NEW YORK>, HONOLULU>]

^

^

^

This is where I'm having the problem, it shows the shortest paths from Boston to other cities,which fine right there those are accurate, but I also need it to show the same for the other cities

for example something this:

[NEW YORK-> ]

[NEW YORK -> , MIAMI>]

[NEW YORK -> , NEW YORK>, HAWAII>, DETROIT>]

[NEW YORK -> , NEW YORK>, MIAMI>]

[NEW YORK -> , NEW YORK>, HAWAII>,CHICAGO>]

[NEW YORK-> , HONOLULU>]

This would be the results from New York to other cities, although the actual routes for NY would probably be a little different. It would also do the same all the other cites

like DETROIT

[DETROIT -> ]

[DETROIT -> , CHICAGO>]

[DETROIT -> , CHICAGO>, HAWAII>,]

[DETROIT -> , NEW YORK>]

[DETROIT -> ,CHICAGO>]

[DETROIT -> , HONOLULU>]

It should produce a overall chart similar to this:

BOSTON -> ]

[BOSTON -> , NEW YORK>]

[BOSTON -> , NEW YORK>, HONOLULU>, DETROIT>]

[BOSTON -> , NEW YORK>, MIAMI>]

[BOSTON -> , NEW YORK>, HONOLULU>,CHICAGO>]

[BOSTON -> , NEW YORK>, HONOLULU>, PHOENIX>]

[BOSTON -> , NEW YORK>, HONOLULU>]

[NEW YORK-> ]

[NEW YORK -> , MIAMI>]

[NEW YORK -> , NEW YORK>, HAWAII>, DETROIT>]

[NEW YORK -> , NEW YORK>, MIAMI>]

[NEW YORK -> , NEW YORK>, HAWAII>,CHICAGO>]

[NEW YORK-> , HONOLULU>]

[DETROIT -> ]

[DETROIT -> , CHICAGO>]

[DETROIT -> , CHICAGO>, HAWAII>,]

[DETROIT -> , NEW YORK>]

[DETROIT -> ,CHICAGO>]

[DETROIT -> , HONOLULU>]

and so on for the other cities

Hopefully makes it a little clearer

Dylancougara at 2007-7-12 10:00:10 > top of Java-index,Java Essentials,New To Java...
# 4

> Okay I think I finally understood your problem. If

> you want to know the paths from other source cities,

> then all you need is to start the dijkstra search

> again with a different source city. So basically you

> just need to call:

>

> final int [] pred = Dijkstra.dijkstra (t, 0);

>

> and replace 0 by any city's index.

>

>

>

> Finally, it'd be interesting if you also recorded the

> costs from moving from one city to another in the

> result you are returning after performing the

> dijkstra algorithm :)

Sorry about that you must've replied when I was typing my reply. I tried calling it again but this is what I get for my output:

BOSTONToNEW YORK: Cost of:22MIAMI: Cost of:122

NEW YORKToMIAMI: Cost of:50HAWAII: Cost of:422

DETROITToCHICAGO: Cost of:22PHOENIX: Cost of:62

MIAMIToHAWAII: Cost of:402

CHICAGOTo

PHOENIXTo

HAWAIIToDETROIT: Cost of:302PHOENIX: Cost of:102

[BOSTON -> ]

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Dylancougara at 2007-7-12 10:00:10 > top of Java-index,Java Essentials,New To Java...
# 5
The program in your original post runs and takes only 300kB of heap space. I presume, therefore, that it's another program which runs out of heap. Could you post that program?
YAT_Archivista at 2007-7-12 10:00:11 > top of Java-index,Java Essentials,New To Java...