public static <V> List<V> topologicalSort(final DirectedGraph<V> g) {
        List<V> ts = new LinkedList<>();
        int inDegree[] = new int[g.getNumberOfVertexes()];
        Queue<V> q = new LinkedList<>();

        int i = 0;
        // for every vertex...
        for (V vertex : g.getVertexList()) {
            inDegree[i] = g.getPredecessorVertexList(vertex).size();
            if (inDegree[i] == 0) {
                q.add(vertex);
            }
            i++;
        }

        V v;
        int j = 0;
        while (!q.isEmpty()) {
            v = q.remove();
            ts.add(v);
            for (V vertex : g.getSuccessorVertexList(v)) {
                j = 0;
                for (V vertex2 : g.getVertexList()) {
                    if (vertex2 == vertex) {
                        break;
                    }
                    j++;
                }
                if (--inDegree[j] == 0) {
                    q.add(vertex);
                }
            }
        }

        if (ts.size() != g.getNumberOfVertexes()) {
            // Graph with cycle
            throw new IllegalStateException("Zyklus gefunden!");
        } else {
            return ts;
        }
    }