Commit ea8646e5 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[ILouvain] ineighbors

parent ef7e8303
......@@ -16,6 +16,7 @@ module Data.Graph.Clustering.ILouvain
import Debug.SimpleReflect
import Data.Set (fromList)
import qualified Data.Set as Set
import Data.Maybe (catMaybes, maybe)
import Data.List (zip, cycle, null)
import Protolude hiding (empty, (&))
......@@ -42,37 +43,43 @@ isFlat g = all (isEmpty . snd) (labNodes g)
iLouvain :: (Eq a, Show a)
=> Int -> NodePath -> HyperGraph a a -> HyperGraph a a
iLouvain 0 p g = g
iLouvain n p g = iLouvain' p g (iLouvain (n-1) p g)
iLouvain n p g = trace (show $ toNodes g :: Text) $
iLouvain' p g (iLouvain (n-1) p g)
iLouvain' :: (Eq a, Show a)
=> NodePath
-> HyperGraph a a
-> HyperGraph a a
-> HyperGraph a a
iLouvain' p g g' = iLouvain'' g $ filter (\n -> elem n (nodes g)) ps
where
-- quick trick to filter path but path of HyperGraph can be different
-- ps = nodes g0
ps = if isFlat g'
then case p of
AllNodes -> nodes g
DfsNodes -> path' g
else reverse $ sortOn (Down . length . hnodes g')
$ nodes g
iLouvain' p g g' = trace (show ps :: Text)
iLouvain'' g g' ps -- $ filter (\n -> elem n (nodes g)) ps
where
-- quick trick to filter path but path of HyperGraph can be different
ps = if isFlat g'
then case p of
AllNodes -> nodes g
DfsNodes -> path' g
else sortOn (length . hnodes g') (nodes g')
iLouvain'' :: Show a
=> HyperGraph a a
-> HyperGraph a a
-> [Node]
-> HyperGraph a a
iLouvain'' g [ ] = g
iLouvain'' g [_] = g
iLouvain'' g ns = foldl' (\g1 n -> step g g1 n
$ filter (\m -> elem m (nodes g1))
$ neighbors g n
) g ns
-- /!\ Above fixes possible error
-- g1 has holes in network (case below):
-- iLouvain'' g ns = foldl' (\g1 n -> step' g g1 n $ neighbors g1 n) g ns
iLouvain'' g g' [ ] = g'
iLouvain'' g g' ns = foldl' (\g1 n -> step g g1 n $ ineighbors g1 n ) g' ns
-- | ineihbors Definition
-- TODO optim sort by jackard
ineighbors :: HyperGraph a a -> Node -> [Node]
ineighbors g n = case isEmpty g of
True -> neighbors g n
False -> filter (\m -> Set.null (Set.intersection (fromList $ hnodes g m) n')
&& m /= n) (nodes g)
where
n' = H.exclusion candidates (fromList ns)
candidates = fromList $ concat $ map (neighbors g) ns
ns = hnodes g n
step :: Show a
=> HyperGraph a b
......@@ -92,10 +99,10 @@ step' :: Show a
-> Node
-> Node
-> HyperGraph a a
step' g g' n1 n2 = -- trace ("step" :: Text) $
-- if s2 > 0 && s2 >= s1
if s2 >= s1
then -- trace ("step:mv" :: Text) $
step' g g' n1 n2 = -- trace ("step " <> show n1 <> " " <> show n2 :: Text) $
if s2 > 0 && s2 >= s1
-- if s2 >= s1
then trace ("step:mv " <> show n1 <> " " <> show n2 :: Text) $
mv g' [n1] [n2]
else -- trace ("step:else" :: Text) $
g'
......@@ -105,13 +112,13 @@ step' g g' n1 n2 = -- trace ("step" :: Text) $
s2 = -- trace ("step:mod2" :: Text) $
imodularity g [n1,n2]
stepB :: Show a
stepMax :: Show a
=> HyperGraph a b
-> HyperGraph a a
-> Node
-> [Node]
-> HyperGraph a a
stepB g g' n ns = snd
stepMax g g' n ns = snd
$ maximumBy (\(n1,g1) (n2,g2) -> compare (imodularity g [n1]) (imodularity g [n2])) gs
where
gs = (n, g') : map (\m -> (m, mv g' [n] [m])) ns
......@@ -161,7 +168,7 @@ spoon = mkGraph ns es
, (4, 5, 1.0)
]
-- | Needed functions (WIP)
-- | Needed function
-- mv: a Node elsewhere in the HyperGraph
-- Move Properties:
......@@ -227,6 +234,7 @@ merge = ufold (&)
------------------------------------------------------------------------
test_mv :: (Ord a, Show a) => HyperGraph a a -> Node -> Node -> Bool
test_mv g a b = (mv (mv g [a] [b]) [b,a] []) == g
------------------------------------------------------------------------
-- Paths in the Graph to be tested
-- Directed graph strategy
......
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