Commit 1bc3746a authored by Jean-Baptiste Mazon's avatar Jean-Baptiste Mazon

add diameter

parent 34553acc
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
module IGraph.Algorithms.Structure module IGraph.Algorithms.Structure
( -- * Shortest Path Related Functions ( -- * Shortest Path Related Functions
shortestPath shortestPath
, diameter
, inducedSubgraph , inducedSubgraph
, isConnected , isConnected
, isStronglyConnected , isStronglyConnected
...@@ -64,6 +65,29 @@ shortestPath gr s t getEdgeW = unsafePerformIO $ allocaVector $ \path -> do ...@@ -64,6 +65,29 @@ shortestPath gr s t getEdgeW = unsafePerformIO $ allocaVector $ \path -> do
, `Neimode' , `Neimode'
} -> `CInt' void- #} } -> `CInt' void- #}
-- | Calculates the diameter of a graph (longest geodesic).
diameter :: Graph d v e
-> EdgeType -- ^ whether to consider directed paths
-> Bool -- ^ if unconnected,
-- return largest component diameter (True)
-- or number of vertices (False)
-> (Int,[Node])
diameter graph directed unconn = unsafePerformIO $
alloca $ \pres ->
allocaVector $ \path -> do
igraphDiameter (_graph graph) pres nullPtr nullPtr path directed unconn
liftM2 (,) (peekIntConv pres) (toNodes path)
{-# INLINE igraphDiameter #-}
{#fun igraph_diameter as ^
{ `IGraph'
, castPtr `Ptr CInt'
, castPtr `Ptr CInt'
, castPtr `Ptr CInt'
, castPtr `Ptr Vector'
, dirToBool `EdgeType'
, `Bool'
} -> `CInt' void- #}
-- | Creates a subgraph induced by the specified vertices. This function collects -- | Creates a subgraph induced by the specified vertices. This function collects
-- the specified vertices and all edges between them to a new graph. -- the specified vertices and all edges between them to a new graph.
inducedSubgraph :: (Ord v, Serialize v) inducedSubgraph :: (Ord v, Serialize v)
...@@ -144,3 +168,8 @@ topSortUnsafe gr = unsafePerformIO $ allocaVectorN n $ \res -> do ...@@ -144,3 +168,8 @@ topSortUnsafe gr = unsafePerformIO $ allocaVectorN n $ \res -> do
, castPtr `Ptr Vector' , castPtr `Ptr Vector'
, `Neimode' , `Neimode'
} -> `CInt' void- #} } -> `CInt' void- #}
-- Marshaller for those "treat edges as directed" booleans.
dirToBool :: Num n => EdgeType -> n
dirToBool D = cFromBool True
dirToBool U = cFromBool False
...@@ -8,6 +8,7 @@ module IGraph.Internal ...@@ -8,6 +8,7 @@ module IGraph.Internal
, withList , withList
, withListMaybe , withListMaybe
, toList , toList
, toNodes
, igraphVectorNull , igraphVectorNull
, igraphVectorFill , igraphVectorFill
, igraphVectorE , igraphVectorE
...@@ -193,6 +194,10 @@ toList vec = do ...@@ -193,6 +194,10 @@ toList vec = do
map realToFrac <$> peekArray n ptr map realToFrac <$> peekArray n ptr
{-# INLINE toList #-} {-# INLINE toList #-}
toNodes :: Ptr Vector -> IO [Node]
toNodes = fmap (map truncate) . toList
{-# INLINE toNodes #-}
{#fun igraph_vector_copy_to as ^ { castPtr `Ptr Vector', id `Ptr CDouble' } -> `()' #} {#fun igraph_vector_copy_to as ^ { castPtr `Ptr Vector', id `Ptr CDouble' } -> `()' #}
-- Initializing elements -- Initializing elements
......
{-# LANGUAGE DataKinds #-} {-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}
module Test.Algorithms module Test.Algorithms
( tests ( tests
) where ) where
...@@ -20,6 +21,7 @@ tests = testGroup "Algorithms" ...@@ -20,6 +21,7 @@ tests = testGroup "Algorithms"
[ graphIsomorphism [ graphIsomorphism
, motifTest , motifTest
, cliqueTest , cliqueTest
, diameterTest
, subGraphs , subGraphs
, decomposeTest , decomposeTest
, pagerankTest , pagerankTest
...@@ -55,6 +57,13 @@ cliqueTest = testGroup "Clique" ...@@ -55,6 +57,13 @@ cliqueTest = testGroup "Clique"
[2,3,4], [2,4,5] ] [2,3,4], [2,4,5] ]
c4 = [[1, 2, 3, 4], [1, 2, 4, 5]] c4 = [[1, 2, 3, 4], [1, 2, 4, 5]]
diameterTest :: TestTree
diameterTest = testGroup "Diameters"
[ testCase "clique" $ fst (diameter (full @'U 10 False) U True) @?= 1
, testCase "star" $ fst (diameter (star 10) D False) @?= 2
, testCase "ring" $ fst (diameter (ring 10) U False) @?= 5
]
subGraphs :: TestTree subGraphs :: TestTree
subGraphs = testGroup "generate induced subgraphs" subGraphs = testGroup "generate induced subgraphs"
[ testCase "" $ test case1 ] [ testCase "" $ test case1 ]
......
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