Commit d4aa9e34 authored by Kai Zhang's avatar Kai Zhang

add more generators

parent f4b3a1b2
......@@ -2,7 +2,7 @@
-- documentation, see http://haskell.org/cabal/users-guide/
name: haskell-igraph
version: 0.3.0
version: 0.3.1-dev
synopsis: Imcomplete igraph bindings
description: This is an attempt to create a complete bindings for the
igraph<"http://igraph.org/c/"> library.
......@@ -10,7 +10,7 @@ license: MIT
license-file: LICENSE
author: Kai Zhang
maintainer: kai@kzhang.org
copyright: (c) 2016 Kai Zhang
copyright: (c) 2016-2017 Kai Zhang
category: Math
build-type: Simple
cabal-version: >=1.24
......
module IGraph.Generators
( ErdosRenyiModel(..)
, erdosRenyiGame
, degreeSequenceGame
, rewire
) where
import Control.Monad (when)
import Data.Hashable (Hashable)
import IGraph
import IGraph.Mutable
import IGraph.Internal.Graph
import IGraph.Internal.Constants
import IGraph.Internal.Data
import IGraph.Internal.Graph
import IGraph.Internal.Initialization
import IGraph.Mutable
data ErdosRenyiModel = GNP Int Double
| GNM Int Int
......@@ -24,3 +30,24 @@ erdosRenyiGame (GNM n m) d self = do
gp <- igraphInit >> igraphErdosRenyiGame IgraphErdosRenyiGnm n
(fromIntegral m) (isD d) self
unsafeFreeze $ MLGraph gp
-- | Generates a random graph with a given degree sequence.
degreeSequenceGame :: [Int] -- ^ Out degree
-> [Int] -- ^ In degree
-> IO (LGraph D () ())
degreeSequenceGame out_deg in_deg = do
out_deg' <- listToVector $ map fromIntegral out_deg
in_deg' <- listToVector $ map fromIntegral in_deg
gp <- igraphDegreeSequenceGame out_deg' in_deg' IgraphDegseqSimpleNoMultiple
unsafeFreeze $ MLGraph gp
-- | Randomly rewires a graph while preserving the degree distribution.
rewire :: (Graph d, Hashable v, Read v, Eq v, Show v, Show e)
=> Int -- ^ Number of rewiring trials to perform.
-> LGraph d v e
-> IO (LGraph d v e)
rewire n gr = do
(MLGraph gptr) <- thaw gr
err <- igraphRewire gptr n IgraphRewiringSimple
when (err /= 0) $ error "failed to rewire graph!"
unsafeFreeze $ MLGraph gptr
......@@ -28,3 +28,9 @@ import Foreign
{#enum igraph_erdos_renyi_t as ErdosRenyi {underscoreToCase}
deriving (Show, Read, Eq) #}
{#enum igraph_rewiring_t as Rewiring {underscoreToCase}
deriving (Show, Read, Eq) #}
{#enum igraph_degseq_t as Degseq {underscoreToCase}
deriving (Show, Read, Eq) #}
......@@ -54,6 +54,13 @@ igraphEdge g i = alloca $ \fr -> alloca $ \to -> do
{#fun igraph_full as ^ { +, `Int', `Bool', `Bool' } -> `IGraphPtr' #}
{#fun igraph_erdos_renyi_game as ^ { +, `ErdosRenyi', `Int', `Double', `Bool'
, `Bool'} -> `IGraphPtr' #}
, `Bool' } -> `IGraphPtr' #}
{#fun igraph_degree_sequence_game as ^ { +, `VectorPtr', `VectorPtr'
, `Degseq' } -> `IGraphPtr' #}
{#fun igraph_rewire as ^ { `IGraphPtr', `Int', `Rewiring' } -> `Int' #}
{#fun igraph_isoclass_create as ^ { +, `Int', `Int', `Bool' } -> `IGraphPtr' #}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment