How to do a topological sort from a subset of items from a given graph?

后端 未结 2 1485
谎友^
谎友^ 2021-01-24 12:52

I have the following problem: You are given a todo list, some items of which depend on others. Write a function that takes a subset of those todos and returns an ordered list of

2条回答
  •  走了就别回头了
    2021-01-24 13:19

    Instead of taking a separate filtering pass, you should be able to check each task for addition to the output filteredTasks as it is processed for addition to orderedTasks.

    Since the only other use of orderedTasks is to check its length, you should then be able to replace it with a counter.

    This might not make your function much faster, but it will simplify your code.

提交回复
热议问题