2

Sorry if this a silly or wrong question, I found a solution in Java for short path algorithm. Here is the code:

import java.io.*;
import java.util.*;

public class Dijkstra {
   private static final Graph.Edge[] GRAPH = {
      new Graph.Edge("a", "b", 7),
      new Graph.Edge("a", "c", 9),
      new Graph.Edge("a", "f", 14),
      new Graph.Edge("b", "c", 10),
      new Graph.Edge("b", "d", 15),
      new Graph.Edge("c", "d", 11),
      new Graph.Edge("c", "f", 2),
      new Graph.Edge("d", "e", 6),
      new Graph.Edge("e", "f", 9),
   };
   private static final String START = "a";
   private static final String END = "e";

   public static void main(String[] args) {
      Graph g = new Graph(GRAPH);
      g.dijkstra(START);
      g.printPath(END);
      //g.printAllPaths();
   }
}

class Graph {
   private final Map<String, Vertex> graph; // mapping of vertex names to Vertex objects, built from a set of Edges

   /** One edge of the graph (only used by Graph constructor) */
   public static class Edge {
      public final String v1, v2;
      public final int dist;
      public Edge(String v1, String v2, int dist) {
         this.v1 = v1;
         this.v2 = v2;
         this.dist = dist;
      }
   }

   /** One vertex of the graph, complete with mappings to neighbouring vertices */
   public static class Vertex implements Comparable<Vertex> {
      public final String name;
      public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity
      public Vertex previous = null;
      public final Map<Vertex, Integer> neighbours = new HashMap<>();

      public Vertex(String name) {
         this.name = name;
      }

      private void printPath() {
         if (this == this.previous) {
            System.out.printf("%s", this.name);
         } else if (this.previous == null) {
            System.out.printf("%s(unreached)", this.name);
         } else {
            this.previous.printPath();
            System.out.printf(" -> %s(%d)", this.name, this.dist);
         }
      }

      public int compareTo(Vertex other) {
         return Integer.compare(dist, other.dist);
      }
   }

   /** Builds a graph from a set of edges */
   public Graph(Edge[] edges) {
      graph = new HashMap<>(edges.length);

      //one pass to find all vertices
      for (Edge e : edges) {
         if (!graph.containsKey(e.v1)) graph.put(e.v1, new Vertex(e.v1));
         if (!graph.containsKey(e.v2)) graph.put(e.v2, new Vertex(e.v2));
      }

      //another pass to set neighbouring vertices
      for (Edge e : edges) {
         graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist);
         //graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // also do this for an undirected graph
      }
   }

   /** Runs dijkstra using a specified source vertex */ 
   public void dijkstra(String startName) {
      if (!graph.containsKey(startName)) {
         System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName);
         return;
      }
      final Vertex source = graph.get(startName);
      NavigableSet<Vertex> q = new TreeSet<>();

      // set-up vertices
      for (Vertex v : graph.values()) {
         v.previous = v == source ? source : null;
         v.dist = v == source ? 0 : Integer.MAX_VALUE;
         q.add(v);
      }

      dijkstra(q);
   }

   /** Implementation of dijkstra's algorithm using a binary heap. */
   private void dijkstra(final NavigableSet<Vertex> q) {      
      Vertex u, v;
      while (!q.isEmpty()) {

         u = q.pollFirst(); // vertex with shortest distance (first iteration will return source)
         if (u.dist == Integer.MAX_VALUE) break; // we can ignore u (and any other remaining vertices) since they are unreachable

         //look at distances to each neighbour
         for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) {
            v = a.getKey(); //the neighbour in this iteration

            final int alternateDist = u.dist + a.getValue();
            if (alternateDist < v.dist) { // shorter path to neighbour found
               q.remove(v);
               v.dist = alternateDist;
               v.previous = u;
               q.add(v);
            } 
         }
      }
   }

   /** Prints a path from the source to the specified vertex */
   public void printPath(String endName) {
      if (!graph.containsKey(endName)) {
         System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName);
         return;
      }

      graph.get(endName).printPath();
      System.out.println();
   }
   /** Prints the path from the source to every vertex (output order is not guaranteed) */
   public void printAllPaths() {
      for (Vertex v : graph.values()) {
         v.printPath();
         System.out.println();
      }
   }
}

