Commit 53c5c9d7 authored by Kai Zhang's avatar Kai Zhang

perform input sanity check in pagerank

parent edc195d9
...@@ -5,6 +5,7 @@ module IGraph ...@@ -5,6 +5,7 @@ module IGraph
, U(..) , U(..)
, D(..) , D(..)
, Graph(..) , Graph(..)
, empty
, mkGraph , mkGraph
, fromLabeledEdges , fromLabeledEdges
...@@ -136,6 +137,10 @@ instance Graph D where ...@@ -136,6 +137,10 @@ instance Graph D where
isDirected = const True isDirected = const True
isD = const True isD = const True
empty :: (Graph d, Hashable v, Read v, Eq v, Show v, Show e)
=> LGraph d v e
empty = runST $ new 0 >>= unsafeFreeze
mkGraph :: (Graph d, Hashable v, Read v, Eq v, Show v, Show e) mkGraph :: (Graph d, Hashable v, Read v, Eq v, Show v, Show e)
=> [v] -> [(Edge, e)] -> LGraph d v e => [v] -> [(Edge, e)] -> LGraph d v e
mkGraph vattr es = runST $ do mkGraph vattr es = runST $ do
......
...@@ -87,15 +87,22 @@ pagerank :: Graph d ...@@ -87,15 +87,22 @@ pagerank :: Graph d
-> Maybe [Double] -- ^ edge weights -> Maybe [Double] -- ^ edge weights
-> Double -- ^ damping factor, usually around 0.85 -> Double -- ^ damping factor, usually around 0.85
-> [Double] -> [Double]
pagerank gr ws d = unsafePerformIO $ alloca $ \p -> do pagerank gr ws d
vptr <- igraphVectorNew 0 | n == 0 = []
vsptr <- igraphVsAll | otherwise = unsafePerformIO $ alloca $ \p -> do
ws' <- case ws of vptr <- igraphVectorNew 0
Just w -> listToVector w vsptr <- igraphVsAll
_ -> liftM VectorPtr $ newForeignPtr_ $ castPtr nullPtr ws' <- case ws of
igraphPagerank (_graph gr) IgraphPagerankAlgoPrpack vptr p vsptr Just w -> if length w /= m
(isDirected gr) d ws' nullPtr then error "pagerank: incorrect length of edge weight vector"
vectorPtrToList vptr else listToVector w
_ -> liftM VectorPtr $ newForeignPtr_ $ castPtr nullPtr
igraphPagerank (_graph gr) IgraphPagerankAlgoPrpack vptr p vsptr
(isDirected gr) d ws' nullPtr
vectorPtrToList vptr
where
n = nNodes gr
m = nEdges gr
personalizedPagerank :: Graph d personalizedPagerank :: Graph d
=> LGraph d v e => LGraph d v e
...@@ -103,13 +110,21 @@ personalizedPagerank :: Graph d ...@@ -103,13 +110,21 @@ personalizedPagerank :: Graph d
-> Maybe [Double] -> Maybe [Double]
-> Double -> Double
-> [Double] -> [Double]
personalizedPagerank gr reset ws d = unsafePerformIO $ alloca $ \p -> do personalizedPagerank gr reset ws d
vptr <- igraphVectorNew 0 | n == 0 = []
vsptr <- igraphVsAll | length reset /= n = error "personalizedPagerank: incorrect length of reset vector"
ws' <- case ws of | otherwise = unsafePerformIO $ alloca $ \p -> do
Just w -> listToVector w vptr <- igraphVectorNew 0
_ -> liftM VectorPtr $ newForeignPtr_ $ castPtr nullPtr vsptr <- igraphVsAll
reset' <- listToVector reset ws' <- case ws of
igraphPersonalizedPagerank (_graph gr) IgraphPagerankAlgoPrpack vptr p vsptr Just w -> if length w /= m
(isDirected gr) d reset' ws' nullPtr then error "pagerank: incorrect length of edge weight vector"
vectorPtrToList vptr else listToVector w
_ -> liftM VectorPtr $ newForeignPtr_ $ castPtr nullPtr
reset' <- listToVector reset
igraphPersonalizedPagerank (_graph gr) IgraphPagerankAlgoPrpack vptr p vsptr
(isDirected gr) d reset' ws' nullPtr
vectorPtrToList vptr
where
n = nNodes gr
m = nEdges gr
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