2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 594fa2746886f41f7d8bf225
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: Topological sort
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 302340
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: topological-sort
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Given a mapping between items, and items they depend on, a [topological sort ](<https://en.wikipedia.org/wiki/Topological sorting> "wp: Topological sorting" ) orders items so that no item precedes an item it depends upon. The compiling of a library in the [VHDL ](https://en.wikipedia.org/wiki/VHDL "wp: VHDL" ) language has the constraint that a library must be compiled after any library it depends on.
							 
						 
					
						
							
								
									
										
										
										
											2019-03-10 22:12:52 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Write a function that will return a valid compile order of VHDL libraries from their dependencies.
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-03-10 22:12:52 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< ul >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Assume library names are single words.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Items mentioned as only dependents have no dependents of their own, but their order of compiling must be given.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Any self dependencies should be ignored.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Any un-orderable dependencies should be ignored.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / ul >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Use the following data as an example:
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< pre >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								LIBRARY          LIBRARY DEPENDENCIES
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								=======          ====================
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								synopsys
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< small > Note: the above data would be un-orderable if, for example, < code > dw04< / code >  is added to the list of dependencies of < code > dw01< / code > .< / small >  
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< strong > C.f.:< / strong >  
						 
					
						
							
								
									
										
										
										
											2019-03-10 22:12:52 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< ul >  
						 
					
						
							
								
									
										
										
										
											2019-05-22 23:30:29 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < li > < a  href = "https://rosettacode.org/wiki/Topological sort/Extracted top item"  title = "Topological sort/Extracted top item"  target = "_blank" > Topological sort/Extracted top item< / a > .< / li > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-10 22:12:52 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / ul >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								There are two popular algorithms for topological sorting:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< ul >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > < a  href = "https://en.wikipedia.org/wiki/Topological sorting"  title = "wp: Topological sorting"  target = "_blank" > Kahn's 1962 topological sort< / a > < / li > 
							 
						 
					
						
							
								
									
										
										
										
											2019-05-22 23:30:29 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < li > < a  href = "https://www.embeddedrelated.com/showarticle/799.php"  target = "_blank" > depth-first search< / a > < / li > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-10 22:12:52 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / ul >  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`topologicalSort`  should be a function. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(typeof topologicalSort === 'function');
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`topologicalSort`  should return correct library order. 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(topologicalSort(libsSimple), ['bbb', 'aaa']);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`topologicalSort`  should return correct library order. 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.deepEqual(topologicalSort(libsVHDL), solutionVHDL);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-09-15 09:57:40 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`topologicalSort`  should return correct library order. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(topologicalSort(libsCustom), solutionCustom);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`topologicalSort`  should ignore unorderable dependencies. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(topologicalSort(libsUnorderable), solutionUnorderable);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const libsSimple =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  `aaa bbb
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  bbb`;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const libsVHDL =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  `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
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  synopsys`;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const solutionVHDL = [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'ieee', 'std_cell_lib', 'gtech', 'dware', 'dw07', 'dw06',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'dw05', 'dw02', 'dw01', 'dw04', 'std', 'ramlib', 'synopsys',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'dw03', 'des_system_lib'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const libsCustom =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  `a b c d
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  b c d
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  d c
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  c base
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  base`;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const solutionCustom = ['base', 'c', 'd', 'b', 'a'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const libsUnorderable =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  `TestLib Base MainLib
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  MainLib TestLib
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Base`;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const solutionUnorderable = ['Base'];
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function topologicalSort(libs) {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function topologicalSort(libs) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // A map of the input data, with the keys as the packages, and the values as
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // and array of packages on which it depends.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const D = libs
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .split('\n')
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .map(e => e.split(' ').filter(ep => ep !== ''))
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    .reduce((p, c) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      p.set(c[0], c.filter((e, i) => (i > 0 & &  e !== c[0] ? e : null))), new Map());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [].concat(...D.values()).forEach(e => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    D.set(e, D.get(e) || []);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // The above map rotated so that it represents a DAG of the form
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Map {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  //    A => [ A, B, C],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  //    B => [C],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  //    C => []
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // where each key represents a node, and the array contains the edges.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const G = [...D.keys()].reduce((p, c) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    p.set(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      c,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      [...D.keys()].filter(e => D.get(e).includes(c))),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Map()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // An array of leaf nodes; nodes with 0 in degrees.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const Q = [...D.keys()].filter(e => D.get(e).length === 0);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // The result array.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const S = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  while (Q.length) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const u = Q.pop();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    S.push(u);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    G.get(u).forEach(v => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      D.set(v, D.get(v).filter(e => e !== u));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (D.get(v).length === 0) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        Q.push(v);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return S;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```