25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 144<br />

7 9 1 2 4 6 3 5 8 10<br />

Fig. 4.14. Linear arrangement of the partially ordered set of Fig. 4.13.<br />

How do we proceed to find one of the possible linear orderings? The recipe is quite simple. We start by<br />

choosing any item that is not preceded by another item (there must be at least one; otherwise a loop would<br />

exist). This object is placed at the head of the resulting list <strong>and</strong> removed from the set S. The remaining set is<br />

still partially ordered, <strong>and</strong> so the same algorithm can be applied again until the set is empty.<br />

In order to describe this algorithm more rigorously, we must settle on a data structure <strong>and</strong> representation<br />

of S <strong>and</strong> its ordering. The choice of this representation is determined by the operations to be performed,<br />

particularly the operation of selecting elements with zero predecessors. Every item should therefore be<br />

represented by three characteristics: its identification key, its set of successors, <strong>and</strong> a count of its<br />

predecessors. Since the number n of elements in S is not given a priori, the set is conveniently organized as<br />

a linked list. Consequently, an additional entry in the description of each item contains the link to the next<br />

item in the list. We will assume that the keys are integers (but not necessarily the consecutive integers from<br />

1 to n). Analogously, the set of each item's successors is conveniently represented as a linked list. Each<br />

element of the successor list is described by an identification <strong>and</strong> a link to the next item on this list. If we<br />

call the descriptors of the main list, in which each item of S occurs exactly once, leaders, <strong>and</strong> the<br />

descriptors of elements on the successor chains trailers, we obtain the following declarations of data types:<br />

TYPE Leader = POINTER TO LeaderDesc;<br />

Trailer = POINTER TO TrailerDesc;<br />

LeaderDesc = RECORD key, count: INTEGER;<br />

trail: Trailer; next: Leader<br />

END;<br />

TrailerDesc = RECORD id: Leader; next: Trailer<br />

END<br />

Assume that the set S <strong>and</strong> its ordering relations are initially represented as a sequence of pairs of keys in<br />

the input file. The input data for the example in Fig. 4.13 are shown below, in which the symbols 〈 are<br />

added for the sake of clarity, symbolizing partial order:<br />

1 〈 2 2 〈 4 4 〈 6 2 〈 10 4 〈 8 6 〈 3 1 〈 3<br />

3 〈 5 5 〈 8 7 〈 5 7 〈 9 9 〈 4 9 〈 10<br />

The first part of the topological sort program must read the input <strong>and</strong> transform the data into a list<br />

structure. This is performed by successively reading a pair of keys x <strong>and</strong> y (x 〈 y). Let us denote the<br />

pointers to their representations on the linked list of leaders by p <strong>and</strong> q. These records must be located by<br />

a list search <strong>and</strong>, if not yet present, be inserted in the list. This task is perfomed by a function procedure<br />

called find. Subsequently, a new entry is added in the list of trailers of x, along with an identification of q;<br />

the count of predecessors of y is incremented by 1. This algorithm is called input phase. Figure 4.15<br />

illustrates the data structure generated during processing the given input data. The function find(w) yields<br />

the pointer to the list element with key w.<br />

In the following poece of program we make use of text scanning, a feature of the Oberon system's text<br />

concept. Instead of considering a text (file) as a sequence of characters, a text is considered as a sequence<br />

of tokens, which are identifiers, numbers, strings, <strong>and</strong> special characters (such as +, *,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!