0% found this document useful (0 votes)
13 views

Program 3

The document describes a programming exercise to implement a topological sorting algorithm to return the topological order of nodes in a directed graph. It provides sample output and test data file to test the implementation. The algorithm uses a stack and indegrees of nodes to iteratively find the topological order. The program is to be implemented in Java using Node class to represent graph nodes, with functions to read input file, calculate topological order and print results.

Uploaded by

Bob Johnson
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Program 3

The document describes a programming exercise to implement a topological sorting algorithm to return the topological order of nodes in a directed graph. It provides sample output and test data file to test the implementation. The algorithm uses a stack and indegrees of nodes to iteratively find the topological order. The program is to be implemented in Java using Node class to represent graph nodes, with functions to read input file, calculate topological order and print results.

Uploaded by

Bob Johnson
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

2.

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.

You might also like