Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Dijkstra's code help

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Dijkstra's code help

    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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Dijkstra's code help

    Do you have a design or algorithm for solving this problem? Given that, someone could help you fix your code so that it follows the algorithm.

    get the distances printed along with the city names.
    Where do you print the city names? Where is the distance value that you want to print with the name?

    Can you post what the program prints now and add some comments to it showing what the problem is
    and show what you want the output to look like.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dijkstra's code help

    Quote Originally Posted by Norm View Post
    Do you have a design or algorithm for solving this problem? Given that, someone could help you fix your code so that it follows the algorithm.

    I'm realizing a different way of Dijkstra needs to written.


    Where do you print the city names? Where is the distance value that you want to print with the name?

    The algorithm is called from the test program. and prints from:
    public String getRoute(String begin, String end)
    function. The problem I'm having is getting the actual distance to go along with the city name.

    Can you post what the program prints now and add some comments to it showing what the problem is
    and show what you want the output to look like.
    From the test function I have two cities listed:
    System.out.println(map.getRoute("Lollardry", "provocative"));
    and the output is as follows:
    Lollardry, scatterling, obstetrist, provocative

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Dijkstra's code help

    Can you post what the program prints now and add some comments to it showing what the problem is
    and show what you want the output to look like.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dijkstra's code help

    Quote Originally Posted by Norm View Post
    Can you post what the program prints now and add some comments to it showing what the problem is
    and show what you want the output to look like.
    Yes, when I run the program the output is as follows:
    Lollardry, scatterling, obstetrist, provocative
    where "Lollardry" is the starting point and "provacative" is my end point.

    What I would like to have is:
    436 Lollardry 236 scatterling 131 obstetrist 69 provocative
    where 436 is the total miles from "Lollardry" to "provocative", 236 is the distance from "Lollardry" to "scatterling", 131 is the distance from "scatterling" to "obstetrist", etc.

    I figure once I have this down, I can do the rest of the project.
    Last edited by csharp100; March 21st, 2012 at 03:13 PM. Reason: clarify info

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Dijkstra's code help

    Where are those distance values available so you can use them when formatting the String for printing?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dijkstra's code help

    Quote Originally Posted by Norm View Post
    Where are those distance values available so you can use them when formatting the String for printing?
    Well that seems to be my problem. The distances are in the linked list array. I guess I have to do a comparison of some sort in order to get the "miles". My return statement from the algorithm is:
    for(int i=0; i<shortestPath.length; i++){
    		if(shortestPath[i]!=null && shortestPath[i].place.equals(end)){
    			//System.out.println(shortestPath[i].path);
    			return shortestPath[i].path;
    			}
    	}
    	return "";
    }
    Last edited by csharp100; March 21st, 2012 at 03:57 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Dijkstra's code help

    Change what is saved in the NodeData class to include what you want to see printed.
    For debugging add a toString() method to NodeData that will return useful info to print out.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dijkstra's code help

    Quote Originally Posted by Norm View Post
    Change what is saved in the NodeData class to include what you want to see printed.
    For debugging add a toString() method to NodeData that will return useful info to print out.
    You lost me there. Do I not have a toString() method in NodeData? Can you help me out with a little code?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Dijkstra's code help

    Start by changing this:
            //public String toString(){
    		//return place + " " +weight;
    	//}
    to this:
       public String toString(){
           return "<"+place + " " +weight + ">";
       }

    Then add more variables to the returned String like path or what ever you want to see.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dijkstra's code help

    Quote Originally Posted by Norm View Post
    Start by changing this:
            //public String toString(){
    		//return place + " " +weight;
    	//}
    to this:
       public String toString(){
           return "<"+place + " " +weight + ">";
       }

    Then add more variables to the returned String like path or what ever you want to see.
    Dang this Java! I changed it like you said, ran the program and nothing changed. Can I not call this method to main? I tired
    System.out.println(toString());
    &
    System.out.println(NodeData.toString());
    and neither work. I am just trying to see what its output would be.
    Last edited by csharp100; March 21st, 2012 at 04:59 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Dijkstra's code help

    That change was for use when debugging the program. You need to change the code so it calls the toString method and prints out what is returned.
    For example print the value of the shortestPath element: shortestPath[i] just before you return its path variable.

    Where are the numbers that you want to include in the path's value? How do you get those numbers into the path variable?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dijkstra's code help

    I did this and it prints exactly what was listed above. No numbers just the "city" names.
    System.out.println(shortestPath[i].path);

    I was working on the toString() method and trying to figure where to change the code and to what in order for it to be called. I tried doing it in main just to see what was in it and to no avail

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Dijkstra's code help

    Yes it would do exactly the same. I don't think I said to print the path because it was already being printed. Leave off the .path and just print the element which will cause the toString() method to be called.

    There will not be any numbers in the print out until you figure out how to get them and write code to put them into the String being returned by getRoute().
    Last edited by Norm; March 21st, 2012 at 05:21 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dijkstra's code help

    Well back for another run at it. Can anyone at least tell me how to print out the distance with the cities? As the code is set up now, it prints
    Lollardry, scatterling, obstetrist, provocative
    I would like to have it print:
    436 Lollardry 236 scatterling 131 obstetrist 69 provocative
    Where 436 is the total miles of the cities visited, 236 is the miles between Lollardry & scatterling 131 is the miles between scatterling & obstetrist, etc.

    I have enclosed the latest code which consist of a test.java, dijkstra.java and test1.dat.
    test.java
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
     
    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(.toString());
    		//break;
     
    		//}
     
    	}
     
    }
    Dijkstra.java
    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);}
    	//System.out.println(numberOfNodes);
     
    	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));
    			//System.out.println(end);
    			//System.out.println(weight);
    			}
    	}
     
    	if(file.hasNextLine()){
    		file.nextLine();
    		//System.out.println(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;
    				//System.out.println("tmp weight" + tmp.weight);
    				tmp.weight=tmp.weight+cur.weight;
    				//System.out.println("tmp weight + cur.weight" + tmp.weight);
    				tmp.path=cur.path+", "+tmp.place;  
    				if(tmp.weight<placesToGo[posIfPresent].weight){
    					placesToGo[posIfPresent]=tmp;
    				}
     
    				else{
    					placesToGo[posIfPresent].weight+=tmpWeight;
    					//System.out.println(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)){
    			//System.out.println(shortestPath[i]);
    			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(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
    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 please! I am racking my brain here and getting nowhere. Thanks.
    Last edited by csharp100; March 22nd, 2012 at 10:10 PM.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Dijkstra's code help

    how to print out the distance with the cities?
    Where are the distance values kept? Where is the printing of the cities done?
    Can the location in the code that builds the String with the cities, access the distances and include them with the cities?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dijkstra's code help

    Quote Originally Posted by Norm View Post
    Where are the distance values kept? Where is the printing of the cities done?
    Can the location in the code that builds the String with the cities, access the distances and include them with the cities?
    Frankly that is my problem. The System.out.println(map.getRoute("start city", "end city") is in test.java where getRoute is in Dijkstra and returns right back to test.java. The return is the cities. How do I get the miles along with the cities? Would you please show me?
    Last edited by csharp100; March 22nd, 2012 at 10:33 PM.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Dijkstra's code help

    Where are the distance values you want printed?

    Is this what is printed and where you want the distances included:
      return shortestPath[i].path;

    When you build the contents of path, you need to add in the distances.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member ziplague's Avatar
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    33
    My Mood
    Cheerful
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: Dijkstra's code help

    By the way, you can make the code more readable by inserting it between "highlight=java" & "/highlight" , it will make it more colorful just use brackets instead of the (").
    Last edited by ziplague; March 24th, 2012 at 09:17 PM.

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. Help merging program code with GUI code
    By Wilha in forum AWT / Java Swing
    Replies: 2
    Last Post: January 25th, 2012, 07:03 PM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2011, 12:03 AM