# Need help for implementing Dijkstra's algorithm in Java

Page 1 of 1

## 0 Replies - 3766 Views - Last Post: 06 August 2009 - 02:03 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=118731&amp;s=220efc678691ed1bbac50c7eb918a0e5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ssct

Reputation: 0
• Posts: 1
• Joined: 05-August 09

# Need help for implementing Dijkstra's algorithm in Java

Posted 06 August 2009 - 02:03 AM

I have saved some samples intersections.
```public class Intersection {

private static String intersect[]= new String[3];

public  static void main(String args[])
{

// initialize  Intersection
intersect[0]="A";
intersect[1]="B";
intersect[2]="C";

for (int i=0; i<intersect.length;i++)
{System.out.println(intersect[i]);}

Intersection inter= new Intersection();
System.out.print(inter.getIndex("C"));

}
//get index of intersection
public  int getIndex(String str)
{
for(int i=0;i<intersect.length;i++)
{
if(intersect[i].equalsIgnoreCase(str))
{

return i;
}
}
return -1;

}

}
```

That's how I implement the graph interface so far.
```import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.ArrayList;
import java.util.List;

public class Map implements Graph{

private final int[][]distances;
public Map(int intersect)
{
distances= new int[intersect][intersect];
}
//link two intersection by given distance
public void add(Intersection start, Intersection end, int distance)
{
distances[start.getIndex("A")][start.getIndex("B")]=distance;

}

public int getDistance(Intersection start, Intersection end)

{
return distances[start.getIndex("A")][end.getIndex("B")];
}
public List <Intersection> getPredecessor(Intersection v){
List<Intersection> list = new ArrayList<Intersection>();

for (int i = 0; i < distances.length; i++)
{
if (distances[i][v.getIndex("A")] > 0)
{
}
}

return list;
}

}
```

There are two problems now. First one is I do not know how to continue to develop priority queue and second one is how to continue to implement Dijkstra's algorithm. I feel lost. Your kind help is really appreciated[code]

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }