Results 1 to 9 of 9
  1. #1
    asilvester635 is offline Member
    Join Date
    Dec 2015
    Posts
    14
    Rep Power
    0

    Default Are there import statement for classes called Edge, Graph, and Vertex?

    I found this primMST java class source code online for the prim algorithm. It was accompanied by another class called BinaryMinHeap, which is used in the primMST class. I copied and pasted the primMST code in NetBeans, and it gave me error labels in the text editor for three classes that were used in the primMST class. They are class Edge, Graph, and Vertex.

    There was another error for class List, but I fixed that by using the import statement import java.util.*;

    Is there an import statement for the class called "Edge", "Graph", and "Vertex"? I've been searching google but I couldn't find anything. Below is the source code for primMST class.

    Java Code:
    package mst;
    
    import java.util.*;
    
    public class PrimMST {
        /**
         * Main method of Prim's algorithm.
         */
        public List<Edge<Integer>> primMST(Graph<Integer> graph){
    
            //binary heap + map data structure
            BinaryMinHeap<Vertex<Integer>> minHeap = new BinaryMinHeap<>();
    
            //map of vertex to edge which gave minimum weight to this vertex.
            Map<Vertex<Integer>,Edge<Integer>> vertexToEdge = new HashMap<>();
    
            //stores final result
            List<Edge<Integer>> result = new ArrayList<>();
    
            //insert all vertices with infinite value initially.
            for(Vertex<Integer> v : graph.getAllVertex()){
                minHeap.add(Integer.MAX_VALUE, v);
            }
    
            //start from any random vertex
            Vertex<Integer> startVertex = graph.getAllVertex().iterator().next();
    
            //for the start vertex decrease the value in heap + map to 0
            minHeap.decrease(startVertex, 0);
    
            //iterate till heap + map has elements in it
            while(!minHeap.empty()){
                //extract min value vertex from heap + map
                Vertex<Integer> current = minHeap.extractMin();
    
                //get the corresponding edge for this vertex if present and add it to final result.
                //This edge wont be present for first vertex.
                Edge<Integer> spanningTreeEdge = vertexToEdge.get(current);
                if(spanningTreeEdge != null) {
                    result.add(spanningTreeEdge);
                }
    
                //iterate through all the adjacent vertices
                for(Edge<Integer> edge : current.getEdges()){
                    Vertex<Integer> adjacent = getVertexForEdge(current, edge);
                    //check if adjacent vertex exist in heap + map and weight attached with this vertex is greater than this edge weight
                    if(minHeap.containsData(adjacent) && minHeap.getWeight(adjacent) > edge.getWeight()){
                        //decrease the value of adjacent vertex to this edge weight.
                        minHeap.decrease(adjacent, edge.getWeight());
                        //add vertex->edge mapping in the graph.
                        vertexToEdge.put(adjacent, edge);
                    }
                }
            }
            return result;
         
        } // end of primMST
    
        private Vertex<Integer> getVertexForEdge(Vertex<Integer> v, Edge<Integer> e){
            return e.getVertex1().equals(v) ? e.getVertex2() : e.getVertex1();
        }
     
    } // end of PrimMST class

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,189
    Rep Power
    12

    Default Re: Are there import statement for classes called Edge, Graph, and Vertex?

    IDE's should import them automatically if they can find them (should be on the class path). Otherwise, you need to do it yourself. Since you have the source code the classes can imported specifying the package name followed by a .*. The package is the first statement in a Java file.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Are there import statement for classes called Edge, Graph, and Vertex?

    for class List, but I fixed that by using the import statement import java.util.*
    You do not need to use import statements. For example the references to the List class could be changed to include the full package name:
    Java Code:
     java.util.List<Edge<Integer>> result = new java.util.ArrayList<>();
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    asilvester635 is offline Member
    Join Date
    Dec 2015
    Posts
    14
    Rep Power
    0

    Default Re: Are there import statement for classes called Edge, Graph, and Vertex?

    Where in the source code do I put the java code java.util.List<Edge<Integer>> result = new java.util.ArrayList<>();? Would it be the first statement inside the class PrimMST?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Are there import statement for classes called Edge, Graph, and Vertex?

    That code was a replacement for the statement at line 18.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,189
    Rep Power
    12

    Default Re: Are there import statement for classes called Edge, Graph, and Vertex?

    I don't think that would work since the OP also needs the package name of Edge (and Graph and Vertex) as stated in post #1.

    Regards,
    Jim
    Last edited by Norm; 04-21-2017 at 03:17 PM. Reason: Fixed "don't"
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Are there import statement for classes called Edge, Graph, and Vertex?

    Those classes will have been in package 'mst' too and hence don't need importing. You will have to find them or reinvent them such that the code will still work. That would be a good exercise ;)

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,189
    Rep Power
    12

    Default Re: Are there import statement for classes called Edge, Graph, and Vertex?

    Ugh. This post should have been "I don't think..." Anyway, It's only been 2 days and I couldn't edit it. I believe I have been able to edit much older posts before. Has something changed regarding this capability or am I remembering incorrectly?

    Regards,
    Jim
    Last edited by jim829; 04-21-2017 at 02:58 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: Are there import statement for classes called Edge, Graph, and Vertex?

    Who knows with this place.
    It's full of surprises!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 35
    Last Post: 12-09-2013, 07:19 PM
  2. Replies: 0
    Last Post: 02-17-2013, 09:21 PM
  3. To import CSV file into java for Plotting graph.
    By Niisha in forum Advanced Java
    Replies: 0
    Last Post: 03-25-2012, 05:31 PM
  4. If Statement Never Gets Called (SUPER EASY!)
    By skaterboy987 in forum New To Java
    Replies: 3
    Last Post: 10-16-2011, 03:44 AM

Posting Permissions

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