Results 1 to 2 of 2

Thread: Please help!

  1. #1
    Betty7 is offline Member
    Join Date
    Apr 2012
    Rep Power

    Question Please help!

    I'm a beginner in Java programming. Sorry for any silly mistakes I'm trying to do a uniform cost search program on a graph but I have some problems in the code.
    I'm not sure if my algorithm works correctly or not.
    This is the problem I get:

    Exception in thread "main" java.lang.ClassCastException: Edge cannot be cast to java.lang.Comparable
    at java.util.PriorityQueue.siftUpComparable(PriorityQ
    at java.util.PriorityQueue.siftUp( 574)
    at java.util.PriorityQueue.offer( 74)
    at java.util.PriorityQueue.add( )
    at UCS.UnifromCostSearch(
    at UCS.main(

    This is my program:

    Java Code:
    import java.util.*;
    class Vertex 
    	public String name;
    	public Vertex Parent;
    	public Edge[] adjacencies;
    	public int weight;
    	public Vertex vertex;
    	public Vertex(String vertexName)
    		name = vertexName;
    	public int getWeight()
    		return weight;
    	public void setWeight(int vertexWeight )
    		weight = vertexWeight;
    	public Edge[] getAdjacencies()
    		return adjacencies;
    	public void setAdjacencies(Edge[] adjacencies)
    		this.adjacencies = adjacencies;
    class Edge
    	public int weight;
    	public Vertex vertex;
    	public Edge(Vertex vertexTarg, int vertexWeight)
    		vertex = vertexTarg;
    		weight = vertexWeight;
    public class UCS
    	public static void UnifromCostSearch(Vertex Start, Vertex Goal)
    		PriorityQueue Fringe = new PriorityQueue();
    		ArrayList explored = new ArrayList();
    		int cost =0;
    		Vertex goal = Goal;
    			Vertex Parent = (Vertex)Fringe.poll();
    			cost += Parent.getWeight();
    			if(Parent == goal)
    				System.out.println("Goal is found: " + goal);
    	    	Edge[] children = Parent.getAdjacencies();
    			for(int i =0; i < children.length;i++)
    public static void main(String[] args)
        Vertex v0 = new Vertex("Redvile");
    	Vertex v1 = new Vertex("Blueville");
    	Vertex v2 = new Vertex("Greenville");
    	Vertex v3 = new Vertex("Orangeville");
    	Vertex v4 = new Vertex("Purpleville");
    	v0.adjacencies = new Edge[]{ new Edge(v1, 5),
    	                             new Edge(v2, 10),
                                     new Edge(v3, 8) };
    	v1.adjacencies = new Edge[]{ new Edge(v0, 5),
    	                             new Edge(v2, 3),
    	                             new Edge(v4, 7) };
    	v2.adjacencies = new Edge[]{ new Edge(v0, 10),
                                     new Edge(v1, 3) };
    	v3.adjacencies = new Edge[]{ new Edge(v0, 8),
    	                             new Edge(v4, 2) };
    	v4.adjacencies = new Edge[]{ new Edge(v1, 7),
                                     new Edge(v3, 2) };
    	Vertex[] vertices = { v0, v1, v2, v3, v4 };
        UnifromCostSearch(v0, v4);
    Any help is greatly appreciated

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Please help!

    Have you read the API documentation for the PriorityQueue class? It needs its elements to have an order; i.e. for elements A and B it needs to decide whether A < B or A > B or A == B; that makes sense for a priority queue ... The elements need to implement the Comparable interface and your Edge class doesn't do that so your Java virtual machine complains.

    kind regards,

    The only person who got everything done by Friday was Robinson Crusoe.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts