Files
freeCodeCamp/curriculum/challenges/russian/08-coding-interview-prep/rosetta-code/topological-sort.russian.md

8.5 KiB
Raw Blame History

title, id, challengeType, forumTopicId, localeTitle
title id challengeType forumTopicId localeTitle
Topological sort 594fa2746886f41f7d8bf225 5 302340 Топологическая сортировка

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
Synopsys

Примечание: вышеуказанные данные были бы неустановимыми, если, например, dw04 добавляется в список зависимостей dw01 .

Cf:
 <a href="http://rosettacode.org/wiki/Topological sort/Extracted top item" title="Topological sort/Extracted top item">Topological sort/Extracted top item</a>. 

Существует два популярных алгоритма топологической сортировки:

Канский топологический сорт 1962 года и поиск по глубине: топологическая сортировка

Джейсон Сакс: «Десять небольших алгоритмов, часть 4: топологическая сортировка» .

Instructions

Write a function that will return a valid compile order of VHDL libraries from their dependencies.
  • Assume library names are single words.
  • Items mentioned as only dependents have no dependents of their own, but their order of compiling must be given.
  • Any self dependencies should be ignored.
  • Any un-orderable dependencies should be ignored.
Use the following data as an example:
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
Note: the above data would be un-orderable if, for example, dw04 is added to the list of dependencies of dw01. C.f.: There are two popular algorithms for topological sorting:

Tests

tests:
  - text: <code>topologicalSort</code> is a function.
    testString: assert(typeof topologicalSort === 'function');
  - text: <code>topologicalSort</code> must return correct library order..
    testString: assert.deepEqual(topologicalSort(libsSimple), ['bbb', 'aaa']);
  - text: <code>topologicalSort</code> must return correct library order..
    testString: assert.deepEqual(topologicalSort(libsVHDL), solutionVHDL);
  - text: <code>topologicalSort</code> must return correct library order..
    testString: assert.deepEqual(topologicalSort(libsCustom), solutionCustom);
  - text: <code>topologicalSort</code> must ignore unorderable dependencies..
    testString: assert.deepEqual(topologicalSort(libsUnorderable), solutionUnorderable);

Challenge Seed

function topologicalSort(libs) {
  // Good luck!
  return true;
}

After Tests

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'];

Solution

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')
    .map(e => e.split(' ').filter(ep => ep !== ''))
    .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;
}