0 Replies - 3548 Views - Last Post: 06 August 2009 - 02:03 AM Rate Topic: -----

#1 ssct  Icon User is offline

  • New D.I.C Head

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

Need help for implementing Dijkstra's algorithm in Java

Post icon  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)
			{
				list.add( v );
			}
		}
		
		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