UNPKG

@informalsystems/quint

Version:

Core tool for the Quint specification language

41 lines (40 loc) 1.49 kB
/** * This code implements stable topological sort in a graph. We are basically * following the Kahn's algorithm, see: * ${@link https://en.wikipedia.org/wiki/Topological_sorting"}. * * This code is a port of the Scala code from Apalache: * * ${@link * https://github.com/apalache-mc/apalache/blob/dd5fff8dbfe707fb450afd2319cf50ebeb568e18/tlair/src/main/scala/at/forsyte/apalache/tla/lir/transformations/impl/StableTopologicalSort.scala * } * * @author Igor Konnov, Informal Systems, 2023 * * Copyright 2022-2023 Informal Systems * Licensed under the Apache License, Version 2.0. * See LICENSE in the project root for license information. */ import { Map } from 'immutable'; import { Set } from 'immutable'; import { WithId } from '../ir/quintIr'; type Edges = Map<bigint, Set<bigint>>; /** * Sort the elements of the list `unsorted` according to the dependencies that * are stored in the incoming edges. The semantics of an edge `u -> v` is that * `u` depends on `v` (or `u` calls `v`). That, in a topologically sorted list, * `v` should appear before `u`. * * @param inEdges * a map from a node `u` to the set of the nodes used by `u` * * @param unsorted a list of nodes * * @returns either `Right(sorted)` that contains the correctly sorted nodes, * or `Left(nodes)` that contains a subgraph with a cycle inside. */ export declare function toposort<T extends WithId>(inEdges: Edges, unsorted: T[]): { cycles: Set<bigint>; sorted: T[]; }; export {};