module Data.IGraph
(
Graph (..)
, D, U, IsUnweighted
, E (isDirected, isWeighted, edgeFrom, edgeTo, edgeWeight, toEdge), Edge
, Weighted, toEdgeWeighted, getWeight
, IsUndirected, ToDirected
, IsDirected, ToUndirected
, emptyGraph
, fromList, fromListWeighted
, insertEdge
, deleteEdge
, deleteNode
, reverseGraphDirection
, toDirected, toUndirected
, numberOfNodes
, numberOfEdges
, member
, nodes
, edges
, neighbours
, VertexSelector(..)
, vsSize, selectedVertices
, EdgeSelector(..)
, esSize, selectedEdges
, areConnected
, shortestPaths, shortestPathsDijkstra, shortestPathsBellmanFord, shortestPathsJohnson
, getShortestPaths, getShortestPathsDijkstra
, getShortestPath, getShortestPathDijkstra
, getAllShortestPaths, getAllShortestPathsDijkstra
, averagePathLength
, pathLengthHist
, diameter, diameterDijkstra
, girth
, eccentricity
, radius
, subcomponent
, inducedSubgraph, SubgraphImplementation(..)
, subgraphEdges
, clusters
, isConnected
, decompose
, Connectedness(..)
, biconnectedComponents
, articulationPoints
, closeness
, betweenness, edgeBetweenness
, pagerank
, personalizedPagerank
, personalizedPagerankVs
, constraint
, maxdegree
, strength
, eigenvectorCentrality
, hubScore
, authorityScore
, closenessEstimate
, betweennessEstimate
, edgeBetweennessEstimate
, centralizationDegree
, centralizationBetweenness
, centralizationCloseness
, centralizationEigenvectorCentrality
, centralizationDegreeTMax
, centralizationBetweennessTMax
, centralizationClosenessTMax
, centralizationEigenvectorCentralityTMax
, bibCoupling
, cocitation
, similarityJaccard
, similarityJaccardPairs
, similarityJaccardEs
, similarityDice
, similarityDicePairs
, similarityDiceEs
, similarityInverseLogWeighted
, minimumSpanningTree
, minimumSpanningTreeUnweighted
, minimumSpanningTreePrim
, transitivityUndirected
, transitivityLocalUndirected
, transitivityAvglocalUndirected
, transitivityBarrat
, laplacian
, assortativityNominal
, assortativity
, assortativityDegree
, coreness
, isDAG
, topologicalSorting
, FASAlgorithm(..)
, feedbackArcSet
, maximumCardinalitySearch
, isChordal
) where
import Data.IGraph.Internal
import Data.IGraph.Internal.Constants
import Data.IGraph.Types
import Data.Hashable
import Data.Map (Map)
import qualified Data.Map as M
import qualified Data.Foldable as F
import Data.List (nub)
import Foreign hiding (unsafePerformIO)
import Foreign.C
import System.IO.Unsafe (unsafePerformIO)
foreign import ccall "igraph_vs_size"
c_igraph_vs_size :: GraphPtr -> VsPtr -> Ptr CInt -> IO CInt
vsSize :: Graph d a -> VertexSelector a -> Int
vsSize g vs = unsafePerformIO $ alloca $ \ip -> do
_e <- withGraph g $ \gp ->
withVs vs g $ \vsp ->
c_igraph_vs_size gp vsp ip
fromIntegral `fmap` peek ip
foreign import ccall "selected_vertices"
c_selected_vertices :: GraphPtr -> VsPtr -> VectorPtr -> IO Int
selectedVertices :: Graph d a -> VertexSelector a -> [a]
selectedVertices g vs = unsafePerformIO $ do
let s = vsSize g vs
v <- newVector s
_ <- withGraph g $ \gp ->
withVs vs g $ \vsp ->
withVector v $ \vp ->
c_selected_vertices gp vsp vp
l <- vectorToList v
return (map (idToNode'' g . round) l)
foreign import ccall "igraph_es_size"
c_igraph_es_size :: GraphPtr -> EsPtr -> Ptr CInt -> IO CInt
esSize :: Graph d a -> EdgeSelector d a -> Int
esSize g es = unsafePerformIO $ alloca $ \ip -> do
_e <- withGraph g $ \gp ->
withEs es g $ \esp ->
c_igraph_es_size
gp
esp
ip
fromIntegral `fmap` peek ip
foreign import ccall "selected_edges"
c_selected_edges :: GraphPtr -> EsPtr -> VectorPtr -> IO CInt
selectedEdges :: Graph d a -> EdgeSelector d a -> [Edge d a]
selectedEdges g es = unsafePerformIO $ do
let s = esSize g es
v <- newVector s
_e <- withGraph g $ \gp ->
withEs es g $ \esp ->
withVector v $ \vp ->
c_selected_edges
gp
esp
vp
l <- vectorToList v
return $ map (edgeIdToEdge g . round) l
foreign import ccall "igraph_are_connected"
c_igraph_are_connected :: GraphPtr -> CInt -> CInt -> Ptr CInt -> IO CInt
areConnected :: Graph d a -> a -> a -> Bool
areConnected g n1 n2 = case (nodeToId g n1, nodeToId g n2) of
(Just i1, Just i2) -> unsafePerformIO $ withGraph g $ \gp -> alloca $ \bp -> do
_ <- c_igraph_are_connected gp (fromIntegral i1) (fromIntegral i2) bp
fmap (== 1) (peek bp)
_ -> False
foreign import ccall "shortest_paths"
c_igraph_shortest_paths :: GraphPtr -> MatrixPtr -> VsPtr -> VsPtr -> CInt -> IO CInt
shortestPaths :: (Ord a, Hashable a)
=> Graph d a
-> VertexSelector a
-> VertexSelector a
-> Map (a,a) (Maybe Int)
shortestPaths g vf vt =
let ls = unsafePerformIO $ do
ma <- newMatrix 0 0
_e <- withGraph g $ \gp ->
withMatrix ma $ \mp ->
withVs vf g $ \vfp ->
withVs vt g $ \vtp ->
c_igraph_shortest_paths
gp
mp
vfp
vtp
(getNeiMode g)
matrixToList ma
nf = selectedVertices g vf
nt = selectedVertices g vt
in M.fromList [ ((f,t), len)
| (f,lf) <- zip nf ls
, (t,len) <- zip nt (map roundMaybe lf)
]
roundMaybe :: Double -> Maybe Int
roundMaybe d = if d == 1/0 then Nothing else Just (round d)
foreign import ccall "shortest_paths_dijkstra"
c_igraph_shortest_paths_dijkstra :: GraphPtr -> MatrixPtr -> VsPtr -> VsPtr
-> VectorPtr -> CInt -> IO CInt
shortestPathsDijkstra :: (Ord a, Hashable a)
=> Graph (Weighted d) a
-> VertexSelector a
-> VertexSelector a
-> Map (a,a) (Maybe Int)
shortestPathsDijkstra g vf vt =
let ls = unsafePerformIO $ do
ma <- newMatrix 0 0
_e <- withGraph g $ \gp ->
withWeights g $ \wp ->
withMatrix ma $ \mp ->
withVs vf g $ \vfp ->
withVs vt g $ \vtp ->
c_igraph_shortest_paths_dijkstra
gp
mp
vfp
vtp
wp
(getNeiMode g)
matrixToList ma
nf = selectedVertices g vf
nt = selectedVertices g vt
in M.fromList [ ((f,t), len)
| (f,lf) <- zip nf ls
, (t,len) <- zip nt (map roundMaybe lf)
]
foreign import ccall "shortest_paths_bellman_ford"
c_igraph_shortest_paths_bellman_ford :: GraphPtr -> MatrixPtr -> VsPtr -> VsPtr -> VectorPtr
-> CInt -> IO CInt
shortestPathsBellmanFord :: (Ord a, Hashable a)
=> Graph (Weighted d) a
-> VertexSelector a
-> VertexSelector a
-> Map (a,a) (Maybe Int)
shortestPathsBellmanFord g vf vt =
let ls = unsafePerformIO $ do
ma <- newMatrix 0 0
_e <- withGraph g $ \gp ->
withWeights g $ \wp ->
withMatrix ma $ \mp ->
withVs vf g $ \vfp ->
withVs vt g $ \vtp ->
c_igraph_shortest_paths_bellman_ford
gp
mp
vfp
vtp
wp
(getNeiMode g)
matrixToList ma
nf = selectedVertices g vf
nt = selectedVertices g vt
in M.fromList [ ((f,t), len)
| (f,lf) <- zip nf ls
, (t,len) <- zip nt (map roundMaybe lf)
]
foreign import ccall "shortest_paths_johnson"
c_igraph_shortest_paths_johnson :: GraphPtr -> MatrixPtr -> VsPtr -> VsPtr -> VectorPtr
-> IO CInt
shortestPathsJohnson :: (Ord a, Hashable a)
=> Graph (Weighted d) a
-> VertexSelector a
-> VertexSelector a
-> Map (a,a) (Maybe Int)
shortestPathsJohnson g vf vt =
let ls = unsafePerformIO $ do
ma <- newMatrix 0 0
_e <- withGraph g $ \gp ->
withWeights g $ \wp ->
withMatrix ma $ \mp ->
withVs vf g $ \vfp ->
withVs vt g $ \vtp ->
c_igraph_shortest_paths_johnson
gp
mp
vfp
vtp
wp
matrixToList ma
nf = selectedVertices g vf
nt = selectedVertices g vt
in M.fromList [ ((f,t), len)
| (f,lf) <- zip nf ls
, (t,len) <- zip nt (map roundMaybe lf)
]
foreign import ccall "get_shortest_paths"
c_igraph_get_shortest_paths :: GraphPtr -> VectorPtrPtr -> VectorPtrPtr -> CInt -> VsPtr -> CInt -> IO CInt
getShortestPaths :: Graph d a
-> a
-> VertexSelector a
-> [ ([a],[Edge d a]) ]
getShortestPaths g f vt =
let mfi = nodeToId g f
in case mfi of
Nothing -> error "getShortestPaths: Invalid node"
Just fi -> unsafePerformIO $ do
vpv <- newVectorPtr 0
vpe <- newVectorPtr 0
_e <- withGraph g $ \gp ->
withVectorPtr vpv $ \vpvp ->
withVectorPtr vpe $ \vpep ->
withVs vt g $ \vtp ->
c_igraph_get_shortest_paths
gp
vpvp
vpep
(fromIntegral fi)
vtp
(getNeiMode g)
v <- vectorPtrToVertices g vpv
e <- vectorPtrToEdges g vpe
return $ zip v e
foreign import ccall "igraph_get_shortest_path"
c_igraph_get_shortest_path :: GraphPtr -> VectorPtr -> VectorPtr -> CInt -> CInt -> CInt -> IO CInt
getShortestPath :: Graph d a -> a -> a -> ([a],[Edge d a])
getShortestPath g n1 n2 =
let mi1 = nodeToId g n1
mi2 = nodeToId g n2
in case (mi1, mi2) of
(Just i1, Just i2) -> unsafePerformIO $
withGraph g $ \gp -> do
v1 <- listToVector ([] :: [Int])
v2 <- listToVector ([] :: [Int])
e <- withVector v1 $ \vp1 -> withVector v2 $ \vp2 ->
c_igraph_get_shortest_path
gp
vp1
vp2
(fromIntegral i1)
(fromIntegral i2)
(getNeiMode g)
if e == 0 then do
vert <- vectorToVertices g v1
edgs <- vectorToEdges g v2
return ( vert, edgs )
else
error $ "getShortestPath: igraph error " ++ show e
_ -> error "getShortestPath: Invalid nodes"
foreign import ccall "get_shortest_paths_dijkstra"
c_igraph_get_shortest_paths_dijkstra :: GraphPtr -> VectorPtrPtr -> VectorPtrPtr
-> CInt -> VsPtr -> VectorPtr -> CInt -> IO CInt
getShortestPathsDijkstra :: Graph (Weighted d) a
-> a
-> VertexSelector a
-> [ ([a],[Edge (Weighted d) a]) ]
getShortestPathsDijkstra g f vt =
let mfi = nodeToId g f
in case mfi of
Nothing -> error "getShortestPathsDijkstra: Invalid node"
Just fi -> unsafePerformIO $ do
vpv <- newVectorPtr 0
vpe <- newVectorPtr 0
_e <- withGraph g $ \gp ->
withWeights g $ \wp ->
withVectorPtr vpv $ \vpvp ->
withVectorPtr vpe $ \vpep ->
withVs vt g $ \vtp ->
c_igraph_get_shortest_paths_dijkstra
gp
vpvp
vpep
(fromIntegral fi)
vtp
wp
(getNeiMode g)
v <- vectorPtrToVertices g vpv
e <- vectorPtrToEdges g vpe
return $ zip v e
foreign import ccall "igraph_get_shortest_path_dijkstra"
c_igraph_get_shortest_path_dijkstra :: GraphPtr -> VectorPtr -> VectorPtr -> CInt
-> CInt -> VectorPtr -> CInt -> IO CInt
getShortestPathDijkstra :: Graph (Weighted d) a -> a -> a -> ([a],[Edge (Weighted d) a])
getShortestPathDijkstra g n1 n2 =
let mi1 = nodeToId g n1
mi2 = nodeToId g n2
in case (mi1, mi2) of
(Just i1, Just i2) -> unsafePerformIO $ do
v1 <- newVector 0
v2 <- newVector 0
e <- withGraph g $ \gp ->
withWeights g $ \wp ->
withVector v1 $ \vp1 ->
withVector v2 $ \vp2 ->
c_igraph_get_shortest_path_dijkstra
gp
vp1
vp2
(fromIntegral i1)
(fromIntegral i2)
wp
(getNeiMode g)
if e == 0 then do
vert <- vectorToVertices g v1
edgs <- vectorToEdges g v2
return ( vert, edgs )
else
error $ "getShortestPathDijkstra: igraph error " ++ show e
_ -> error "getShortestPathDijkstra: Invalid nodes"
foreign import ccall "get_all_shortest_paths"
c_igraph_get_all_shortest_paths :: GraphPtr
-> VectorPtrPtr
-> VectorPtr
-> CInt
-> VsPtr
-> CInt
-> IO CInt
getAllShortestPaths :: Graph d a
-> a
-> VertexSelector a
-> [[a]]
getAllShortestPaths g f vt =
let mfi = nodeToId g f
in case mfi of
Nothing -> error "getAllShortestPaths: Invalid node"
Just fi -> unsafePerformIO $ do
vpr <- newVectorPtr 0
_e <- withGraph g $ \gp ->
withVectorPtr vpr $ \vprp ->
withVs vt g $ \vtp ->
c_igraph_get_all_shortest_paths
gp
vprp
nullPtr
(fromIntegral fi)
vtp
(getNeiMode g)
vectorPtrToVertices g vpr
foreign import ccall "get_all_shortest_paths_dijkstra"
c_igraph_get_all_shortest_paths_dijkstra :: GraphPtr
-> VectorPtrPtr
-> VectorPtr
-> CInt
-> VsPtr
-> VectorPtr
-> CInt
-> IO CInt
getAllShortestPathsDijkstra :: Graph (Weighted d) a
-> a
-> VertexSelector a
-> [[a]]
getAllShortestPathsDijkstra g f vt =
let mfi = nodeToId g f
in case mfi of
Nothing -> error "getAllShortestPaths: Invalid node"
Just fi -> unsafePerformIO $ do
vpr <- newVectorPtr 0
_e <- withGraph g $ \gp ->
withWeights g $ \wp ->
withVectorPtr vpr $ \vprp ->
withVs vt g $ \vtp ->
c_igraph_get_all_shortest_paths_dijkstra
gp
vprp
nullPtr
(fromIntegral fi)
vtp
wp
(getNeiMode g)
vectorPtrToVertices g vpr
foreign import ccall "igraph_average_path_length"
c_igraph_average_path_length :: GraphPtr -> Ptr CDouble -> Bool -> Bool -> IO CInt
averagePathLength :: Graph d a
-> Bool
-> Bool
-> Double
averagePathLength g b1 b2 = unsafePerformIO $ do
alloca $ \dp -> do
_e <- withGraph g $ \gp ->
c_igraph_average_path_length
gp
dp
b1
b2
realToFrac `fmap` peek dp
foreign import ccall "igraph_path_length_hist"
c_igraph_path_length_hist :: GraphPtr -> VectorPtr -> Ptr CDouble -> Bool -> IO CInt
pathLengthHist :: Graph d a
-> Bool
-> ([Double],Double)
pathLengthHist g b = unsafePerformIO $ do
v <- newVector 0
d <- alloca $ \dp -> do
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_path_length_hist
gp
vp
dp
b
realToFrac `fmap` peek dp
l <- vectorToList v
return (l,d)
foreign import ccall "igraph_diameter"
c_igraph_diameter :: GraphPtr
-> Ptr CInt
-> Ptr CInt
-> Ptr CInt
-> VectorPtr
-> Bool
-> Bool
-> IO CInt
diameter :: Graph d a
-> Bool
-> Bool
-> (Int, (a,a), [a])
diameter g b1 b2 = unsafePerformIO $ do
alloca $ \ip -> do
alloca $ \fip -> do
alloca $ \tip -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_diameter
gp
ip
fip
tip
vp
b1
b2
d <- fromIntegral `fmap` peek ip
fi <- fromIntegral `fmap` peek fip
ti <- fromIntegral `fmap` peek tip
p <- vectorToVertices g v
return (d, (idToNode'' g fi, idToNode'' g ti), p)
foreign import ccall "igraph_diameter_dijkstra"
c_igraph_diameter_dijkstra
:: GraphPtr -> VectorPtr -> Ptr CDouble -> Ptr CInt -> Ptr CInt -> VectorPtr
-> Bool -> Bool -> IO CInt
diameterDijkstra :: Graph d a
-> (Double, a, a, [a])
diameterDijkstra g@(G _) = unsafePerformIO $
alloca $ \dp -> alloca $ \sp -> alloca $ \tp -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withOptionalWeights g $ \wp ->
withVector v $ \vp ->
c_igraph_diameter_dijkstra
gp
wp
dp
sp
tp
vp
(isDirected g)
True
d <- peek dp
s <- peek sp
t <- peek tp
path <- vectorToVertices g v
return ( realToFrac d
, idToNode'' g (fromIntegral s)
, idToNode'' g (fromIntegral t)
, path )
foreign import ccall "igraph_girth"
c_igraph_girth :: GraphPtr -> Ptr CInt -> VectorPtr -> IO CInt
girth :: Graph d a
-> (Int, [a])
girth g = unsafePerformIO $ do
alloca $ \ip -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_girth
gp
ip
vp
gr <- fromIntegral `fmap` peek ip
s <- vectorToVertices g v
return (gr, s)
foreign import ccall "eccentricity"
c_igraph_eccentricity :: GraphPtr -> VectorPtr -> VsPtr -> CInt -> IO CInt
eccentricity :: Graph d a -> VertexSelector a -> [(a,Int)]
eccentricity g vs = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVs vs g $ \vsp ->
withVector v $ \vp ->
c_igraph_eccentricity
gp
vp
vsp
(getNeiMode g)
l <- map round `fmap` vectorToList v
return $ zip (selectedVertices g vs) l
foreign import ccall "igraph_radius"
c_igraph_radius :: GraphPtr -> Ptr CDouble -> CInt -> IO CInt
radius :: Graph d a -> Int
radius g = unsafePerformIO $ do
alloca $ \dp -> do
_e <- withGraph g $ \gp ->
c_igraph_radius
gp
dp
(getNeiMode g)
round `fmap` peek dp
foreign import ccall "igraph_subcomponent"
c_igraph_subcomponent :: GraphPtr -> VectorPtr -> CDouble -> CInt -> IO CInt
subcomponent :: Graph d a -> a -> [a]
subcomponent g a = case nodeToId g a of
Just i -> unsafePerformIO $ do
v <- newVector 0
_ <- withGraph g $ \gp -> withVector v $ \vp ->
c_igraph_subcomponent
gp
vp
(fromIntegral i)
(getNeiMode g)
vectorToVertices g v
_ -> []
foreign import ccall "induced_subgraph"
c_igraph_induced_subcomponent :: GraphPtr -> GraphPtr -> VsPtr -> CInt -> IO CInt
inducedSubgraph :: Graph d a -> VertexSelector a -> SubgraphImplementation -> Graph d a
inducedSubgraph g vs i = unsafePerformIO $ do
withGraph g $ \gp ->
withVs vs g $ \vsp ->
withGraph (emptyWithCtxt g) $ \gp' -> do
setVertexIds gp
_e <- c_igraph_induced_subcomponent
gp
gp'
vsp
(fromIntegral $ fromEnum i)
subgraphFromPtr g gp'
foreign import ccall "subgraph_edges"
c_igraph_subgraph_edges
:: GraphPtr
-> GraphPtr
-> EsPtr
-> Bool
-> IO CInt
subgraphEdges :: Graph d a -> EdgeSelector d a -> Graph d a
subgraphEdges g es = unsafePerformIO $ do
withGraph g $ \gp ->
withEs es g $ \esp ->
withGraph (emptyWithCtxt g) $ \gp' -> do
setVertexIds gp
_e <- c_igraph_subgraph_edges
gp
gp'
esp
True
subgraphFromPtr g gp'
foreign import ccall "igraph_clusters"
c_igraph_clusters
:: GraphPtr
-> VectorPtr
-> VectorPtr
-> Ptr CInt
-> CInt
-> IO CInt
clusters
:: Graph d a
-> Connectedness
-> (Int, [Int])
clusters g c = unsafePerformIO $ alloca $ \ip -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_clusters
gp
nullPtr
vp
ip
(fromIntegral $ fromEnum c)
i <- peek ip
l <- vectorToList v
return (fromIntegral i, map round l)
foreign import ccall "igraph_is_connected"
c_igraph_is_connected :: GraphPtr -> Ptr CInt -> CInt -> IO CInt
isConnected :: Graph d a -> Connectedness -> Bool
isConnected g c = unsafePerformIO $ withGraph g $ \gp -> alloca $ \b -> do
_ <- c_igraph_is_connected gp b (fromIntegral $ fromEnum c)
r <- peek b
return $ r == 1
foreign import ccall "igraph_decompose"
c_igraph_decompose
:: GraphPtr
-> GraphVecPtr
-> CInt
-> CLong
-> CInt
-> IO CInt
decompose
:: Graph d a
-> Connectedness
-> Int
-> Int
-> [Graph d a]
decompose g m ma mi = unsafePerformIO $ do
gvp <- newGraphVector 0
_e <- withGraph g $ \gp ->
withGraphVector gvp $ \gvpp -> do
setVertexIds gp
c_igraph_decompose
gp
gvpp
(fromIntegral $ fromEnum m)
(fromIntegral ma)
(fromIntegral mi)
graphVectorToSubgraphs gvp g
foreign import ccall "igraph_biconnected_components"
c_igraph_biconnected_components
:: GraphPtr
-> Ptr CInt
-> VectorPtrPtr
-> VectorPtrPtr
-> VectorPtrPtr
-> VectorPtr
-> IO CInt
biconnectedComponents
:: Graph d a
-> (Int, [[Edge d a]], [[Edge d a]], [[a]], [a])
biconnectedComponents g = unsafePerformIO $ alloca $ \ip -> do
t <- newVectorPtr 0
ce <- newVectorPtr 0
c <- newVectorPtr 0
a <- newVector 0
_e <- withGraph g $ \gp ->
withVectorPtr t $ \tp ->
withVectorPtr ce $ \cep ->
withVectorPtr c $ \cp ->
withVector a $ \ap ->
c_igraph_biconnected_components
gp
ip
tp
cep
cp
ap
i <- peek ip
ts <- mapM (vectorToEdges' g) =<< vectorPtrToListOfVectorPtr t
ces <- mapM (vectorToEdges' g) =<< vectorPtrToListOfVectorPtr ce
cs <- mapM (vectorToVertices' g) =<< vectorPtrToListOfVectorPtr c
as <- vectorToVertices g a
return (fromIntegral i, ts, ces, cs, as)
foreign import ccall "igraph_articulation_points"
c_igraph_articulation_points :: GraphPtr -> VectorPtr -> IO CInt
articulationPoints :: Graph d a -> [a]
articulationPoints g = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_articulation_points
gp
vp
vectorToVertices g v
foreign import ccall "closeness"
c_igraph_closeness :: GraphPtr -> VectorPtr -> VsPtr -> CInt -> VectorPtr -> IO CInt
closeness :: Ord a => Graph d a -> VertexSelector a -> Map a Double
closeness g vs = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withOptionalWeights g $ \wp ->
withVs vs g $ \vsp ->
withVector v $ \vp ->
c_igraph_closeness
gp
vp
vsp
(getNeiMode g)
wp
scores <- vectorToList v
return $ M.fromList $ zip (selectedVertices g vs) scores
foreign import ccall "betweenness"
c_igraph_betweenness :: GraphPtr -> VectorPtr -> VsPtr -> Bool -> VectorPtr -> Bool -> IO CInt
betweenness :: Ord a => Graph d a -> VertexSelector a -> Map a Double
betweenness g vs = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withOptionalWeights g $ \wp ->
withVs vs g $ \vsp ->
withVector v $ \vp ->
c_igraph_betweenness
gp
vp
vsp
True
wp
True
scores <- vectorToList v
return $ M.fromList $ zip (selectedVertices g vs) scores
foreign import ccall "igraph_edge_betweenness"
c_igraph_edge_betweenness :: GraphPtr -> VectorPtr -> Bool -> VectorPtr -> IO CInt
edgeBetweenness :: Ord (Edge d a) => Graph d a -> Map (Edge d a) Double
edgeBetweenness g = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withOptionalWeights g $ \wp ->
withVector v $ \vp ->
c_igraph_edge_betweenness
gp
vp
True
wp
scores <- vectorToList v
return $ M.fromList $ zip (F.toList $ edges g) scores
foreign import ccall "pagerank"
c_igraph_pagerank
:: GraphPtr
-> VectorPtr
-> Ptr CDouble
-> VsPtr
-> Bool
-> CDouble
-> VectorPtr
-> ArpackPtr
-> IO CInt
pagerank
:: Graph d a
-> VertexSelector a
-> Double
-> (Double, [(a,Double)])
pagerank g vs d = unsafePerformIO $ alloca $ \dp -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withVs vs g $ \vsp ->
withOptionalWeights g $ \wp ->
withArpack g $ \ap ->
c_igraph_pagerank
gp
vp
dp
vsp
True
(realToFrac d)
wp
ap
res <- peek dp
lis <- vectorToList v
return (realToFrac res, zip (nodes g) lis)
foreign import ccall "personalized_pagerank"
c_igraph_personalized_pagerank
:: GraphPtr
-> VectorPtr
-> Ptr CDouble
-> VsPtr
-> Bool
-> CDouble
-> VectorPtr
-> VectorPtr
-> ArpackPtr
-> IO CInt
personalizedPagerank
:: Graph d a
-> VertexSelector a
-> Double
-> (Double, [(a, Double)])
personalizedPagerank g vs d = unsafePerformIO $ alloca $ \dp -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withVs vs g $ \vsp ->
withOptionalWeights g $ \wp ->
withArpack g $ \ ap ->
c_igraph_personalized_pagerank
gp
vp
dp
vsp
True
(realToFrac d)
nullPtr
wp
ap
res <- peek dp
lis <- vectorToList v
return (realToFrac res, zip (nodes g) lis)
foreign import ccall "personalized_pagerank_vs"
c_igraph_personalized_pagerank_vs
:: GraphPtr
-> VectorPtr
-> Ptr CDouble
-> VsPtr
-> Bool
-> CDouble
-> VsPtr
-> VectorPtr
-> ArpackPtr
-> IO CInt
personalizedPagerankVs
:: Graph d a
-> VertexSelector a
-> Double
-> VertexSelector a
-> (Double, [(a, Double)])
personalizedPagerankVs g vs d reset = unsafePerformIO $ alloca $ \dp -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withVs vs g $ \vsp ->
withVs reset g $ \resetp ->
withOptionalWeights g $ \wp ->
withArpack g $ \ap ->
c_igraph_personalized_pagerank_vs
gp
vp
dp
vsp
True
(realToFrac d)
resetp
wp
ap
res <- peek dp
lis <- vectorToList v
return (realToFrac res, zip (nodes g) lis)
foreign import ccall "constraint"
c_igraph_constraint :: GraphPtr -> VectorPtr -> VsPtr -> VectorPtr -> IO CInt
constraint :: Ord a => Graph d a -> VertexSelector a -> Map a Double
constraint g vs = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withOptionalWeights g $ \wp ->
withVector v $ \vp ->
withVs vs g $ \vsp ->
c_igraph_constraint
gp
vp
vsp
wp
scores <- vectorToList v
return $ M.fromList $ zip (selectedVertices g vs) scores
foreign import ccall "maxdegree"
c_igraph_maxdegree :: GraphPtr -> Ptr CInt -> VsPtr -> CInt -> Bool -> IO CInt
maxdegree :: Graph d a
-> VertexSelector a
-> Bool
-> Int
maxdegree g vs b = unsafePerformIO $
withGraph g $ \gp ->
withVs vs g $ \vsp ->
alloca $ \ip -> do
_e <- c_igraph_maxdegree
gp
ip
vsp
(getNeiMode g)
b
fromIntegral `fmap` peek ip
foreign import ccall "strength"
c_igraph_strength :: GraphPtr -> VectorPtr -> VsPtr -> CInt -> Bool -> VectorPtr -> IO CInt
strength :: Ord a
=> Graph d a
-> VertexSelector a
-> Bool
-> Map a Int
strength g vs b = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withVs vs g $ \vsp ->
withOptionalWeights g $ \wp ->
c_igraph_strength
gp
vp
vsp
(getNeiMode g)
b
wp
scores <- vectorToList v
return $ M.fromList $ zip (selectedVertices g vs) (map round scores)
foreign import ccall "igraph_eigenvector_centrality"
c_igraph_eigenvector_centrality
:: GraphPtr
-> VectorPtr
-> Ptr CDouble
-> Bool
-> Bool
-> VectorPtr
-> ArpackPtr
-> IO CInt
eigenvectorCentrality
:: Graph d a
-> Bool
-> (Double, [(a, Double)])
eigenvectorCentrality g s = unsafePerformIO $ alloca $ \dp -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withOptionalWeights g $ \wp ->
withArpack g $ \ap ->
c_igraph_eigenvector_centrality
gp
vp
dp
True
s
wp
ap
res <- peek dp
lis <- vectorToList v
return (realToFrac res, zip (nodes g) lis)
foreign import ccall "igraph_hub_score"
c_igraph_hub_score
:: GraphPtr
-> VectorPtr
-> Ptr CDouble
-> Bool
-> VectorPtr
-> ArpackPtr
-> IO CInt
hubScore
:: Graph d a
-> Bool
-> (Double, [(a, Double)])
hubScore g s = unsafePerformIO $ alloca $ \dp -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withOptionalWeights g $ \wp ->
withArpack g $ \ap ->
c_igraph_hub_score
gp
vp
dp
s
wp
ap
res <- peek dp
lis <- vectorToList v
return (realToFrac res, zip (nodes g) lis)
foreign import ccall "igraph_authority_score"
c_igraph_authority_score
:: GraphPtr
-> VectorPtr
-> Ptr CDouble
-> Bool
-> VectorPtr
-> ArpackPtr
-> IO CInt
authorityScore
:: Graph d a
-> Bool
-> (Double, [(a, Double)])
authorityScore g s = unsafePerformIO $ alloca $ \dp -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withOptionalWeights g $ \wp ->
withArpack g $ \ap ->
c_igraph_authority_score
gp
vp
dp
s
wp
ap
res <- peek dp
lis <- vectorToList v
return (realToFrac res, zip (nodes g) lis)
foreign import ccall "closeness_estimate"
c_igraph_closeness_estimate :: GraphPtr -> VectorPtr -> VsPtr -> CInt -> CDouble -> VectorPtr -> IO CInt
closenessEstimate :: Ord a => Graph d a
-> VertexSelector a
-> Int
-> Map a Double
closenessEstimate g vs cutoff = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withVs vs g $ \vsp ->
withOptionalWeights g $ \wp ->
c_igraph_closeness_estimate
gp
vp
vsp
(getNeiMode g)
(fromIntegral cutoff)
wp
scores <- vectorToList v
return $ M.fromList $ zip (selectedVertices g vs) scores
foreign import ccall "betweenness_estimate"
c_igraph_betweenness_estimate :: GraphPtr -> VectorPtr -> VsPtr -> Bool -> CDouble
-> VectorPtr -> Bool -> IO CInt
betweennessEstimate :: Ord a
=> Graph d a
-> VertexSelector a
-> Int
-> Map a Double
betweennessEstimate g@(G _) vs cutoff = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withVs vs g $ \vsp ->
withOptionalWeights g $ \wp ->
c_igraph_betweenness_estimate
gp
vp
vsp
(isDirected g)
(fromIntegral cutoff)
wp
True
scores <- vectorToList v
return $ M.fromList $ zip (selectedVertices g vs) scores
foreign import ccall "igraph_edge_betweenness_estimate"
c_igraph_edge_betweenness_estimate :: GraphPtr -> VectorPtr -> Bool -> CDouble -> VectorPtr -> IO CInt
edgeBetweennessEstimate :: Ord (Edge d a)
=> Graph d a
-> Int
-> Map (Edge d a) Double
edgeBetweennessEstimate g@(G _) cutoff = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withOptionalWeights g $ \wp ->
c_igraph_edge_betweenness_estimate
gp
vp
(isDirected g)
(fromIntegral cutoff)
wp
scores <- vectorToList v
return $ M.fromList $ zip (F.toList $ edges g) scores
foreign import ccall "igraph_centralization_degree"
c_igraph_centralization_degree :: GraphPtr -> VectorPtr -> CInt -> Bool
-> Ptr CDouble -> Ptr CDouble -> Bool -> IO CInt
centralizationDegree :: Ord a => Graph d a
-> Bool
-> Bool
-> (Map a Double, Double, Double)
centralizationDegree g l n = unsafePerformIO $ do
v <- newVector 0
alloca $ \cp ->
alloca $ \tp -> do
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_centralization_degree
gp
vp
(getNeiMode g)
l
cp
tp
n
scores <- vectorToList v
c <- peek cp
t <- peek tp
return ( M.fromList (zip (F.toList (nodes g)) scores)
, realToFrac c
, realToFrac t )
foreign import ccall "igraph_centralization_betweenness"
c_igraph_centralization_betweenness :: GraphPtr -> VectorPtr -> Bool -> Bool
-> Ptr CDouble -> Ptr CDouble -> Bool -> IO CInt
centralizationBetweenness :: Ord a => Graph d a
-> Bool
-> (Map a Double, Double, Double)
centralizationBetweenness g@(G _) n = unsafePerformIO $
alloca $ \cp -> alloca $ \tp -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_centralization_betweenness
gp
vp
(isDirected g)
False
cp
tp
n
scores <- vectorToList v
c <- peek cp
t <- peek tp
return ( M.fromList (zip (F.toList (nodes g)) scores)
, realToFrac c
, realToFrac t )
foreign import ccall "igraph_centralization_closeness"
c_igraph_centralization_closeness
:: GraphPtr -> VectorPtr -> CInt -> Ptr CDouble -> Ptr CDouble -> Bool -> IO CInt
centralizationCloseness :: Ord a => Graph d a
-> Bool
-> (Map a Double, Double, Double)
centralizationCloseness g n = unsafePerformIO $
alloca $ \cp -> alloca $ \tp -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_centralization_closeness
gp
vp
(getNeiMode g)
cp
tp
n
scores <- vectorToList v
c <- peek cp
t <- peek tp
return ( M.fromList (zip (F.toList (nodes g)) scores)
, realToFrac c
, realToFrac t )
foreign import ccall "igraph_centralization_eigenvector_centrality"
c_igraph_centralization_eigenvector_centrality
:: GraphPtr
-> Ptr CDouble
-> Bool
-> Bool
-> ArpackPtr
-> Ptr CDouble
-> Ptr CDouble
-> Bool
-> IO CInt
centralizationEigenvectorCentrality
:: Graph d a
-> Bool
-> Bool
-> (Double, Double, Double)
centralizationEigenvectorCentrality g s n = unsafePerformIO $
alloca $ \evp ->
alloca $ \csp ->
alloca $ \tmp -> do
_e <- withGraph g $ \gp ->
withArpack g $ \ap ->
c_igraph_centralization_eigenvector_centrality
gp
evp
True
s
ap
csp
tmp
n
ev <- peek evp
cs <- peek csp
tm <- peek tmp
return (realToFrac ev,realToFrac cs,realToFrac tm)
foreign import ccall "igraph_centralization_degree_tmax"
c_igraph_centralization_degree_tmax
:: GraphPtr -> CInt -> CInt -> Bool -> Ptr CDouble -> IO CInt
centralizationDegreeTMax :: Either (Graph d a) Int
-> Bool
-> Double
centralizationDegreeTMax egi b = unsafePerformIO $
alloca $ \rp -> do
_e <- withGraph' egi $ \gp ->
c_igraph_centralization_degree_tmax
gp
i
neimode
b
rp
r <- peek rp
return ( realToFrac r )
where
withGraph' (Left g) = withGraph g
withGraph' (Right _) = (\f -> f nullPtr)
i = either (const 0) fromIntegral egi
neimode = either getNeiMode (const (fromIntegral (fromEnum Out))) egi
foreign import ccall "igraph_centralization_betweenness_tmax"
c_igraph_centralization_betweenness_tmax
:: GraphPtr -> CInt -> Bool -> Ptr CDouble -> IO CInt
centralizationBetweennessTMax :: Either (Graph d a) Int
-> Double
centralizationBetweennessTMax egi = unsafePerformIO $
alloca $ \rp -> do
_e <- withGraph' egi $ \gp ->
c_igraph_centralization_betweenness_tmax
gp
i
directed
rp
r <- peek rp
return ( realToFrac r )
where
withGraph' (Left g) = withGraph g
withGraph' (Right _) = (\f -> f nullPtr)
i = either (const 0) fromIntegral egi
directed = either (\g@(G _) -> isDirected g) (const True) egi
foreign import ccall "igraph_centralization_closeness_tmax"
c_igraph_centralization_closeness_tmax
:: GraphPtr -> CInt -> CInt -> Ptr CDouble -> IO CInt
centralizationClosenessTMax :: Either (Graph d a) Int -> Double
centralizationClosenessTMax egi = unsafePerformIO $
alloca $ \rp -> do
_e <- withGraph' egi $ \gp ->
c_igraph_centralization_closeness_tmax
gp
i
neimode
rp
r <- peek rp
return ( realToFrac r )
where
withGraph' (Left g) = withGraph g
withGraph' (Right _) = (\f -> f nullPtr)
i = either (const 0) fromIntegral egi
neimode = either getNeiMode (const (fromIntegral (fromEnum Out))) egi
foreign import ccall "igraph_centralization_eigenvector_centrality_tmax"
c_igraph_centralization_eigenvector_centrality_tmax
:: GraphPtr
-> CInt
-> Bool
-> Bool
-> Ptr CDouble
-> IO CInt
centralizationEigenvectorCentralityTMax
:: Either (Graph d a) Int
-> Bool
-> Bool
-> Double
centralizationEigenvectorCentralityTMax egi dir sc = unsafePerformIO $ alloca $ \dp -> do
_e <- withGraph' egi $ \gp ->
c_igraph_centralization_eigenvector_centrality_tmax
gp
i
dir
sc
dp
realToFrac `fmap` peek dp
where
withGraph' (Left g) = withGraph g
withGraph' (Right _) = (\f -> f nullPtr)
i = either (const 0) fromIntegral egi
foreign import ccall "bibcoupling"
c_igraph_bibcoupling :: GraphPtr -> MatrixPtr -> VsPtr -> IO CInt
bibCoupling :: Graph d a -> VertexSelector a -> [(a,[(a,Int)])]
bibCoupling g vs = unsafePerformIO $ do
let selected = selectedVertices g vs
nrows = length selected
ncols = numberOfNodes g
m <- newMatrix nrows ncols
_e <- withGraph g $ \gp ->
withVs vs g $ \vsp ->
withMatrix m $ \mp ->
c_igraph_bibcoupling
gp
mp
vsp
ids <- map (map round) `fmap` matrixToList m
return $ zip selected (map (zip (nodes g)) ids)
foreign import ccall "cocitation"
c_igraph_cocitation :: GraphPtr -> MatrixPtr -> VsPtr -> IO CInt
cocitation :: Graph d a -> VertexSelector a -> [(a,[(a,Int)])]
cocitation g vs = unsafePerformIO $ do
let selected = selectedVertices g vs
nrows = length selected
ncols = numberOfNodes g
m <- newMatrix nrows ncols
_e <- withGraph g $ \gp ->
withVs vs g $ \vsp ->
withMatrix m $ \mp ->
c_igraph_cocitation
gp
mp
vsp
ids <- map (map round) `fmap` matrixToList m
return $ zip selected (map (zip (nodes g)) ids)
foreign import ccall "similarity_jaccard"
c_igraph_similarity_jaccard :: GraphPtr -> MatrixPtr -> VsPtr -> CInt -> Bool -> IO CInt
similarityJaccard :: Graph d a
-> VertexSelector a
-> Bool
-> [(a,[(a,Double)])]
similarityJaccard g vs loops = unsafePerformIO $ do
let selected = selectedVertices g vs
nrows = length selected
m <- newMatrix nrows nrows
_e <- withGraph g $ \gp ->
withVs vs g $ \vsp ->
withMatrix m $ \mp ->
c_igraph_similarity_jaccard
gp
mp
vsp
(getNeiMode g)
loops
ids <- matrixToList m
return $ zip selected (map (zip selected) ids)
foreign import ccall "igraph_similarity_jaccard_pairs"
c_igraph_similarity_jaccard_pairs
:: GraphPtr
-> VectorPtr
-> VectorPtr
-> CInt
-> Bool
-> IO CInt
similarityJaccardPairs :: Graph d a
-> [Edge d a]
-> Bool
-> [(Edge d a,Double)]
similarityJaccardPairs g@(G _) es loops = unsafePerformIO $ do
p <- listToVector $
foldr (\e r -> nodeToId'' g (edgeFrom e):nodeToId'' g (edgeTo e):r)
[]
es
r <- newVector (length es)
_e <- withGraph g $ \gp ->
withVector r $ \rp ->
withVector p $ \pp ->
c_igraph_similarity_jaccard_pairs
gp
rp
pp
(getNeiMode g)
loops
res <- vectorToList r
return $ zip es res
foreign import ccall "similarity_jaccard_es"
c_igraph_similarity_jaccard_es
:: GraphPtr
-> VectorPtr
-> EsPtr
-> CInt
-> Bool
-> IO CInt
similarityJaccardEs
:: Graph d a
-> EdgeSelector d a
-> Bool
-> [(Edge d a, Double)]
similarityJaccardEs g es b = unsafePerformIO $ do
let sel = selectedEdges g es
v <- newVector (length sel)
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withEs es g $ \esp ->
c_igraph_similarity_jaccard_es
gp
vp
esp
(getNeiMode g)
b
l <- vectorToList v
return $ zip sel l
foreign import ccall "similarity_dice"
c_igraph_similarity_dice
:: GraphPtr
-> MatrixPtr
-> VsPtr
-> CInt
-> Bool
-> IO CInt
similarityDice :: Graph d a
-> VertexSelector a
-> Bool
-> [(a,[(a,Double)])]
similarityDice g vs loops = unsafePerformIO $ do
let sel = selectedVertices g vs
n = length sel
m <- newMatrix n n
_e <- withGraph g $ \gp ->
withVs vs g $ \vsp ->
withMatrix m $ \mp ->
c_igraph_similarity_dice
gp
mp
vsp
(getNeiMode g)
loops
res <- matrixToList m
return $ zip sel (map (zip sel) res)
foreign import ccall "igraph_similarity_dice_pairs"
c_igraph_similarity_dice_pairs
:: GraphPtr
-> VectorPtr
-> VectorPtr
-> CInt
-> Bool
-> IO CInt
similarityDicePairs :: Graph d a
-> [Edge d a]
-> Bool
-> [(Edge d a, Double)]
similarityDicePairs g@(G _) es loops = unsafePerformIO $ do
v <- listToVector $
foldr (\e r -> nodeToId'' g (edgeFrom e) : nodeToId'' g (edgeTo e) : r)
[] es
r <- newVector (length es)
_e <- withGraph g $ \gp ->
withVector r $ \rp ->
withVector v $ \vp ->
c_igraph_similarity_dice_pairs
gp
rp
vp
(getNeiMode g)
loops
res <- vectorToList r
return $ zip es res
foreign import ccall "similarity_dice_es"
c_igraph_similarity_dice_es
:: GraphPtr
-> VectorPtr
-> EsPtr
-> CInt
-> Bool
-> IO CInt
similarityDiceEs
:: Graph d a
-> EdgeSelector d a
-> Bool
-> [(Edge d a, Double)]
similarityDiceEs g es b = unsafePerformIO $ do
let sel = selectedEdges g es
v <- newVector (length sel)
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withEs es g $ \esp ->
c_igraph_similarity_dice_es
gp
vp
esp
(getNeiMode g)
b
l <- vectorToList v
return $ zip sel l
foreign import ccall "similarity_inverse_log_weighted"
c_igraph_similarity_inverse_log_weighted
:: GraphPtr
-> MatrixPtr
-> VsPtr
-> CInt
-> IO CInt
similarityInverseLogWeighted
:: Graph d a
-> VertexSelector a
-> [(a,[(a,Double)])]
similarityInverseLogWeighted g vs = unsafePerformIO $ do
let sel = selectedVertices g vs
nrows = length sel
ncols = numberOfNodes g
m <- newMatrix nrows ncols
_e <- withGraph g $ \gp ->
withMatrix m $ \mp ->
withVs vs g $ \vsp ->
c_igraph_similarity_inverse_log_weighted
gp
mp
vsp
(getNeiMode g)
r <- matrixToList m
return $ zip sel (map (zip (nodes g)) r)
foreign import ccall "igraph_minimum_spanning_tree"
c_igraph_minimum_spanning_tree
:: GraphPtr
-> VectorPtr
-> VectorPtr
-> IO CInt
minimumSpanningTree :: Graph d a -> [Edge d a]
minimumSpanningTree g@(G _) = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withOptionalWeights g $ \wp ->
c_igraph_minimum_spanning_tree
gp
vp
wp
vectorToEdges g v
foreign import ccall "igraph_minimum_spanning_tree_unweighted"
c_igraph_minimum_spanning_tree_unweighted
:: GraphPtr
-> GraphPtr
-> IO CInt
minimumSpanningTreeUnweighted :: IsUnweighted d => Graph d a -> Graph d a
minimumSpanningTreeUnweighted g = unsafePerformIO $
withGraph g $ \gp ->
withGraph (emptyWithCtxt g) $ \gp' -> do
setVertexIds gp
_e <- c_igraph_minimum_spanning_tree_unweighted
gp
gp'
subgraphFromPtr g gp'
foreign import ccall "igraph_minimum_spanning_tree_prim"
c_igraph_minimum_spanning_tree_prim
:: GraphPtr
-> GraphPtr
-> VectorPtr
-> IO CInt
minimumSpanningTreePrim
:: IsUnweighted d
=> Graph (Weighted d) a
-> Graph (Weighted d) a
minimumSpanningTreePrim g = unsafePerformIO $
withGraph g $ \gp ->
withOptionalWeights g $ \wp ->
withGraph (emptyWithCtxt g) $ \gp' -> do
setVertexIds gp
_e <- c_igraph_minimum_spanning_tree_prim
gp
gp'
wp
subgraphFromPtr g gp'
foreign import ccall "igraph_transitivity_undirected"
c_igraph_transitivity_undirected
:: GraphPtr
-> Ptr CDouble
-> CInt
-> IO CInt
transitivityUndirected :: Graph d a -> Double
transitivityUndirected g = unsafePerformIO $ alloca $ \dp -> do
_e <- withGraph g $ \gp ->
c_igraph_transitivity_undirected
gp
dp
(fromIntegral $ fromEnum TransitivityZero)
realToFrac `fmap` peek dp
foreign import ccall "transitivity_local_undirected"
c_igraph_transitivity_local_undirected
:: GraphPtr
-> VectorPtr
-> VsPtr
-> CInt
-> IO CInt
transitivityLocalUndirected :: Graph d a -> VertexSelector a -> [(a,Double)]
transitivityLocalUndirected g vs = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withVs vs g $ \vsp ->
c_igraph_transitivity_local_undirected
gp
vp
vsp
(fromIntegral $ fromEnum TransitivityZero)
let sel = selectedVertices g vs
r <- vectorToList v
return $ zip sel r
foreign import ccall "igraph_transitivity_avglocal_undirected"
c_igraph_transitivity_avglocal_undirected
:: GraphPtr
-> Ptr CDouble
-> CInt
-> IO CInt
transitivityAvglocalUndirected :: Graph d a -> Double
transitivityAvglocalUndirected g = unsafePerformIO $ alloca $ \dp -> do
_e <- withGraph g $ \gp ->
c_igraph_transitivity_avglocal_undirected
gp
dp
(fromIntegral $ fromEnum TransitivityZero)
realToFrac `fmap` peek dp
foreign import ccall "transitivity_barrat"
c_igraph_transitivity_barrat
:: GraphPtr
-> VectorPtr
-> VsPtr
-> VectorPtr
-> CInt
-> IO CInt
transitivityBarrat :: Graph d a -> VertexSelector a -> [(a,Double)]
transitivityBarrat g vs = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withVs vs g $ \vsp ->
withOptionalWeights g $ \wp ->
c_igraph_transitivity_barrat
gp
vp
vsp
wp
(fromIntegral $ fromEnum TransitivityZero)
r <- vectorToList v
return $ zip (selectedVertices g vs) r
foreign import ccall "igraph_laplacian"
c_igraph_laplacian
:: GraphPtr
-> MatrixPtr
-> SpMatrixPtr
-> Bool
-> VectorPtr
-> IO CInt
laplacian :: Graph d a
-> Bool
-> [[Double]]
laplacian g norm = unsafePerformIO $ do
m <- newMatrix 0 0
_e <- withGraph g $ \gp ->
withMatrix m $ \mp ->
withOptionalWeights g $ \wp ->
c_igraph_laplacian
gp
mp
nullPtr
norm
wp
matrixToList m
foreign import ccall "igraph_assortativity_nominal"
c_igraph_assortativity_nominal
:: GraphPtr
-> VectorPtr
-> Ptr CDouble
-> Bool
-> IO CInt
assortativityNominal
:: (Eq vertexType)
=> Graph d (vertexType, a)
-> Bool
-> Double
assortativityNominal g b = unsafePerformIO $ alloca $ \dp -> do
let all_types = map fst $ nodes g
types = map fst $ zip [(0 :: Int)..] (nub all_types)
v <- listToVector types
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_assortativity_nominal
gp
vp
dp
b
realToFrac `fmap` peek dp
foreign import ccall "igraph_assortativity"
c_igraph_assortativity
:: GraphPtr
-> VectorPtr
-> VectorPtr
-> Ptr CDouble
-> Bool
-> IO CInt
assortativity
:: (Eq vertexTypeIncoming, Eq vertexTypeOutgoing)
=> Graph d (vertexTypeIncoming, vertexTypeOutgoing, a)
-> Bool
-> Double
assortativity g b = unsafePerformIO $ alloca $ \dp -> do
let (all_types_inc, all_types_out) = unzip [ (i,o) | (i,o,_) <- nodes g ]
types_inc = map fst $ zip [(0 :: Int)..] (nub all_types_inc)
types_out = map fst $ zip [(0 :: Int)..] (nub all_types_out)
v1 <- listToVector types_inc
v2 <- listToVector types_out
_e <- withGraph g $ \gp ->
withVector v1 $ \vp1 ->
withVector v2 $ \vp2 ->
c_igraph_assortativity
gp
vp1
vp2
dp
b
realToFrac `fmap` peek dp
foreign import ccall "igraph_assortativity_degree"
c_igraph_assortativity_degree
:: GraphPtr
-> Ptr CDouble
-> Bool
-> IO CInt
assortativityDegree
:: Graph d a
-> Bool
-> Double
assortativityDegree g b = unsafePerformIO $ alloca $ \dp -> do
_e <- withGraph g $ \gp ->
c_igraph_assortativity_degree
gp
dp
b
realToFrac `fmap` peek dp
foreign import ccall "igraph_coreness"
c_igraph_coreness
:: GraphPtr
-> VectorPtr
-> CInt
-> IO CInt
coreness :: Graph d a -> [(Double, a)]
coreness g = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_coreness
gp
vp
(getNeiMode g)
r <- vectorToList v
return $ zip r (nodes g)
foreign import ccall "igraph_is_dag"
c_igraph_is_dag
:: GraphPtr
-> Ptr Bool
-> IO CInt
isDAG :: Graph d a -> Bool
isDAG g = unsafePerformIO $ alloca $ \bp -> do
_e <- withGraph g $ \gp ->
c_igraph_is_dag
gp
bp
peek bp
foreign import ccall "igraph_topological_sorting"
c_igraph_topological_sorting
:: GraphPtr
-> VectorPtr
-> CInt
-> IO CInt
topologicalSorting
:: Graph d a
-> [a]
topologicalSorting g = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_topological_sorting
gp
vp
(getNeiMode g)
vectorToVertices g v
foreign import ccall "igraph_feedback_arc_set"
c_igraph_feedback_arc_set
:: GraphPtr
-> VectorPtr
-> VectorPtr
-> CInt
-> IO CInt
feedbackArcSet
:: Graph d a
-> FASAlgorithm
-> [a]
feedbackArcSet g algo = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
withOptionalWeights g $ \wp ->
c_igraph_feedback_arc_set
gp
vp
wp
(fromIntegral $ fromEnum algo)
vectorToVertices g v
foreign import ccall "igraph_maximum_cardinality_search"
c_igraph_maximum_cardinality_search
:: GraphPtr
-> VectorPtr
-> VectorPtr
-> IO CInt
maximumCardinalitySearch
:: Graph d a
-> [(Int, a)]
maximumCardinalitySearch g = unsafePerformIO $ do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_maximum_cardinality_search
gp
vp
nullPtr
r <- vectorToList v
return $ zip (map round r) (nodes g)
foreign import ccall "igraph_is_chordal"
c_igraph_is_chordal
:: GraphPtr
-> VectorPtr
-> VectorPtr
-> Ptr Bool
-> VectorPtr
-> GraphPtr
-> IO CInt
isChordal
:: Graph d a
-> (Bool, [Edge d a])
isChordal g@(G _) = unsafePerformIO $ alloca $ \bp -> do
v <- newVector 0
_e <- withGraph g $ \gp ->
withVector v $ \vp ->
c_igraph_is_chordal
gp
nullPtr
nullPtr
bp
vp
nullPtr
b <- peek bp
as <- vectorToVertices g v
let mkEdges (f:t:r) = (toEdge f t : mkEdges r)
mkEdges [] = []
mkEdges [_] = error "Error in `isChordal': Invalid number of arguments to `mkEdges'"
return (b, mkEdges as)