tsort.hs
来自「Haskell是一种程序语言。特别的」· HS 代码 · 共 33 行
HS
33 行
-----------------------------------------------------------------------------
-- |
-- Module : Tsort
-- Copyright : Thomas Hallgren? Lennart Augustsson?
--
-- Maintainer : Malcolm Wallace <Malcolm.Wallace@cs.york.ac.uk>
-- Stability : Stable
-- Portability : All
--
-- partial topological sort
-----------------------------------------------------------------------------
module Tsort(ptsort) where
import Compat(difference)
import List
-- | partial topological sort
--
-- ptsort G sorts the graph G. A graph is a list of nodes, each
-- node is a pair, a name and a list of names of connected nodes.
--
-- The output is a list of lists, where the lists contain nodes
-- that come before nodes occuring in later lists.
-- The order of nodes within each list is arbitrary and not
-- determined by the graph.
ptsort :: (Eq a) => [(a, [a])] -> [[a]]
ptsort [] = []
ptsort gG =
case partition (\(_, x) -> null x) gG of
([], _) -> error "ptsort: cycle in data\n"
(a, b) -> let a' = map fst a
in a' : ptsort (map (\(x, xs) -> (x, difference xs a')) b)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?