Commit 1851c3ee authored by Alexandre Delanoë's avatar Alexandre Delanoë

[READ] cosmetics

parent 37e0cd91
...@@ -24,8 +24,11 @@ no further improvement can be achieved and the first phase is then ...@@ -24,8 +24,11 @@ no further improvement can be achieved and the first phase is then
complete. complete.
Reference: Reference:
Blondel, Vincent D; Guillaume, Jean-Loup; Lambiotte, Renaud; Lefebvre, Etienne (9 October 2008). "Fast unfolding of communities in large networks". Journal of Statistical Mechanics: Theory and Experiment. 2008 (10): P10008. arXiv:0803.0476 Freely accessible. doi:10.1088/1742-5468/2008/10/P10008. Blondel, Vincent D; Guillaume, Jean-Loup; Lambiotte, Renaud;
Lefebvre, Etienne (9 October 2008). "Fast unfolding of communities
in large networks". Journal of Statistical Mechanics: Theory and
Experiment. 2008 (10): P10008. arXiv:0803.0476 Freely accessible.
doi:10.1088/1742-5468/2008/10/P10008.
-} -}
...@@ -48,16 +51,21 @@ data ClusteringMethod = Glue | Klue ...@@ -48,16 +51,21 @@ data ClusteringMethod = Glue | Klue
deriving (Eq) deriving (Eq)
------------------------------------------------------------------------
-- | Specific FGL needed functions
-- | Find LNode of a node (i.e. a node with label) -- | Find LNode of a node (i.e. a node with label)
lnode :: (Graph gr) => gr a b -> Node -> Maybe (LNode a) lnode :: (Graph gr) => gr a b -> Node -> Maybe (LNode a)
lnode cgr n = case lab cgr n of lnode cgr n = case lab cgr n of
Nothing -> Nothing Nothing -> Nothing
Just l -> Just (n, l) Just l -> Just (n, l)
-- We need to implement a fold over graph -- | Fold over graph definitions: type and function
-- | Fold over graph type
type CFunFold a b c = Context a b -> c -> c type CFunFold a b c = Context a b -> c -> c
-- | Fold over graph function
xdfsFoldWith :: (Graph gr) xdfsFoldWith :: (Graph gr)
=> CFun a b [Node] => CFun a b [Node]
-> CFunFold a b c -> CFunFold a b c
...@@ -67,10 +75,12 @@ xdfsFoldWith :: (Graph gr) ...@@ -67,10 +75,12 @@ xdfsFoldWith :: (Graph gr)
-> c -> c
xdfsFoldWith _ _ acc [] _ = acc xdfsFoldWith _ _ acc [] _ = acc
xdfsFoldWith _ _ acc _ g | isEmpty g = acc xdfsFoldWith _ _ acc _ g | isEmpty g = acc
xdfsFoldWith d f acc (v:vs) g = case match v g of xdfsFoldWith d f acc (v:vs) g =
(Just c, g') -> xdfsFoldWith d f (f c acc) (d c++vs) g' case match v g of
(Nothing, g') -> xdfsFoldWith d f acc vs g' (Just c, g') -> xdfsFoldWith d f (f c acc) (d c++vs) g'
(Nothing, g') -> xdfsFoldWith d f acc vs g'
------------------------------------------------------------------------
-- Our basic graph. Nodes have custom labels. Edges have weight assigned to them. -- Our basic graph. Nodes have custom labels. Edges have weight assigned to them.
type FEdge = Double type FEdge = Double
...@@ -106,7 +116,7 @@ iteration gr cs = xdfsFoldWith suc' step cs (nodes gr) gr ...@@ -106,7 +116,7 @@ iteration gr cs = xdfsFoldWith suc' step cs (nodes gr) gr
step :: CFunFold a FEdge CGr step :: CFunFold a FEdge CGr
step (p, v, l, s) cgr = cgr step (p, v, l, s) cgr = cgr
where where
mNc = nodeCommunity v cgr mNc = nodeCommunity v cgr
ncs = nodeNeighbours v cgr ncs = nodeNeighbours v cgr
-- We move node from community nc into ncs -- We move node from community nc into ncs
moves :: Maybe (LNode Community, [LNode Community]) moves :: Maybe (LNode Community, [LNode Community])
...@@ -162,30 +172,39 @@ moveNode gr n direction c = moveNodeWithNeighbours lnNeighbors n direction c ...@@ -162,30 +172,39 @@ moveNode gr n direction c = moveNodeWithNeighbours lnNeighbors n direction c
lnNeighbors :: Adj FEdge lnNeighbors :: Adj FEdge
lnNeighbors = lneighbors gr n lnNeighbors = lneighbors gr n
-- | Same asa 'moveNode' above but with only node neighbours, not whole graph -- | Same as 'moveNode' above but with only node neighbours, not whole graph
moveNodeWithNeighbours :: Adj FEdge -> Node -> Direction -> Community -> Community moveNodeWithNeighbours :: Adj FEdge -> Node -> Direction -> Community -> Community
moveNodeWithNeighbours lnNeighbors n direction (Community (ns, inwsum, totwsum)) = Community (newNs, newInWsum, newTotWsum) moveNodeWithNeighbours lnNeighbors n direction (Community (ns, inwsum, totwsum)) =
where Community (newNs, newInWsum, newTotWsum)
newNs = case direction of where
Into -> n:ns
OutOf -> DL.delete n ns newNs = case direction of
comNeighbors :: Adj FEdge Into -> n:ns
comNeighbors = filter (\ln -> snd ln `elem` ns) lnNeighbors OutOf -> DL.delete n ns
nonComNeighbors :: Adj FEdge
nonComNeighbors = filter (\ln -> snd ln `notElem` ns) lnNeighbors newInWsum = inwsum + directionN * sumN
-- Update InWeightSum with connections between node and the community newTotWsum = totwsum + directionN * (sumN - sumNonCom)
sumN :: InWeightSum
sumN = sum $ map fst comNeighbors directionN :: Double
-- Update TotWeightSum, subtracting connections between node and community directionN = case direction of
-- and adding connections of node to non-community Into -> 1
sumNonCom :: TotWeightSum OutOf -> -1
sumNonCom = sum $ map fst nonComNeighbors
directionN = case direction of -- Update InWeightSum with connections between node and the community
Into -> 1 sumN :: InWeightSum
OutOf -> -1 sumN = sum $ map fst comNeighbors
newInWsum = inwsum + directionN * sumN
newTotWsum = totwsum + directionN * (sumN - sumNonCom) -- Update TotWeightSum, subtracting connections between node and community
-- and adding connections of node to non-community
sumNonCom :: TotWeightSum
sumNonCom = sum $ map fst nonComNeighbors
-- Node Adj Context
comNeighbors :: Adj FEdge
comNeighbors = filter (\ln -> snd ln `elem` ns) lnNeighbors
nonComNeighbors :: Adj FEdge
nonComNeighbors = filter (\ln -> snd ln `notElem` ns) lnNeighbors
{- {-
......
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