Gareth Rees <[email protected]> added the comment:
Just to elaborate on what I mean by "bug magnet". (I'm sure Pablo understands
this, but there may be other readers who would like to see it spelled out.)
Suppose that you have a directed graph represented as a mapping from a vertex
to an iterable of its out-neighbours. Then the "obvious" way to get a total
order on the vertices in the graph would be to generate the edges and pass them
to topsort:
def edges(graph):
return ((v, w) for v, ww in graph.items() for w in ww)
order = topsort(edges(graph))
This will appear to work fine if it is never tested with a graph that has
isolated vertices (which would be an all too easy omission).
To handle isolated vertices you have to remember to write something like this:
reversed_graph = {v: [] for v in graph}
for v, ww in graph.items():
for w in ww:
reversed_graph[w].append(v)
order = topsort(edges(graph)) + [
v for v, ww in graph.items() if not ww and not reversed_graph[v]]
I think it likely that beginner programmers will forget to do this and be
surprised later on when their total order is missing some of the vertices.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue17005>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com