1 {-| Module : Gargantext.Viz.Graph.FGL
2 Description : FGL main functions used in Garg
3 Copyright : (c) CNRS, 2017-Present
4 License : AGPL + CECILL v3
5 Maintainer : team@gargantext.org
6 Stability : experimental
9 Main FGL funs/types to ease portability with IGraph.
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE ConstraintKinds #-}
16 module Gargantext.Viz.Graph.FGL where
18 import Gargantext.Prelude
19 import qualified Data.Graph.Inductive as FGL
20 import Data.List as List
21 ------------------------------------------------------------------
24 type Graph_Undirected = FGL.Gr () ()
25 type Graph_Directed = FGL.Gr () ()
27 type Graph = FGL.Graph
31 ------------------------------------------------------------------
34 mkGraph :: [Node] -> [Edge] -> Graph_Undirected
35 mkGraph = FGL.mkUGraph
37 neighbors :: Graph gr => gr a b -> Node -> [Node]
38 neighbors = FGL.neighbors
40 -- | TODO bug: if graph is undirected, we need to filter
41 -- nub . (map (\(n1,n2) -> if n1 < n2 then (n1,n2) else (n2,n1))) . FGL.edges
42 edges :: Graph gr => gr a b -> [Edge]
45 nodes :: Graph gr => gr a b -> [Node]
48 ------------------------------------------------------------------------
51 filterNeighbors :: Graph_Undirected -> Node -> [Node]
52 filterNeighbors g n = List.nub $ neighbors g n
54 -- Q: why not D.G.I.deg ? (Int as result)
55 degree :: Graph_Undirected -> Node -> Double
56 degree g n = fromIntegral $ List.length (filterNeighbors g n)
58 vcount :: Graph_Undirected -> Double
59 vcount = fromIntegral . List.length . List.nub . nodes
61 -- | TODO tests, optim and use IGraph library, fix IO ?
62 ecount :: Graph_Undirected -> Double
63 ecount = fromIntegral . List.length . List.nub . edges
66 ------------------------------------------------------------------
67 -- | Main sugared functions
69 mkGraphUfromEdges :: [(Int, Int)] -> Graph_Undirected
70 mkGraphUfromEdges es = mkGraph ns es
72 ns = List.nub (a <> b)
74 (a, b) = List.unzip es