diff -r 5de6a70446f6 lemon/topological_sort.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lemon/topological_sort.h	Tue Nov 25 14:55:20 2014 +0100
@@ -0,0 +1,50 @@
+#ifndef LEMON_TOPOLOGICAL_SORT_H
+#define LEMON_TOPOLOGICAL_SORT_H
+
+#include <queue>
+#include <vector>
+#include <cassert>
+
+namespace lemon {
+
+template <typename G>
+std::vector<typename G::Node> TopologicalSort(const G& graph) {
+
+    typedef typename G::Node Node;
+    typedef typename G::template NodeMap<int> NodeMap;
+    typedef typename G::NodeIt NodeIt;
+    typedef typename G::OutArcIt OutArcIt;
+
+    std::queue<Node> queue;
+
+    NodeMap deg(graph);
+
+    for (NodeIt vertex(graph); vertex != INVALID; ++vertex) {
+        deg[vertex] = countInArcs(graph, vertex);
+        if(deg[vertex] == 0) {
+            queue.push(vertex);
+        }
+    }
+
+    std::vector<Node> sorted;
+    while (!queue.empty()) {
+        Node source = queue.front();
+        queue.pop();
+
+        sorted.push_back(source);
+
+        for (OutArcIt it(graph, source); it != INVALID; ++it) {
+            Node target = graph.target(it);
+            assert(deg[target] >= 1); //Not a DAG otherwise
+            --deg[target];
+            if (deg[target] == 0) {
+                queue.push(target);
+            }
+        }
+    }
+    return sorted;
+}
+
+}
+
+#endif
diff -r 5de6a70446f6 test/CMakeLists.txt
--- a/test/CMakeLists.txt	Wed Jul 09 14:41:36 2014 +0200
+++ b/test/CMakeLists.txt	Tue Nov 25 14:55:20 2014 +0100
@@ -52,6 +52,7 @@
   random_test
   suurballe_test
   time_measure_test
+  topological_sort_test
   tsp_test
   unionfind_test
 )
diff -r 5de6a70446f6 test/topological_sort_test.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/topological_sort_test.cc	Tue Nov 25 14:55:20 2014 +0100
@@ -0,0 +1,47 @@
+
+#include <lemon/maps.h>
+#include <lemon/list_graph.h>
+#include <lemon/topological_sort.h>
+
+#include <algorithm>
+
+#include "test_tools.h"
+
+using namespace lemon;
+
+template<class T>
+unsigned indexOf(const std::vector<T>& v, const T& what) {
+    return std::find(v.begin(), v.end(), what) - v.begin();
+}
+
+int main() {
+    ListDigraph graph;
+    typedef ListDigraph::Node Node;
+
+    Node maincpp = graph.addNode();
+    Node foohpp = graph.addNode();
+    Node foocpp = graph.addNode();
+    Node barhpp = graph.addNode();
+    Node barcpp = graph.addNode();
+    Node foobarlib = graph.addNode();
+    Node aout = graph.addNode();
+
+    graph.addArc(maincpp, foohpp);
+    graph.addArc(foocpp, foohpp);
+    graph.addArc(barcpp, barhpp);
+    graph.addArc(barhpp, foohpp);
+    graph.addArc(aout, foocpp);
+    graph.addArc(aout, barcpp);
+    graph.addArc(aout, foobarlib);
+
+    std::vector<Node> sorted = TopologicalSort(graph);
+
+    check(indexOf(sorted, maincpp) < indexOf(sorted, foohpp), "ordering error");
+    check(indexOf(sorted, foocpp) < indexOf(sorted, foohpp), "ordering error");
+    check(indexOf(sorted, barcpp) < indexOf(sorted, barhpp), "ordering error");
+    check(indexOf(sorted, barhpp) < indexOf(sorted, foohpp), "ordering error");
+    check(indexOf(sorted, aout) < indexOf(sorted, foocpp), "ordering error");
+    check(indexOf(sorted, aout) < indexOf(sorted, barcpp), "ordering error");
+    check(indexOf(sorted, aout) < indexOf(sorted, foobarlib), "ordering error");
+
+}
