]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core/Viz/Graph/Tools/IGraph.hs
Merge branch 'dev-distributional' of ssh://gitlab.iscpif.fr:20022/gargantext/haskell...
[gargantext.git] / src / Gargantext / Core / Viz / Graph / Tools / IGraph.hs
1 {-|
2 Module : Gargantext.Core.Viz.Graph.Tools.IGraph
3 Description : Tools to build Graph
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Reference:
11 * Gábor Csárdi, Tamás Nepusz: The igraph software package for complex network research. InterJournal Complex Systems, 1695, 2006.
12
13 -}
14
15 module Gargantext.Core.Viz.Graph.Tools.IGraph
16 where
17
18 import Data.Serialize
19 import Data.Singletons (SingI)
20 import IGraph hiding (mkGraph, neighbors, edges, nodes, Node, Graph)
21 import Protolude
22 import qualified Data.List as List
23 import qualified IGraph as IG
24 import qualified IGraph.Algorithms.Clique as IG
25 import qualified IGraph.Algorithms.Community as IG
26 import qualified IGraph.Random as IG
27
28 ------------------------------------------------------------------
29 -- | Main Types
30 type Graph_Undirected = IG.Graph 'U () ()
31 type Graph_Directed = IG.Graph 'D () ()
32
33 type Node = IG.Node
34 type Graph = IG.Graph
35
36 ------------------------------------------------------------------
37 -- | Main Graph management Functions
38 neighbors :: IG.Graph d v e -> IG.Node -> [IG.Node]
39 neighbors = IG.neighbors
40
41 edges :: IG.Graph d v e -> [Edge]
42 edges = IG.edges
43
44 nodes :: IG.Graph d v e -> [IG.Node]
45 nodes = IG.nodes
46
47 ------------------------------------------------------------------
48 -- | Partitions
49 maximalCliques :: IG.Graph d v e -> [[Int]]
50 maximalCliques g = IG.maximalCliques g (min',max')
51 where
52 min' = 0
53 max' = 0
54
55 ------------------------------------------------------------------
56 type Seed = Int
57
58 -- | Tools to analyze graphs
59 partitions_spinglass :: (Serialize v, Serialize e)
60 => Seed -> IG.Graph 'U v e -> IO [[Int]]
61 partitions_spinglass s g = do
62 gen <- IG.withSeed s pure
63 pure $ IG.findCommunity g Nothing Nothing IG.spinglass gen
64
65 ------------------------------------------------------------------
66
67 mkGraph :: (SingI d, Ord v,
68 Serialize v, Serialize e) =>
69 [v] -> [LEdge e] -> IG.Graph d v e
70 mkGraph = IG.mkGraph
71
72
73 ------------------------------------------------------------------
74 mkGraphUfromEdges :: [(Int, Int)] -> Graph_Undirected
75 mkGraphUfromEdges es = mkGraph (List.replicate n ()) $ zip es $ repeat ()
76 where
77 (a,b) = List.unzip es
78 n = List.length (List.nub $ a <> b)
79
80 {-
81 mkGraphDfromEdges :: [(Int, Int)] -> Graph_Directed
82 mkGraphDfromEdges = undefined
83 -}