src/com/michaelho/GraphAlgorithms/GR1.java
/**
         * The function that output the value of vertices in topological order. Note that a graph can
         * have multiple topological output.
         * Runtime: O(V+E)
         *
         * @param graph The input graph.
         *
         * @return The values of vertices in the graph output in topological order.
         * */
The function that output the value of vertices in topological order. Note that a graph can
have multiple topological output.

@param graph The input graph.

@return The values of vertices in the graph output in topological order.
The function that output the value of vertices in topological order . Note that a graph can have multiple topological output . @param graph The input graph . @return The values of vertices in the graph output in topological order .
int[] topologicalSort(Graph graph) {
            Stack<Integer> stack = new Stack<>();
            boolean[] visited = new boolean[graph.V];
            for (int i = 0; i < graph.V; i++)
                if (!visited[i])
                    topologicalSortHelper(graph, stack, i, visited);
            List<Integer> output = new ArrayList<>();
            while (!stack.empty())
                output.add(stack.pop());
            return output.stream().mapToInt(Integer::intValue).toArray();
        }
int [ ] topologicalSort ( Graph graph ) { Stack < Integer > stack = new Stack < > ( ) ; boolean [ ] visited = new boolean [ graph . V ] ; for ( int i = 0 ; i < graph . V ; i ++ ) if ( ! visited [ i ] ) topologicalSortHelper ( graph , stack , i , visited ) ; List < Integer > output = new ArrayList < > ( ) ; while ( ! stack . empty ( ) ) output . add ( stack . pop ( ) ) ; return output . stream ( ) . mapToInt ( Integer :: intValue ) . toArray ( ) ; }
The function that output the value of vertices in topological order.
The function that output the value of vertices in topological order .