Hello everyone,
I have the following code for Dijkstra's algorithm. What my problem is how to get from a start node to ALL the nodes. If I have 4 nodes say STL, NY, CHI, & ATL all with different weights and my start node is STL, then I need to figure the shortest distance from STL to NY, STL to CHI and STL to ATL. There could be a weight between STL & CHI of 250 miles and 275 miles. My other problem is trying to get the distances printed along with the city names. I got the city names to print but not the distances. If anyone can help I could appreciate. Code would be nice! Thanks everyone!
Test file:
public class test { public static void main(String args[]) throws FileNotFoundException { //for (int i = 0; i<=args.length-1; i++){ File file = new File("test1.dat"); dijkstra map = new dijkstra(file); //Scanner input = new Scanner(args[i+1]); //int choice = input.nextInt(); //switch (choice){ //case 0: //System.out.println(map.toString()); //break; //case 1: System.out.println(map.getRoute("Lollardry", "provocative")); //System.out.println(map.getRoute("provocative", "pseudolaminated")); //break; //} } }
dijkstra code:
import java.io.File; import java.io.FileNotFoundException; import java.util.Iterator; import java.util.Scanner; import java.util.LinkedList; /** * @author Clint Sharp */ public class dijkstra { int numberOfNodes=0; LinkedList[] array; public dijkstra(File map) throws FileNotFoundException{ Scanner file = new Scanner(map); int pos=0; String cur=""; cur=file.nextLine(); if(pos==0){numberOfNodes=Integer.parseInt(cur);} array = new LinkedList[numberOfNodes]; for(int i=0; i<numberOfNodes;i++){ cur=file.nextLine(); LinkedList list = new LinkedList(); list.add(new NodeData(cur,0)); array[i]=list; } while(file.hasNextLine()){ cur=file.next(); //System.out.println("Start "+cur); String start = cur; cur=file.next(); //System.out.println("End "+cur); String end = cur; cur=file.next(); //System.out.println("Weight "+cur); String weightString = cur; int weight = Integer.parseInt(weightString); for(int i=0; i<numberOfNodes; i++){ if(((NodeData)array[i].getFirst()).place.equals(start)){ array[i].add(new NodeData(end, weight)); } } if(file.hasNextLine()){ file.nextLine(); } } } public String getRoute(String begin, String end){ boolean beginFound=false; boolean endFound=false; for(int i=0;i<array.length;i++){ if(((NodeData)array[i].getFirst()).place.equals(begin)){beginFound=true;} } for(int i=0;i<array.length;i++){ if(((NodeData)array[i].getFirst()).place.equals(end)){endFound=true;} } if(endFound==false || beginFound==false ){throw new IllegalArgumentException();} NodeData[] placesToGo = new NodeData[numberOfNodes]; NodeData[] shortestPath = new NodeData[numberOfNodes]; int placesToGoSize=0; int placesToGoCounter=0; int shortestPathCounter=0; NodeData cur = new NodeData(begin, 0); cur.path=cur.place; placesToGo[0]=cur; placesToGoCounter++; placesToGoSize++; while(placesToGoSize!=0){ int posInDataArray=0; for(int i=0; i<array.length; i++){ if(cur.place.equals(((NodeData)array[i].getFirst()).place)){ posInDataArray=i; break; } } Iterator iter = array[posInDataArray].iterator(); Object trash = iter.next(); NodeData tmp = null; while(iter.hasNext()){ tmp=((NodeData)iter.next()); boolean isPresentInShortestPath=false; for(int i=0; i<shortestPath.length;i++){ if(shortestPath[i]!=null && shortestPath[i].place.equals(tmp.place)){ isPresentInShortestPath=true; } } if(isPresentInShortestPath==false){ int posIfPresent=-1; boolean isPresentInPlacesToGo = false; for(int i=0; i<placesToGo.length;i++){ if(placesToGo[i]!=null && placesToGo[i].place.equals(tmp.place)){ isPresentInPlacesToGo=true; posIfPresent=i; } } if(isPresentInPlacesToGo==true){ int tmpWeight=tmp.weight; tmp.weight=tmp.weight+cur.weight; tmp.path=cur.path+", "+tmp.place; if(tmp.weight<placesToGo[posIfPresent].weight){ placesToGo[posIfPresent]=tmp; } else{ placesToGo[posIfPresent].weight+=tmpWeight; } } if(isPresentInPlacesToGo==false){ tmp.weight=tmp.weight+cur.weight; tmp.appendToPath(cur.path+", "+tmp.place); placesToGo[placesToGoCounter]=tmp; placesToGoCounter++; placesToGoSize++; } } } int smallest=-1; int smallestPos=-1; for(int i=0; i<placesToGo.length;i++){ if(smallest==-1 && placesToGo[i]!=null){ smallest=placesToGo[i].weight; smallestPos=i; } if(placesToGo[i]!=null && placesToGo[i].weight<smallest){ smallest=placesToGo[i].weight; smallestPos=i; } } NodeData smallestData = placesToGo[smallestPos]; placesToGo[smallestPos]=null; placesToGoSize--; shortestPath[shortestPathCounter]=smallestData; shortestPathCounter++; smallest=-1; smallestPos=-1; for(int i=0; i<placesToGo.length;i++){ if(smallest==-1 && placesToGo[i]!=null){ smallest=placesToGo[i].weight; smallestPos=i; } if(placesToGo[i]!=null && placesToGo[i].weight<smallest){ smallest=placesToGo[i].weight; smallestPos=i; } } if(smallestPos!=-1){cur=placesToGo[smallestPos];} } for(int i=0; i<shortestPath.length; i++){ if(shortestPath[i]!=null && shortestPath[i].place.equals(end)){ return shortestPath[i].path; } } return ""; } public String toString(){ String output=""; for(int i=0; i<array.length; i++){ output+=i+1; Iterator iter = array[i].iterator(); while(iter.hasNext()){ output+="-->"+((NodeData)iter.next()).toString(); } output+="\n"; } return output; } public class NodeData{ //public NodeData(){} public NodeData(String placeIn, int weightIn){ place=placeIn; weight=weightIn; } public String place=""; public int weight=0; public String path=place; //public String getPath(){ //return path; //} public void appendToPath(String appendRoute){ path=path+appendRoute; } //public void appendWeight(int appendWeight){ //weight=weight+appendWeight; //} //public void setWeight(int betterWeight){ //weight=betterWeight; //} //public void setPath(String betterRoute){ //path=place+", "+betterRoute; //} //public String toString(){ //return place + " " +weight; //} } }
test1.dat file:
15 congelative expugn externization flier frontsman harpylike hydrochlorplatinic Lollardry nonstriped obstetrist picamar provocative pseudolaminated scatterling unreefed Lollardry expugn 59 Lollardry scatterling 236 expugn expugn 168 expugn harpylike 208 externization obstetrist 80 externization scatterling 34 externization unreefed 185 flier unreefed 8 frontsman expugn 112 frontsman unreefed 138 harpylike harpylike 144 harpylike hydrochlorplatinic 148 hydrochlorplatinic Lollardry 124 hydrochlorplatinic expugn 136 hydrochlorplatinic externization 145 hydrochlorplatinic provocative 76 nonstriped scatterling 129 obstetrist frontsman 207 obstetrist harpylike 135 obstetrist hydrochlorplatinic 61 obstetrist nonstriped 189 obstetrist obstetrist 204 obstetrist provocative 69 picamar expugn 72 picamar flier 49 picamar harpylike 121 picamar obstetrist 119 provocative pseudolaminated 144 pseudolaminated frontsman 74 pseudolaminated hydrochlorplatinic 127 pseudolaminated nonstriped 193 pseudolaminated scatterling 23 scatterling obstetrist 131 unreefed provocative 127
Please disregard the comment lines.
Thanks again!