3.7 KiB
3.7 KiB
title | id | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
Topological sort | 594fa2746886f41f7d8bf225 | 5 | 拓扑排序 |
Description
给定项目之间的映射以及它们所依赖的项目, 拓扑排序会对项目进行排序 ,以使项目不在其所依赖的项目之前。
用VHDL语言编译库有一个限制,即必须在它依赖的库之后编译库。
任务:编写一个函数,该函数将从其依赖项返回VHDL库的有效编译顺序。
假设库名称是单个单词。仅作为家属提及的项目没有自己的家属,但必须给出他们的编制顺序。任何自我依赖都应该被忽略。应忽略任何不可订购的依赖项。使用以下数据作为示例:
图书馆图书馆依赖 ======= ==================== des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee dw01 ieee dw01 dware gtech dw02 ieee dw02 dware dw03 std synopsys dware dw03 dw02 dw01 ieee gtech dw04 dw04 ieee dw01 dware gtech dw05 dw05 ieee dware dw06 dw06 ieee dware dw07 ieee dware dware ieee dware gtech ieee gtech ramlib std ieee std_cell_lib ieee std_cell_lib 新思
注意:如果将dw04
添加到dw01
的依赖项列表中,则上述数据将无法订购。
<a href="http://rosettacode.org/wiki/Topological sort/Extracted top item" title="Topological sort/Extracted top item">Topological sort/Extracted top item</a>.
拓扑排序有两种流行的算法:
Kahn的1962拓扑排序和深度优先搜索: 拓扑排序
Jason Sachs: “十个小算法,第四部分:拓扑排序” 。
Instructions
Tests
tests:
- text: <code>topologicalSort</code>是一个函数。
testString: 'assert(typeof topologicalSort === "function", "<code>topologicalSort</code> is a function.");'
- text: <code>topologicalSort</code>必须返回正确的库顺序..
testString: 'assert.deepEqual(topologicalSort(libsSimple), ["bbb", "aaa"], "<code>topologicalSort</code> must return correct library order..");'
- text: <code>topologicalSort</code>必须返回正确的库顺序..
testString: 'assert.deepEqual(topologicalSort(libsVHDL), solutionVHDL, "<code>topologicalSort</code> must return correct library order..");'
- text: <code>topologicalSort</code>必须返回正确的库顺序..
testString: 'assert.deepEqual(topologicalSort(libsCustom), solutionCustom, "<code>topologicalSort</code> must return correct library order..");'
- text: <code>topologicalSort</code>必须忽略不可共享的依赖项。
testString: 'assert.deepEqual(topologicalSort(libsUnorderable), solutionUnorderable, "<code>topologicalSort</code> must ignore unorderable dependencies..");'
Challenge Seed
function topologicalSort(libs) {
// Good luck!
return true;
}
After Test
console.info('after the test');
Solution
// solution required