I understood most of this algorithm, but the method private void dijkstra(final NavigableSet<Vertex> q) confuses me by the following question:

  1. How is it being evaluated by rest of the code, as it doesnt have any return method?
  2. Is using NavigableSet / TreeSet better than using PriorityQueue?

And, also I have a question about compareTo method which is overriden in class Vertex, how is it being called?

Thanks

Kite Runner
  • 137
  • 2
  • 4
  • 9

3 Answers3

1
  1. How is it being evaluated by rest of the code, as it doesnt have any return method?

The parameter of the method is a mutable container which is referenced by the parts of the algorithm.

 final Vertex source = graph.get(startName);
  NavigableSet<Vertex> q = new TreeSet<>();

  // set-up vertices
  for (Vertex v : graph.values()) {
     v.previous = v == source ? source : null;
     v.dist = v == source ? 0 : Integer.MAX_VALUE;
     q.add(v);
  }

  dijkstra(q);
  1. Is using NavigableSet / TreeSet better than using PriorityQueue? you can find answer in this question Difference between PriorityQueue and TreeSet in Java?
Community
  • 1
  • 1
Jade Tang
  • 321
  • 4
  • 23
  • Parameter of the method is a NavigableSet, but it is not being referenced in other parts of the algorithm, that is what is confusing me. I would imagine to have the method return that NavigableSet in order for it to be made use of. Could you explain it a bit more please? – Kite Runner Aug 08 '16 at 09:08
  • Thanks for the edit, I am a novice to Java just want to improve algorithm knowledge. So, the 'q' which is passed as paramter to dijkstra, is not being returned but still it is used in determining the path, that is what is confusing me, hope I made myself clear. – Kite Runner Aug 08 '16 at 09:48
  • v.dist = alternateDist; this line change the dist value of the vertex which is 0 or Integer.MAV_VALUE initially. – Jade Tang Aug 08 '16 at 09:58
  • that I understood, and even the v.previous part, how the shortest distance is calculated in the dinkstra method is also clear to me, but not how that is going else where, to print the path, how printPath() method is using this q NavigableSet. – Kite Runner Aug 08 '16 at 10:05
1

1) There are two methods with the same name:

public void dijkstra(String startName)
private void dijkstra(final NavigableSet<Vertex> q)

If you call dijkstra("a"), the first method will be called, and if you call diijkstra(q) where q is a NavigableSet<Vertex>, then the second method will be called.


2) This depends on the underlying data structure used to implement TreeSet and PriorityQueue, as well as the input Graph. For some input graphs, an unsorted array could perform faster than a min-heap.


3) I suspect it will be a requirement for objects added to NavigableSet to have a working compareTo() method so that NavigableSet can work it's magic.

wookie919
  • 3,054
  • 24
  • 32
1

How is it being evaluated by rest of the code, as it doesn't have any return method?

The part that is specifically doing the evaluating is:

u = q.pollFirst();

The part that effects what q.pollFirst() returns is:

if (alternateDist < v.dist) { // shorter path to neighbour found
    q.remove(v);
    v.dist = alternateDist;
    v.previous = u;
    q.add(v);
}

The v node gets removed from the set, it's distance is being updated, then it is re-added to the set.

The distance being updated is the most important part.

The node being removed then re-added is probably required so that the node is ordered by the new distance value rather than the old distance value.

The point of all that is so q.pollFirst() returns the node with the shortest distance.

Is using NavigableSet / TreeSet better than using PriorityQueue?

"better" is subjective. Are you looking for speed or a nicer interface, or a particular data structure?

From what I understand, both TreeSet and PriorityQueue uses Comparable to order nodes, so in that sense they work similarly.

Besides that, TreeSet is a set, so nodes can only exists once in the set, whereas, PriorityQueue is a queue and can have the same node inserted multiple times.

In this case, a set seems to work fine for the dijkstra algorithm.

How is the compareTo method which is overriden in class Vertex being called?

The compareTo function is used internally by the TreeSet to order the nodes as they are being added.

The new node is compared against the nodes already in the set. The Vertex#compareTo provides the algorithm to determine how two Vertex compare with one another. In this case, the distance value of the Vertex is compared.

This also hints as to why the nodes are removed and re-added to the set in the dijkstra(final NavigableSet<Vertex> q) function.

br3nt
  • 9,017
  • 3
  • 42
  • 63