← Runnable Projects

阅读中文版本

Python Topological Sort CLI

Python Topological Sort — a runnable python project with source, install and run commands, and expected output.

intermediatepythonunverified

Project

py-topo-sort-cli

Entry point: topo.py

Install

python topo.py edges.txt

Run

python topo.py sample.txt

Test

pytest topo_test.py

Expected output

Kahn algorithm topological sort over 'a b' dependency edges.

Dependencies

  • python >=3.10

Verification evidence

Source reviewed against python standard library APIs. Commands verified for syntax; execution not yet exercised in CI.

Security note

Reads only the file or arguments passed on the command line; makes no network requests and writes nothing outside stdout.

What it does

Python Topological Sort CLI is a self-contained python command-line program. Kahn algorithm topological sort over ‘a b’ dependency edges.

Install and run

python topo.py edges.txt
python topo.py sample.txt

Project structure

py-topo-sort-cli/
├── topo.py
└── README snippet above

Key source

# topo.py
import sys
from collections import defaultdict, deque
adj, indeg = defaultdict(list), defaultdict(int)
for line in open(sys.argv[1]):
    a, b = line.split()
    adj[b].append(a); indeg[a] += 1; indeg.setdefault(b, 0)
q = deque(n for n in indeg if indeg[n] == 0)
out = []
while q:
    n = q.popleft(); out.append(n)
    for m in adj[n]:
        indeg[m] -= 1
        if indeg[m] == 0: q.append(m)
print(" ".join(out) if len(out) == len(indeg) else "cycle")

Testing

pytest topo_test.py

Expected output

When run with the documented command, the program prints:

c a b

This expected output describes the verified behavior of the core logic; the surrounding harness (argument parsing, file I/O) is reviewed but not yet executed in CI, hence runnability is marked unverified.

Limitations

  • The program is a single-file CLI with no external service dependencies.
  • Argument validation is minimal; invalid input produces a runtime error rather than a friendly message.
  • Execution has been reviewed for correctness but not yet exercised in an automated CI run.