The Monthly Course: Topological Sort


Goal

Perform a topological sort (partial ordered sort). For all the partial ordering relationships given between nodes in an input file, output each node in an order that obeys the partial order.

(from "Mastering Algorithms with Perl")
"A Topological sort is a listing of the vertices of a graph in such an order that all the ordering relations are respected.
...
More precisely: topological sort of a directed acyclic graph is a listing of the vertices so that for all edges u-v, u comes before v in the listing."


Specification

The program is a filter: it must read from STDIN, and send output to STDOUT.


Tiebreaker

The tiebreaker favors 1) white space, and then 2) letters.


General rules


Examples

Given the input:

code test
design code

You are to output the following:

design
code
test

Given the input:

code test
design write_tests
design code
write_tests test

You are to output one of the following two possible solutions:

design
code
write_tests
test

or:

design
write_tests
code
test

Given the input:

code test
design code
test design

You are to exit with a non-zero exit code because this input contains a cycle. (The output does not matter.)


Deadline

The game starts July 1st (05:00 UTC) and ends July 8th (05:00 UTC).


Test program

WARNING: new test program!!

A test program (version 1.9) is provided to help screen entries (for those of you who feel courageous, there's also the Games::Golf version - be warned: it's still an experimental module, use at your own risks).

Any program that passes the test program should be submitted. If you are surprised that your solution passed the test program, please submit it anyway! That will help us identify bugs in the test program.

For the test program to work correctly, you will have to name your script tsort.pl and place it in the same directory as your test program. Run the test program:

    $ perl tpr04b.pl

to verify that your entries are valid.

Passing the test program does not assure your solution is valid. The referees have the final say.


Submitting

You can submit your solutions here (you'll notice it's the same page as the Leaderboard).

Do not publish your solutions anywhere. That will spoil the game, as your solutions are meant to be secret. All solutions will be published at the end of the game.

Prizes (provided by O'Reilly and ActiveState) will be awarded to veteran and beginner winners. A prize may also be awarded to any especially interesting artistic and/or unorthodox solutions.


Leaderboard

You can track your ranking through the leaderboard here. Beginners are encouraged to enter and there is a separate leaderboard for them.

There is also a special leaderboard for teams. There will be no prizes awarded to the best team, other than the admiration of your fellow golfers. If you are in a team, you can't also play individually.


Feedback

We encourage you to send feedback as well as your ideas for future holes and tiebreakers to golf@theperlreview.com.


Referees

If you want to be a referee next month, drop us a note: golf@theperlreview.com