Commit d8d7f6b0 authored by Kai Zhang's avatar Kai Zhang

add rewireEdges

parent 1987cba3
...@@ -8,6 +8,7 @@ module IGraph.Algorithms.Generators ...@@ -8,6 +8,7 @@ module IGraph.Algorithms.Generators
, ErdosRenyiModel(..) , ErdosRenyiModel(..)
, erdosRenyiGame , erdosRenyiGame
, degreeSequenceGame , degreeSequenceGame
, rewireEdges
, rewire , rewire
) where ) where
...@@ -15,6 +16,7 @@ import Data.Serialize (Serialize) ...@@ -15,6 +16,7 @@ import Data.Serialize (Serialize)
import Data.Singletons (SingI, Sing, sing, fromSing) import Data.Singletons (SingI, Sing, sing, fromSing)
import System.IO.Unsafe (unsafePerformIO) import System.IO.Unsafe (unsafePerformIO)
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import Control.Monad.Primitive (RealWorld)
import qualified Foreign.Ptr as C2HSImp import qualified Foreign.Ptr as C2HSImp
import Foreign import Foreign
...@@ -75,8 +77,11 @@ ring n = unsafePerformIO $ do ...@@ -75,8 +77,11 @@ ring n = unsafePerformIO $ do
, `Bool' , `Bool'
} -> `CInt' void- #} } -> `CInt' void- #}
data ErdosRenyiModel = GNP Int Double data ErdosRenyiModel = GNP Int Double -- ^ G(n,p) graph, every possible edge is
| GNM Int Int -- included in the graph with probability p.
| GNM Int Int -- ^ G(n,m) graph, m edges are selected
-- uniformly randomly in a graph with n
-- vertices.
erdosRenyiGame :: forall d. SingI d erdosRenyiGame :: forall d. SingI d
=> ErdosRenyiModel => ErdosRenyiModel
...@@ -115,6 +120,22 @@ degreeSequenceGame out_deg in_deg = do ...@@ -115,6 +120,22 @@ degreeSequenceGame out_deg in_deg = do
, castPtr `Ptr Vector', castPtr `Ptr Vector', `Degseq' , castPtr `Ptr Vector', castPtr `Ptr Vector', `Degseq'
} -> `CInt' void- #} } -> `CInt' void- #}
-- | Rewire the edges of a graph with constant probability.
rewireEdges :: MGraph RealWorld d v e
-> Double -- ^ The rewiring probability a constant between zero and
-- one (inclusive).
-> Bool -- ^ whether loop edges are allowed in the new graph, or not.
-> Bool -- ^ whether multiple edges are allowed in the new graph.
-> IO ()
rewireEdges gr p loop multi = igraphRewireEdges (_mgraph gr) p loop multi
{#fun igraph_rewire_edges as ^
{ `IGraph'
, `Double'
, `Bool'
, `Bool'
} -> `CInt' void- #}
-- | Randomly rewires a graph while preserving the degree distribution. -- | Randomly rewires a graph while preserving the degree distribution.
rewire :: (Serialize v, Ord v, Serialize e) rewire :: (Serialize v, Ord v, Serialize e)
=> Int -- ^ Number of rewiring trials to perform. => Int -- ^ Number of rewiring trials to perform.
...@@ -125,3 +146,4 @@ rewire n gr = do ...@@ -125,3 +146,4 @@ rewire n gr = do
igraphRewire (_mgraph gr') n IgraphRewiringSimple igraphRewire (_mgraph gr') n IgraphRewiringSimple
unsafeFreeze gr' unsafeFreeze gr'
{#fun igraph_rewire as ^ { `IGraph', `Int', `Rewiring' } -> `CInt' void-#} {#fun igraph_rewire as ^ { `IGraph', `Int', `Rewiring' } -> `CInt' void-#}
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