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

add diameter

parent 34553acc
......@@ -3,6 +3,7 @@
module IGraph.Algorithms.Structure
( -- * Shortest Path Related Functions
shortestPath
, diameter
, inducedSubgraph
, isConnected
, isStronglyConnected
......@@ -64,6 +65,29 @@ shortestPath gr s t getEdgeW = unsafePerformIO $ allocaVector $ \path -> do
, `Neimode'
} -> `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
-- the specified vertices and all edges between them to a new graph.
inducedSubgraph :: (Ord v, Serialize v)
......@@ -144,3 +168,8 @@ topSortUnsafe gr = unsafePerformIO $ allocaVectorN n $ \res -> do
, castPtr `Ptr Vector'
, `Neimode'
} -> `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
, withList
, withListMaybe
, toList
, toNodes
, igraphVectorNull
, igraphVectorFill
, igraphVectorE
......@@ -193,6 +194,10 @@ toList vec = do
map realToFrac <$> peekArray n ptr
{-# 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' } -> `()' #}
-- Initializing elements
......
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}
module Test.Algorithms
( tests
) where
......@@ -20,6 +21,7 @@ tests = testGroup "Algorithms"
[ graphIsomorphism
, motifTest
, cliqueTest
, diameterTest
, subGraphs
, decomposeTest
, pagerankTest
......@@ -55,6 +57,13 @@ cliqueTest = testGroup "Clique"
[2,3,4], [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 = testGroup "generate induced subgraphs"
[ 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