ArticleslgStudy

science

Tsort

Tsort is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Tsort rather than just read about it. In short: The tsort program is a command line utility on Unix and Unix-like platforms, that performs a topological sort on its input. It is part of the POSIX.1 standard. and has been since The Single UNIX Specification, Version 2.

Tsort — main illustration
Tsort — illustration

Key takeaways

  • Tsort belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Tsort to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Tsort from memory before moving on to harder problems.

Reference excerpt

The tsort program is a command line utility on Unix and Unix-like platforms, that performs a topological sort on its input. It is part of the POSIX.1 standard. and has been since The Single UNIX Specification, Version 2.

History According to its info page, this command was initially written for providing an ordering of object files that allowed the linker to process them sequentially (each one exactly once, and in order). The FreeBSD manual page dates its appearance to Version 7 Unix. Note that the following description is describing the behavior of the FreeBSD implementation of tsort and mentions GNU features where they may exist. Other implementations or versions may differ.

Syntax tsort [-dlq] [FILE]

FreeBSD options can be:

-d turn on debugging -l search for and display the longest cycle. -q Do not display informational messages about cycles.

GNU provides the following options only:

--help display help message and exit --version display version information and exit

There are no options prescribed by POSIX.

Behavior tsort reads its input (from the given FILE, or standard input if no input file is given or for a FILE of '-') as pairs of strings, separated by blanks, indicating a partial ordering. The output is a total ordering that corresponds to the given partial ordering. In other words: for a directed acyclic graph (used as a dependency graph), tsort produces a listing of the vertices so that for all edges 'a->b', 'a' comes before 'b' in the listing.

Examples tsort lists the vertices of a directed acyclic graph in such an order that all ordering/direction relations are respected:

Call graph tsort can help rearranging functions in a source file so that as many as possible are defined before they are used (Interpret the following as: main() calls parse_options(), tail_file() and tail_forever(); tail_file() calls pretty_name(), and so on. The result is that dump_remainder() should be defined first, start_lines() second, etc.):

Library The traditional ld (Unix linker) requires that its library inputs be sorted in topological order, since it processes files in a single pass. This applies both to static libraries (*.a) and dynamic libraries (*.so), and in the case of static libraries preferably for the individual object files contained within. BSD UNIX uses tsort as a common part of the typical ar & ranlib command invocations (from /usr/share/mk/bsd.lib.mk):

Here lorder ("library order") is used to generate the inter-file dependency list by inspecting the symbol table.

Usage notes Notice the interchangeability of white space separators so the following inputs are equivalent:

Pairs of identical items indicate presence of a vertex, but not ordering (so the following represents one vertex without edges):

a a

Strictly speaking there is no topological ordering of a graph that contains one or more cycles. However tsort prints a warning and GNU tsort prints the detected cycles to standard error (lines beginning with 'tsort:'):

See also

Sort (Unix) Make (software) Topological sorting List of Unix commands Call graph

POSIX From 1997 until 2024, the POSIX version of the tsort program accepted no arguments other than an optional file name containing the input data (it reads from standard input if no file is specified). With the 2024 version, POSIX specifies an optional -w argument that reports on the number of loops found in the exit status of the command.

References

Further reading Knuth, Donald E. (1997). The Art of Computer Programming. Vol. 1 (3rd ed.). pp. 261–268. ISBN 0-201-89683-4. Kahn, A.B. (1962). "Topological sorting of large networks". Communications of the ACM. 5 (11): 558–562. doi:10.1145/368996.369025. S2CID 16728233.

External links manual page of tsort on

FreeBSD, OpenBSD, NetBSD Archived 2016-06-03 at the Wayback Machine, AIX, Solaris, HP-UX dep-trace Orders basic dependencies and unfolds nested ones. (basic: without 2D graphical presumption)

Worked examples

Example 1 — a first encounter with Tsort

Start with the simplest possible case. Write down what Tsort claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Tsort before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Tsort ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Tsort

In research
Tsort appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Tsort in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Tsort is common in secondary-school and first-year university syllabi. It links to neighbouring topics Inferno (operating system) commands, Unix SUS2008 utilities, so understanding it makes those chapters shorter.
In everyday life
Look for Tsort outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Tsort” →

How to study Tsort in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Tsort means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Tsort out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Tsort in simple terms?

The tsort program is a command line utility on Unix and Unix-like platforms, that performs a topological sort on its input. It is part of the POSIX.1 standard. and has been since The Single UNIX Specification, Version 2.

Why does Tsort matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Tsort?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Tsort.

Tags

  • Inferno (operating system) commands
  • Unix SUS2008 utilities

Keep exploring