title: Cost/Distance of all possible paths from source to destination node
body: <p>In my App Indoor navigation...in a specific case.... i have to find all possible paths from a specific source to a destination provided by user...
  Algo is working properly and giving all possible paths.... but how to calculate the distance too for these paths..??? 
  Here is the working algo....</p>
  
  <pre><code>class GraphFindAllPaths&lt;T&gt; implements Iterable&lt;T&gt; {
  
  /* A map from nodes in the graph to sets of outgoing edges.  Each
   * set of edges is represented by a map from edges to doubles.
   */
  public final Map&lt;T, Map&lt;T, Double&gt;&gt; graph = new HashMap&lt;T, Map&lt;T, Double&gt;&gt;();
  
  /**
   *  Adds a new node to the graph. If the node already exists then its a
   *  no-op.
   * 
   * @param node  Adds to a graph. If node is null then this is a no-op.
   * @return      true if node is added, false otherwise.
   */
  
  
  public boolean addNode(T node) {
      if (node == null) {
          throw new NullPointerException("The input node cannot be null.");
      }
      if (graph.containsKey(node)) return false;
  
      graph.put(node, new HashMap&lt;T, Double&gt;());
      return true;
  }
  
  /**
   * Given the source and destination node it would add an arc from source 
   * to destination node. If an arc already exists then the value would be 
   * updated the new value.
   *  
   * @param source                    the source node.
   * @param destination               the destination node.
   * @param length                    if length if 
   * @throws NullPointerException     if source or destination is null.
   * @throws NoSuchElementException   if either source of destination does not exists. 
   */
  public void addEdge (T source, T destination, double length) {
      if (source == null || destination == null) {
          throw new NullPointerException("Source and Destination, both should be non-null.");
      }
      if (!graph.containsKey(source) || !graph.containsKey(destination)) {
          throw new NoSuchElementException("Source and Destination, both should be part of graph");
      }
      /* A node would always be added so no point returning true or false */
      graph.get(source).put(destination, length);
  
  
  
  }
  
  /**
   * Removes an edge from the graph.
   * 
   * @param source        If the source node.
   * @param destination   If the destination node.
   * @throws NullPointerException     if either source or destination specified is null
   * @throws NoSuchElementException   if graph does not contain either source or destination
   */
  public void removeEdge (T source, T destination) {
      if (source == null || destination == null) {
          throw new NullPointerException("Source and Destination, both should be non-null.");
      }
      if (!graph.containsKey(source) || !graph.containsKey(destination)) {
          throw new NoSuchElementException("Source and Destination, both should be part of graph");
      }
      graph.get(source).remove(destination);
  }
  
  /**
   * Given a node, returns the edges going outward that node,
   * as an immutable map.
   * 
   * @param node The node whose edges should be queried.
   * @return An immutable view of the edges leaving that node.
   * @throws NullPointerException   If input node is null.
   * @throws NoSuchElementException If node is not in graph.
   */
  public Map&lt;T, Double&gt; edgesFrom(T node) {
      if (node == null) {
          throw new NullPointerException("The node should not be null.");
      }
      Map&lt;T, Double&gt; edges = graph.get(node);
      if (edges == null) {
          throw new NoSuchElementException("Source node does not exist.");
      }
      return Collections.unmodifiableMap(edges);
  }
  
  /**
   * Returns the iterator that travels the nodes of a graph.
   * 
   * @return an iterator that travels the nodes of a graph.
   */
  @Override public Iterator&lt;T&gt; iterator() {
      //System.out.println(graph.keySet().iterator());
      return graph.keySet().iterator();
  }
  }
  
  /**
   * Given a connected directed graph, find all paths between any two input points.
   */
  public class FindAllPaths&lt;T&gt; {
  
  private final  GraphFindAllPaths&lt;T&gt; graph;
  
  /**
   * Takes in a graph. This graph should not be changed by the client
   */
  public FindAllPaths(GraphFindAllPaths&lt;T&gt; graph) {
      if (graph == null) {
          throw new NullPointerException("The input graph cannot be null.");
      }
      this.graph = graph;
  }
  
  
  private void validate (T source, T destination) {
  
      if (source == null) {
          throw new NullPointerException("The source: " + source + " cannot be  null.");
      }
      if (destination == null) {
          throw new NullPointerException("The destination: " + destination + " cannot be  null.");
      }
      if (source.equals(destination)) {
          throw new IllegalArgumentException("The source and destination: " + source + " cannot be the same.");
      }
  }
  
  /**
   * Returns the list of paths, where path itself is a list of nodes.
   * 
   * @param source            the source node
   * @param destination       the destination node
   * @return                  List of all paths
   */
  public List&lt;List&lt;T&gt;&gt; getAllPaths(T source, T destination) {
      validate(source, destination);
  
      List&lt;List&lt;T&gt;&gt; paths = new ArrayList&lt;List&lt;T&gt;&gt;();
      recursive(source, destination, paths, new LinkedHashSet&lt;T&gt;());
      return paths;
  }
  
  
  // so far this dude ignore's cycles.
  private void recursive (T current, T destination, List&lt;List&lt;T&gt;&gt; paths, LinkedHashSet&lt;T&gt; path) {
      path.add(current);
  
      if (current == destination) {
          paths.add(new ArrayList&lt;T&gt;(path));
          path.remove(current);
          return;
      }
  
      final Set&lt;T&gt; edges  = graph.edgesFrom(current).keySet();
  
      for (T t : edges) {
          if (!path.contains(t)) {
              //System.out.println(t);
              recursive (t, destination, paths, path);
          }
      }
  
      path.remove(current);
  }    
  
  public static void main(String[] args) {
      GraphFindAllPaths&lt;String&gt; graphFindAllPaths = new GraphFindAllPaths&lt;String&gt;();
      graphFindAllPaths.addNode("A");
      graphFindAllPaths.addNode("B");
      graphFindAllPaths.addNode("C");
      graphFindAllPaths.addNode("D");
  
      graphFindAllPaths.addEdge("A", "B", 10);
      graphFindAllPaths.addEdge("A", "C", 15);
      graphFindAllPaths.addEdge("B", "A", 10);
      graphFindAllPaths.addEdge("C", "A", 15);
      graphFindAllPaths.addEdge("B", "D", 10);
      graphFindAllPaths.addEdge("C", "D", 20);
      graphFindAllPaths.addEdge("D", "B", 10);
      graphFindAllPaths.addEdge("D", "C", 20);
  
      graphFindAllPaths.addEdge("B", "C", 5);
      graphFindAllPaths.addEdge("C", "B", 5);
  
  
  
      FindAllPaths&lt;String&gt; findAllPaths = new FindAllPaths&lt;String&gt;(graphFindAllPaths);
  
  
  
      for (List&lt;String&gt; path :findAllPaths.getAllPaths("D", "A"))
      {
          System.out.println(path);
      }
  
  
     // assertEquals(paths, findAllPaths.getAllPaths("A", "D"));
  }
  
  
  }
  </code></pre>
  
  <p>code Prints all possible paths..</p>
  
  <pre><code>[D, B, A]
  [D, B, C, A]
  [D, C, A]
  [D, C, B, A]
  </code></pre>