简体   繁体   English

Haskell生成具有QuickCheck属性的图形

[英]Haskell Generating graphs with QuickCheck properties

Graphs have these properties: 图形具有以下属性:

The type 'Edge' represents an edge between two nodes. 类型“边缘”表示两个节点之间的边缘。

data Edge v = Edge {source :: v, target :: v}
          deriving (Show,Eq,Ord)

The 'Graph' type represents a directed graph. “图”类型表示有向图。

data Graph v = Graph {nodes :: Set v, edges :: Set (Edge v)}
           deriving Show

The fuction 'isDAG' tests if a graph is acyclic. 功能“ isDAG”测试图是否为非循环图。

isDAG :: Ord v => Graph v -> Bool
isDAG g = isValid g && all nocycle (nodes g)
where nocycle v = all (\a -> v `notMember` reachable g a) $ Set.map target (adj g v)

The fuction 'isForest' tests if a valid DAG is a forest (a set of trees) 功能“ isForest”测试有效的DAG是否为森林(一组树木)

isForest :: Ord v => DAG v -> Bool
isForest g = isDAG g && all (\v -> length (adj g v) <= 1) (nodes g)

The generators code is: 生成器代码为:

DAGs generator DAG发生器

dag :: (Ord v, Arbitrary v) => Gen (DAG v)
dag = arbitrary `suchThat` isDAG

Forests generator 森林发电机

forest :: (Ord v, Arbitrary v) => Gen (Forest v)
forest = arbitrary `suchThat` isForest

I want to improve the generators Dag and Forest, so they are defined based on their properties and not with 'suchThat'. 我想改进生成器Dag和Forest,因此它们是基于其属性而不是“ suchThat”定义的。 How can I do it? 我该怎么做?

Thank you in advance. 先感谢您。

I believe the question at the core is how to generate DAGs and forests. 我认为核心问题是如何生成DAG和森林。

What's a forest? 什么是森林? Forest is a collection of trees. 森林是树木的集合。 What's a tree? 什么是树? Tree is a graph where every node except the root has exactly one parent. 树是一个图,其中除根以外的每个节点都只有一个父节点。 How do we turn it into an algorithm? 我们如何将其转化为算法? Generate a list of nodes. 生成节点列表。 For every node in the list going from the left randomly pick an element to the right of it as its parent and create an edge to it. 对于列表中从左到右的每个节点,随机选择一个位于其右侧的元素作为其父元素,并为其创建边。

What is a DAG? 什么是DAG? DAG is a directed acyclic graph. DAG是有向无环图。 What can we do with DAGs? 我们可以用DAG做什么? We can topologically order them. 我们可以在拓扑上订购它们。 What does that mean? 这意味着什么? It means we can put them in a sequence where every edge goes from left to right. 这意味着我们可以将它们按顺序排列,每个边缘从左到右。 How do we turn it into an algorithm? 我们如何将其转化为算法? Generate a list of nodes. 生成节点列表。 For every node in the list going from the left randomly pick a subset of elements to the right of it and create an edge to them. 对于列表中从左到右的每个节点,随机选择一个在其右侧的元素子集并为其创建边。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM