package com.stephenramthun.algorithms.graph;

import com.stephenramthun.datastructures.Graph;
import com.stephenramthun.datastructures.Vertex;
import com.stephenramthun.datastructures.PriorityQueue;

import java.util.HashSet;
import java.util.Map;

public class Dijkstra {

    /**
     * Finds the shortest paths from a source vertex to all other vertices in
     * the graph.
     * @param graph     Graph to use as basis for search.
     * @param source    Start vertex from where the distances are
     *                  calculated.
     */
    @SuppressWarnings("unchecked")
    public static void run(Graph graph, Vertex source) {
        PriorityQueue<Vertex> queue = new PriorityQueue<>();
        HashSet<Vertex> inQueue = new HashSet<>();

        for (Object o : graph.getVertices()) {
            Vertex v = (Vertex)o;
            v.setDistance(Double.MAX_VALUE);
            v.setPrevious(null);
        }

        source.setDistance(0);
        queue.add(source);
        inQueue.add(source);

        while (!queue.isEmpty()) {
            Vertex v = queue.poll();

            Map map = v.getEdges();

            for (Object o : map.entrySet()) {
                Map.Entry<Vertex, Double> edge = (Map.Entry<Vertex, Double>)o;
                Vertex vertex = edge.getKey();
                double weight = edge.getValue();

                double distance = v.getDistance() + weight;

                if (distance < vertex.getDistance()) {
                    vertex.setDistance(distance);
                    vertex.setPrevious(v);
                    // If vertex not in queue, add to queue
                    if (!inQueue.contains(vertex)) {
                        queue.add(vertex);
                        inQueue.add(vertex);
                    }
                }
            }
        }
    }
}

@@ -242,16 +242,408 @@
 l.Map;%0A%0A
+/**%0A * Single source shortest path algorithm for graphs with non-negative weights,%0A * based on Dijkstra's algorithm. Uses a priority queue implementation to keep%0A * track of the currently considered edges with smallest weights at each step%0A * of the algorithm.%0A *%0A * @author Stephen Ramthun%0A * @see %3Ca href=%22https://en.wikipedia.org/wiki/Dijkstra%2527s_algorithm%22%3EDijkstra's Algorithm%3Ca/%3E%0A */%0A%0A
 public c
@@ -1939,67 +1939,8 @@
 v);%0A
-                    // If vertex not in queue, add to queue
 %0A   

Add documentation and attribution.