25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

4.3.3 An Application: Partial Ordering (Topological Sorting)<br />

An appropriate example of the use of a flexible, dynamic data structure is the process of topological<br />

sorting. This is a sorting process of items over which a partial ordering is defined, i.e., where an ordering is<br />

given over some pairs of items but not between all of them. The following are examples of partial orderings:<br />

1. In a dictionary or glossary, words are defined in terms of other words. If a word v is defined in terms<br />

of a word w, we denote this by v 〈 w. Topological sorting of the words in a dictionary means<br />

arranging them in an order such that there will be no forward references.<br />

2. A task (e.g., an engineering project) is broken up into subtasks. Completion of certain subtasks<br />

must usually precede the execution of other subtasks. If a subtask v must precede a subtask w, we<br />

write v 〈 w. Topological sorting means their arrangement in an order such that upon initiation of each<br />

subtask all its prerequisite subtasks have been completed.<br />

3. In a university curriculum, certain courses must be taken before others since they rely on the material<br />

presented in their prerequisites. If a course v is a prerequisite for course w, we write v 〈 w.<br />

Topological sorting means arranging the courses in such an order that no course lists a later course as<br />

prerequisite.<br />

4. In a program, some procedures may contain calls of other procedures. If a procedure v is called by a<br />

procedure w, we write v 〈 w. Topological sorting implies the arrangement of procedure declarations in<br />

such a way that there are no forward references.<br />

In general, a partial ordering of a set S is a relation between the elements of S. It is denoted by the<br />

symbol "〈", verbalized by precedes, <strong>and</strong> satisfies the following three properties (axioms) for any distinct<br />

elements x, y, z of S:<br />

1. if x 〈 y <strong>and</strong> y 〈 z, then x 〈 z (transitivity)<br />

2. if x 〈 y, then not y 〈 x (asymmetry)<br />

3. not z 〈 z (irreflexivity)<br />

For evident reasons, we will assume that the sets S to be topologically sorted by an algorithm are finite.<br />

Hence, a partial ordering can be illustrated by drawing a diagram or graph in which the vertices denote the<br />

elements of S <strong>and</strong> the directed edges represent ordering relationships. An example is shown in Fig. 4.13.<br />

2<br />

1<br />

10<br />

6<br />

4<br />

8 9<br />

3<br />

5<br />

7<br />

Fig. 4.13. Partially ordered set<br />

The problem of topological sorting is to embed the partial order in a linear order. Graphically, this implies<br />

the arrangement of the vertices of the graph in a row, such that all arrows point to the right, as shown in<br />

Fig. 4.14. Properties (1) <strong>and</strong> (2) of partial orderings ensure that the graph contains no loops. This is exactly<br />

the prerequisite condition under which such an embedding in a linear order is possible.

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

Saved successfully!

Ooh no, something went wrong!