0

Can somebody explain to me what is "Weight decoration object" in the code below? Whats kind of parameters should I input? For example, if I want to find shortest path from two airport, I have String Vertices and weighted edges (int). Thanks!

    public class Dijkstra<V, E> {
    /** Infinity value. */
    protected static final Integer INFINITE = Integer.MAX_VALUE;

    /** Input graph. */
    protected Graph<V, E> graph;

    /** Decoration key for edge weights */
    protected Object WEIGHT;

    /** Decoration key for vertex distances */
    protected Object DIST = new Object();

    /** Decoration key for entries in the priority queue */
    protected Object ENTRY = new Object();

    /** Auxiliary priority queue. */
    protected AdaptablePriorityQueue<Integer, Vertex<V>> Q;

    /**
     * Executes Dijkstra's algorithm.
     * 
     * @param g
     *            Input graph
     * @param s
     *            Source vertex
     * @param w
     *            Weight decoration object
     */
    public void execute(Graph<V, E> g, Vertex<V> s, Object w) {
        graph = g;
        WEIGHT = w;
        DefaultComparator<Integer> dc = new DefaultComparator<Integer>();
        Q = new HeapAdaptablePriorityQueue<Integer, Vertex<V>>(dc);
        dijkstraVisit(s);
    }

    /**
     * Get the distance of a vertex from the source vertex. //end#fragment
     * execute This method returns the length of a shortest path from the   source
     * to <tt>u</tt> after {@link #execute(Graph,Vertex,Object) execute} has
     * been called. //begin#fragment execute
     * 
     * @param u
     *            Start vertex for the shortest path tree
     */
    public int getDist(Vertex<V> u) {
        return (Integer) u.get(DIST);
    }

    /**
     * The actual execution of Dijkstra's algorithm.
     * 
     * @param v
     *            source vertex.
     */
    @SuppressWarnings("unchecked")
    protected void dijkstraVisit(Vertex<V> v) {
        // store all the vertices in priority queue Q
        for (Vertex<V> u : graph.vertices()) {
            int u_dist;
            if (u == v)
                u_dist = 0;
            else
                u_dist = INFINITE;
            Entry<Integer, Vertex<V>> u_entry = Q.insert(u_dist, u); // autoboxing
            u.put(ENTRY, u_entry);
        }
        // grow the cloud, one vertex at a time
        while (!Q.isEmpty()) {
            // remove from Q and insert into cloud a vertex with minimum
            // distance
            Entry<Integer, Vertex<V>> u_entry = Q.min();
            Vertex<V> u = u_entry.getValue();
            int u_dist = u_entry.getKey();
            Q.remove(u_entry); // remove u from the priority queue
            u.put(DIST, u_dist); // the distance of u is final
            u.remove(ENTRY); // remove the entry decoration of u
            if (u_dist == INFINITE)
                continue; // unreachable vertices are not processed
            // examine all the neighbors of u and update their distances
            for (Edge<E> e : graph.incidentEdges(u)) {
                Vertex<V> z = graph.opposite(u, e);
                Entry<Integer, Vertex<V>> z_entry = 
                    (Entry<Integer, Vertex<V>>) z.get(ENTRY);
                if (z_entry != null) { // check that z is in Q, i.e., not in
                                        // the cloud
                    int e_weight = (Integer) e.get(WEIGHT);
                    int z_dist = z_entry.getKey();
                    if (u_dist + e_weight < z_dist) // relaxation of edge e =
                                                    // (u,z)
                        Q.replaceKey(z_entry, u_dist + e_weight);
                }
            }
        }
    }
}
Codez
  • 1
  • 1
  • It's just a random object that's a key such that `e.get(WEIGHT)` returns the edge's weight. It can be any object, but you can imagine it to be e.g. the string `"Weight"`. – Louis Wasserman Aug 03 '16 at 00:12
  • Thanks. I try a random object, it gives me a null pointer exception. If I have a graph and start Vertex, I do not understand why there is a weight object? – Codez Aug 03 '16 at 00:23
  • Well, of course. It has to be the object used in creating the graph. The weight object is what you use to look at the graph, it isn't an element of the graph. – Louis Wasserman Aug 03 '16 at 00:25
  • Thanks, Louis. Can you please give me an example? How can I make this execute method works? (e.g.I have input like" YSL YYT 300", and I stored first two String as two vertices in a graph and 300 will be my weighted edge) – Codez Aug 03 '16 at 00:38
  • I don't know how your Edge type is implemented or how the edges of the graph were constructed. But however it was constructed is probably how you get it out afterwards. – Louis Wasserman Aug 03 '16 at 00:56
  • The Edge and Vertex are two interfaces. public interface Vertex extends DecorablePosition { /** Returns the element associated with the vertex. */ V getElement(); } public interface DecorablePosition extends Position, Map { } – Codez Aug 03 '16 at 02:01
  • Okay. And how do the edges get constructed? – Louis Wasserman Aug 03 '16 at 02:06
  • At some point in the graph construction there has to be a line `edge.put(something, weight)`, though the variable names may be different. Whatever that something is is what you need. – Louis Wasserman Aug 03 '16 at 02:08
  • That tells me the implementation of the graph, but it doesn't tell me how the graph is constructed. – Louis Wasserman Aug 03 '16 at 02:18
  • Although to be honest it looks like it's just passed into your code and it isn't something you have to worry about. But the code you've showed doesn't say anything about where the weight key comes from. Where is the code that converts that string you gave into a Graph object? – Louis Wasserman Aug 03 '16 at 02:20
  • I split the user input "YSL YYT 300" to three parts, then insert "YSL" as a vertice and "YYT" as another vertice. And insert an edge with weight 300 between these two vertices. – Codez Aug 03 '16 at 02:33
  • *How* do you insert the edge with weight 300? Show me the code. – Louis Wasserman Aug 03 '16 at 02:39
  • Thanks, Louis. I test the graph, it successfully store the vertices and edges. The only thing confused me is this object. I don't know where it comes from, I test this get(key) method, still don't understand what is key for this vertex. – Codez Aug 03 '16 at 02:41
  • Based on that, I am fairly confident that's not how edges are intended to be inserted. If you create the edge that way then there is no possible way your algorithm could work. The third argument to insertEdge is intended to be something different, and you're supposed to do `e12.put(something, weight)`. – Louis Wasserman Aug 03 '16 at 02:49
  • Based on the Dijkstra class you've shown me that's the only possible explanation: you're not creating the graph the way it's intended to be created. – Louis Wasserman Aug 03 '16 at 02:50
  • Thanks. What do you think this Weight Decoration object refer to? key of Vertex? key of Edge? Is it just a symbol for Vertex or Edge object to get correspond value? If so, can I just do like, v1.put("v1",value); v2.put("v1",value); e12.put("e12",value) ? – Codez Aug 03 '16 at 03:07
  • No, definitely not. You have to use the same key for the weight of all edges, e.g. `e12.put("weight", 300)` – Louis Wasserman Aug 03 '16 at 03:21
  • But, I mean, isn't key suppose be a set that not allow any duplicate? So the new one will replace the old one? – Codez Aug 03 '16 at 03:27
  • Each edge has its own map, each of which has only distinct keys within itself, but different edges (and different maps) can have the same key (and absolutely must for this to work) – Louis Wasserman Aug 03 '16 at 03:35
  • Thanks, man. It works. You are my hero. lol – Codez Aug 03 '16 at 04:12

0 Answers0