import // initialise instance variables n = 10;

import java.util.*;import java.io.*;/** * Class Topological  * The main method creates and populates a graph from edges in file * Edges.txt. It then calls method TopologicalOrder() which you are asked to * code using the final algorithm for this presented in lectures (slide 13  * of handout 20) *  * @author I.Ross  */public class Topological{    // instance variables – do not alter    private DiGraphADT g;    private int n;        /**     * Constructor for objects of class Topological – do not alter except     * that you may wish to replace the instance of DiGraphEdgeList with an      * instance of a different implementation of DiGraphADT     */    public Topological()    {        // initialise instance variables        n = 10;   // example graph used here has 10 nodes        g = new AdjacencyMatrix(n);        try {            readGraph();        }        catch (Exception e){            System.out.println(“Invalid file content”);        }    }    /**    * populates the graph taking data from file Edges.txt – do not alter    */    private void readGraph() throws Exception     {        int firstNode, secNode;        Reader r = new BufferedReader(new FileReader(“Edges.txt”));        StreamTokenizer st = new StreamTokenizer(r);        st.parseNumbers();        st.nextToken();        while (st.ttype != StreamTokenizer.TT_EOF) {          firstNode = (int) st.nval;          st.nextToken();          secNode = (int) st.nval;          System.out.println(firstNode + ” ” + secNode);          g.addEdge(firstNode,secNode);          st.nextToken();        }    }   /** prints a Topological ordering of the nodes in the graph, using     * the improved algorithm presented in lectures. This algorithm makes use    * of a queue holding nodes of degree 0 * @return    */    public void TopologicalOrder()    {                // create an empty queue        Queue ZeroQueue = new LinkedList();        Queue FinalQueue = new LinkedList();        int counts = new int10;                // populate an array with initially holding the indegrees of nodes        // and place nodes with indegree 0 on the queue        for(int i = 0; i < n; i++)        {                        countsi = g.inDegree(i);                        // if the in degree of the node is equal to zero            if(countsi == 0)            {                ZeroQueue.add(i);                           }        }                // repeatedly remove a node from the queue, print it (i.e. display         // the node number), decrement the array entry for all its successor         // nodes and add to the queue any nodes whose indegree hence falls to 0                while(!ZeroQueue.isEmpty())        {                int node = ZeroQueue.remove();        FinalQueue.add(node);                for (int Successor : g.successors(node))        {            // decrement the count of the successor nodes for this node            countsSuccessor --;            // if the in degree for the successor is now equal to zero            if(countsSuccessor == 0)            {                ZeroQueue.add(Successor);            }                    }        }                        // print the output        System.out.println(" TOPOLOGICAL ORDERING ");                for ( int v: FinalQueue)            {                System.out.println(v);            }            }            /** creates and populates a graph and then prints a Topological     * ordering of the nodes  - do not alter     */        public static void main(String args)    {        Topological gr = new Topological();        gr.TopologicalOrder();                 }}

Related Posts

© All Right Reserved
x

Hi!
I'm Melba!

Would you like to get a custom essay? How about receiving a customized one?

Check it out