package edu.princeton.cs.algs4.ch42; import edu.princeton.cs.algs4.ch43.EdgeWeightedDigraph; import edu.princeton.cs.algs4.ch43.EdgeWeightedDirectedCycle; import edu.princeton.cs.introcs.*; /************************************************************************* * Compilation: javac Topoological.java * Dependencies: Digraph.java DepthFirstOrder.java DirectedCycle.java * EdgeWeightedDigraph.java EdgeWeightedDirectedCycle.java * SymbolDigraph.java * Data files: http://algs4.cs.princeton.edu/42directed/jobs.txt * * Compute topological ordering of a DAG or edge-weighted DAG. * Runs in O(E + V) time. * * % java Topological jobs.txt "/" * Calculus * Linear Algebra * Introduction to CS * Programming Systems * Algorithms * Theoretical CS * Artificial Intelligence * Machine Learning * Neural Networks * Robotics * Scientific Computing * Computational Biology * Databases * * *************************************************************************/ /** * The <tt>Topological</tt> class represents a data type for * determining a topological order of a directed acyclic graph (DAG). * Recall, a digraph has a topological order if and only if it is a DAG. * The <em>hasOrder</em> operation determines whether the digraph has * a topological order, and if so, the <em>order</em> operation * returns one. * <p> * This implementation uses depth-first search. * The constructor takes time proportional to <em>V</em> + <em>E</em> * (in the worst case), * where <em>V</em> is the number of vertices and <em>E</em> is the number of edges. * Afterwards, the <em>hasOrder</em> operation takes constant time; * the <em>order</em> operation takes time proportional to <em>V</em>. * <p> * See {@link DirectedCycle} and {@link edu.princeton.cs.algs4.ch43.EdgeWeightedDirectedCycle} to compute a * directed cycle if the digraph is not a DAG. * <p> * For additional documentation, see <a href="/algs4/42digraph">Section 4.2</a> of * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne */ public class Topological { private Iterable<Integer> order; // topological order /** * Determines whether the digraph <tt>G</tt> has a topological order and, if so, * finds such a topological order. * @param G the digraph */ public Topological(Digraph G) { DirectedCycle finder = new DirectedCycle(G); if (!finder.hasCycle()) { DepthFirstOrder dfs = new DepthFirstOrder(G); order = dfs.reversePost(); } } /** * Determines whether the edge-weighted digraph <tt>G</tt> has a topological * order and, if so, finds such an order. * @param G the edge-weighted digraph */ public Topological(EdgeWeightedDigraph G) { EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G); if (!finder.hasCycle()) { DepthFirstOrder dfs = new DepthFirstOrder(G); order = dfs.reversePost(); } } /** * Returns a topological order if the digraph has a topologial order, * and <tt>null</tt> otherwise. * @return a topological order of the vertices (as an interable) if the * digraph has a topological order (or equivalently, if the digraph is a DAG), * and <tt>null</tt> otherwise */ public Iterable<Integer> order() { return order; } /** * Does the digraph have a topological order? * @return <tt>true</tt> if the digraph has a topological order (or equivalently, * if the digraph is a DAG), and <tt>false</tt> otherwise */ public boolean hasOrder() { return order != null; } /** * Unit tests the <tt>Topological</tt> data type. */ public static void main(String[] args) { String filename = args[0]; String delimiter = args[1]; SymbolDigraph sg = new SymbolDigraph(filename, delimiter); Topological topological = new Topological(sg.G()); for (int v : topological.order()) { StdOut.println(sg.name(v)); } } } /************************************************************************* * Copyright 2002-2012, Robert Sedgewick and Kevin Wayne. * * This file is part of algs4-package.jar, which accompanies the textbook * * Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne, * Addison-Wesley Professional, 2011, ISBN 0-321-57351-X. * http://algs4.cs.princeton.edu * * * algs4-package.jar is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * algs4-package.jar is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with algs4-package.jar. If not, see http://www.gnu.org/licenses. *************************************************************************/