@@ -0,0 +1,1681 @@
+package com.stephenramthun.algorithms.graph;%0A%0Aimport com.stephenramthun.datastructures.Graph;%0Aimport com.stephenramthun.datastructures.Vertex;%0Aimport com.stephenramthun.datastructures.LinkedList;%0A%0Aimport java.util.Collection;%0A%0Apublic class TopologicalSort %7B%0A%0A    /**%0A     * Finds a topological ordering of the vertices in a graph object and%0A     * returns this as an ordered List.%0A     * @param graph     The graph to find a topological ordering for.%0A     * @return          A List of vertices in the graph in topological ordering.%0A     *                  If no topological ordering is possible, e.g. if there is%0A     *                  a cycle in the graph, null is returned.%0A     * @see             LinkedList%0A     */%0A    public static LinkedList%3CVertex%3E sort(Graph graph) %7B%0A        graph.calculateInDegrees();%0A        LinkedList%3CVertex%3E work   = new LinkedList%3C%3E();%0A        LinkedList%3CVertex%3E sorted = new LinkedList%3C%3E();%0A        Collection vertices       = graph.getVertices();%0A%0A        for (Object v : vertices) %7B%0A            Vertex vertex = (Vertex)v;%0A            if (vertex.getInDegree() == 0) %7B%0A                work.add(vertex);%0A            %7D%0A        %7D%0A%0A        while (!work.isEmpty()) %7B%0A            Vertex v = (Vertex)work.removeFirst();%0A            sorted.add(v);%0A%0A            for (Object o : v.getEdges()) %7B%0A                Vertex edge = (Vertex)o;%0A                edge.decrementInDegree();%0A%0A                if (edge.getInDegree() == 0) %7B%0A                    work.add(edge);%0A                %7D%0A            %7D%0A        %7D%0A%0A        if (sorted.size() != vertices.size()) %7B%0A            // Graph contains cycle.%0A            return null;%0A        %7D%0A%0A        return sorted;%0A    %7D%0A%7D%0A

Add topological sort.