0

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 :: 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 :: 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 :: (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'. How can I do it?

Thank you in advance.

Vinho
  • 1
  • 3
  • 2
    Possible duplicate of [Arbitrary instance for generating unbiased graphs for quickcheck](http://stackoverflow.com/questions/36398392/arbitrary-instance-for-generating-unbiased-graphs-for-quickcheck) – user2407038 Apr 27 '16 at 15:54
  • It isn't I checked that and it does not answer my question. What I want to improve are those generators and not the instance. Can you help? – Vinho Apr 27 '16 at 15:59
  • 1
    I'm not sure what you mean by "improve" the generators. As it stands, this question does not ask any more than the duplicate - both questions ask about writing "less biased" generators. – user2407038 Apr 27 '16 at 16:05
  • As you can see they are written with 'suchThat', and not according to their properties. How can I write them using QuickCheck property combinators? – Vinho Apr 27 '16 at 16:52
  • 1
    The question is a duplicate. The problem is that it didn't get good answers last time. But asking the same question again isn't going to fix that. – Paul Johnson Apr 27 '16 at 17:15
  • What do you mean _improve those generatorns and not the instance_? The definition of `arbitrary` in the instance is a generator! – Petr Apr 27 '16 at 17:52
  • Yes but my instance is already done. I just want a different implementation of my generators without using 'suchThat'. Not the instance, but those who return Gen (DAG and FOREST). – Vinho Apr 29 '16 at 09:55

1 Answers1

3

I believe the question at the core is how to generate DAGs and forests.

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 is a directed acyclic graph. What can we do with DAGs? 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.

niteria
  • 1,375
  • 1
  • 9
  • 14
  • Thank you for your answer. But do you have any idea of what I should do in order to generate graphs that satisfy the DAG property? Instead of 'suchThat' ? Thank you in advance. – Vinho Apr 29 '16 at 05:04
  • 1
    The second part of my answer answers this question. – niteria May 03 '16 at 10:09