Summary Lecture Exercise Programming Exercises

J.E.D.I Figure 1.24 Figure 1.25 To generate the output, which is a linear ordering of the objects such that no object appears in the sequence before its direct predecessors, we proceed as follows: 1. Look for an item, say k, with count of direct predecessors equal to zero, i.e., COUNT[k] == 0. Put k in the output. 2. Scan list of direct successors of k, and decrement the count of each such successor by 1. 3. Repeat steps 1 and 2 until all items are in the output. To avoid having to go through the COUNT vector repeatedly as we look for objects with a count of zero, we will constitute all such objects into a linked queue. Initially, the queue will consist of items with no direct predecessors there will always be at least one such item. Subsequently, each time that the count of direct predecessors of an item drops to zero, it is inserted into the queue, ready for output. Since for each item, say j, in the queue, the count is zero, we can now reuse COUNT[j] as a link field such that COUNT[j] = k if k is the next item in the queue = 0 if j is the rear element in the queue Hence, we have an embedded linked queue in a sequential vector. If the input to the algorithm is correct, i.e., if the input relations satisfy partial ordering, then the algorithm terminates when the queue is empty with all n objects placed in the output. If, on the other hand, partial ordering is violated such that there are objects which constitute one or more loops for instance, 1 ≺ 2; 2 ≺ 3; 3 ≺ 4; 4 ≺ 1 , then the algorithm still terminates, but objects comprising a loop will not be placed in the output. This approach of topological sorting uses both sequential and linked allocation techniques, and the use of a linked queue that is embedded in a sequential vector.

3.6 Summary

• A queue is a linearly ordered set of elements obeying the first-in, first-out FIFO principle • Two basic queue operations are insertion at the rear and deletion at the front • Queues have 2 implementations - sequential and linked • In circular queues, there is no need to move the elements to make room for insertion • The topological sorting approach discussed uses both sequential and linked allocation techniques, as well as a linked queue embedded in a sequential vector Data Structures 48 J.E.D.I

3.7 Lecture Exercise

1. Topological Sorting. Given the partial ordering of seven elements, how can they be arranged such that no element appears in the sequence before its direct predecessor? a 1,3, 2,4, 1,4, 3,5, 3,6, 3,7, 4,5, 4,7, 5,7, 6,7, 0,0 b 1,2, 2,3, 3,6, 4,5, 4,7, 5,6, 5,7, 6,2, 0,0

3.8 Programming Exercises

1. Create a multiple-queue implementation that co-exist in a single vector. Use Garwicks algorithm for the memory reallocation during overflow. 2. Write a Java program that implements the topological sorting algorithm. 3. Subject Scheduler using Topological Sorting. Implement a subject scheduler using the topological sorting algorithm. The program shall prompt for an input file containing the subjects in the set and the partial ordering of subjects. A subject in the input file shall be of the form number, subject where number is an integer assigned to the subject and subject is the course identifier [e.g. 1, CS 1]. Every number, subject pair must be placed on a separate line in the input file. To terminate the input, use 0, 0. Prerequisites of subjects shall be retrieved also from the same file. A prerequisite definition must be of the form i, j, one line per pair, where i is the number assigned to the prerequisite of subject numbered j. Also terminate the input using 0, 0. The output is also a file where the name will be asked from the user. The output shall bear the semester number just an auto-number from 1 to n along with the subjects for the semester in table form. For simplicity, we won’t consider here year level requisites and seasonal subjects. Sample Input File Sample Output File 1, CS 1 2, CS 2 . . . 0, 0 1, 2 2, 3 . . . 0, 0  start of partial order definition Sem 1 CS 1 Sem 2 CS 2 CS 3 Data Structures 49 J.E.D.I 4 Binary Trees

4.1 Objectives