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 3 of 3

Thread: Dijstra's Algorithm Problems

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation Dijstra's Algorithm Problems

    Hey guys, So I'm trying write a dijstra's implementation in java. First off, here is the algorith:
    package lab3;
     
    import java.util.HashMap;
    import java.util.Stack;
     
    /**
     * Compute shortest paths in a graph.
     *
     * Your constructor should compute the actual shortest paths and
     * maintain all the information needed to reconstruct them.  The
     * returnPath() function should use this information to return the
     * appropriate path of edge ID's from the start to the given end.
     *
     * Note that the start and end ID's should be mapped to vertices using
     * the graph's get() function.
     */
    class ShortestPaths {
     
    	Multigraph graph;
    	final int INF = Integer.MAX_VALUE;
    	PriorityQueue<Integer> Q;
    	int n;
    	int dist[];
    	Handle handles[];
    	Edge edge[];
     
    	/**
    	 * Constructor
    	 */
    	public ShortestPaths(Multigraph G, int startId) {
    		Q = new PriorityQueue<Integer>();
    		graph = G;
    		n = graph.nVertices();
    		dist = new int [n];
    		edge = new Edge [n];
    		handles = new Handle[n];
     
    		for (int i = 0; i<n; i++){
    			dist[i] = INF;
    		}
    		dist[startId] = 0;
     
    		Handle h = Q.insert(startId, dist[startId]);
    		handles[startId] = h;	
    		Q = new PriorityQueue<Integer>();
    		while (!Q.isEmpty()){
    			Vertex v = graph.get(Q.min());
    			Q.extractMin();
    			while (v.adj().hasNext()){
    				relax(v.adj().next());	
    			}		
    		}	
    	}
     
    	private void relax(Edge e) {
    		Handle h;
    		int v = e.from().id();
    		int w = e.to().id();
    		if (dist[w] > dist[v] + e.weight()) {
    			dist[w] = dist[v] + e.weight();
    			edge[w] = e;
    			if (handles[w].getIndex() != -1){
    				Q.decreaseKey(handles[w], dist[w]);
    			}
    			else{
    				h = Q.insert(w, dist[w]);
    				handles[w] = h;
    			}
    		}
    	}
     
    	/**
    	 * Calculates the list of edge ID's forming a shortest path from the start
    	 * vertex to the specified end vertex.
    	 *
    	 * @return the array
    	 */
    	public int[] returnPath(int endId) {
    		int c = 0;
    		int[] path = new int[edge.length];
    		for (Edge e = edge[endId]; e != null; e = edge[e.from().id()]) {
    			path[c] = e.id();
    			c++;
    		}
    		return path;
    	}
     
     
    }
    I followed someone else's psuedocode very closely but for whatever reason, my edge[] array is just full of null data, which means I can't actually return the shortest path. Any ideas on why that's happening/how to fix it? Maybe I'm not understanding dijstra's correctly. Anyway, thanks for your help.


  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation Dijstra's Algorithm Problems

    Hey guys, So I'm trying write a dijstra's implementation in java. First off, here is the algorithm:
    package lab3;
     
    import java.util.HashMap;
    import java.util.Stack;
     
    /**
     * Compute shortest paths in a graph.
     *
     * Your constructor should compute the actual shortest paths and
     * maintain all the information needed to reconstruct them.  The
     * returnPath() function should use this information to return the
     * appropriate path of edge ID's from the start to the given end.
     *
     * Note that the start and end ID's should be mapped to vertices using
     * the graph's get() function.
     */
    class ShortestPaths {
     
    	Multigraph graph;
    	final int INF = Integer.MAX_VALUE;
    	PriorityQueue<Integer> Q;
    	int n;
    	int dist[];
    	Handle handles[];
    	Edge edge[];
     
    	/**
    	 * Constructor
    	 */
    	public ShortestPaths(Multigraph G, int startId) {
    		Q = new PriorityQueue<Integer>();
    		graph = G;
    		n = graph.nVertices();
    		dist = new int [n];
    		edge = new Edge [n];
    		handles = new Handle[n];
     
    		for (int i = 0; i<n; i++){
    			dist[i] = INF;
    		}
    		dist[startId] = 0;
     
    		Handle h = Q.insert(startId, dist[startId]);
    		handles[startId] = h;	
    		Q = new PriorityQueue<Integer>();
    		while (!Q.isEmpty()){
    			Vertex v = graph.get(Q.min());
    			Q.extractMin();
    			while (v.adj().hasNext()){
    				relax(v.adj().next());	
    			}		
    		}	
    	}
     
    	private void relax(Edge e) {
    		Handle h;
    		int v = e.from().id();
    		int w = e.to().id();
    		if (dist[w] > dist[v] + e.weight()) {
    			dist[w] = dist[v] + e.weight();
    			edge[w] = e;
    			if (handles[w].getIndex() != -1){
    				Q.decreaseKey(handles[w], dist[w]);
    			}
    			else{
    				h = Q.insert(w, dist[w]);
    				handles[w] = h;
    			}
    		}
    	}
     
    	/**
    	 * Calculates the list of edge ID's forming a shortest path from the start
    	 * vertex to the specified end vertex.
    	 *
    	 * @return the array
    	 */
    	public int[] returnPath(int endId) {
    		int c = 0;
    		int[] path = new int[edge.length];
    		for (Edge e = edge[endId]; e != null; e = edge[e.from().id()]) {
    			path[c] = e.id();
    			c++;
    		}
    		return path;
    	}
     
     
    }
    I followed someone else's psuedocode very closely but for whatever reason, my edge[] array is just full of null data, which means I can't actually return the shortest path. Any ideas on why that's happening/how to fix it? Maybe I'm not understanding dijstra's correctly. Anyway, thanks for your help.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dijstra's Algorithm Problems

    There are several class definitions missing. They are needed to compile and execute the code for testing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ctt-sp algorithm(cost transitive tournament shortest path algorithm)
    By seeniya in forum Java Theory & Questions
    Replies: 3
    Last Post: April 22nd, 2014, 05:36 AM
  2. Replies: 16
    Last Post: November 21st, 2013, 08:49 AM
  3. Problems with the Diamond-Square algorithm
    By Whiteclaws in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 1st, 2013, 04:42 PM
  4. Replies: 3
    Last Post: June 10th, 2013, 04:42 AM
  5. [SOLVED] Algorithm Help
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2010, 04:12 PM

Tags for this Thread