Program 3
Program 3
15
PROGRAMMING EXERCISE 1. Create a function int *topologicalOrder(Node *digraph, int numNodes); in JAVA, int[] topologicalOrder(Node[] digraph). It should return the array topOrder and print outputs in the form, including the nal topological order, shown below. stack = [2, 3, 4], node selected = 4 stack = [2, 3], node selected = 3 topological order = [4, 3, ] Use the following datale topologicalOrder.dat to test your program. 14 0 1 2 3 4 5 6 7 8 10 11 12 13 //numNodes ( 3): 5 9 10 //node (outdergree) and nodes adjacent to (10): 0 5 8 10 9 7 6 11 12 13 //no particular order ( 2): 5 8 ( 6): 1 5 6 8 10 11 ( 3): 0 1 5 ( 3): 6 7 10 ( 2): 7 10 ( 5): 8 10 12 11 13 ( 1): 13 ( 0): ( 0): ( 2): 10 11 ( 1): 12
Use the structure Node (or a similar class in Java) given in the lecture; the name adjNodes might be better than the name links for the array inside Node and likewise the name nodes may be better than the name digraph for Node-array. Use a function void readInput() to read tjhe datale topologicalOrder.dat; the lename can be a parameter to readInput-function. You can keep numNodes and the array nodes (or digraph) global. Use dynamic memory allocation for all arrays. The arrays inDegrees and stack should be local to topologicalOrder-function. Keep program logic, indentations, variable and names and their parameters, use of {}, etc in clean form to avoid loosing points.