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
complete.
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
deriving (Eq)
------------------------------------------------------------------------
-- | Specific FGL needed functions
-- | Find LNode of a node (i.e. a node with label)
lnode :: (Graph gr) => gr a b -> Node -> Maybe (LNode a)
lnode cgr n = case lab cgr n of
Nothing -> Nothing
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
-- | Fold over graph function
xdfsFoldWith :: (Graph gr)
=> CFun a b [Node]
-> CFunFold a b c
......@@ -67,10 +75,12 @@ xdfsFoldWith :: (Graph gr)
-> c
xdfsFoldWith _ _ acc [] _ = acc
xdfsFoldWith _ _ acc _ g | isEmpty g = acc
xdfsFoldWith d f acc (v:vs) g = case match v g of
(Just c, g') -> xdfsFoldWith d f (f c acc) (d c++vs) g'
(Nothing, g') -> xdfsFoldWith d f acc vs g'
xdfsFoldWith d f acc (v:vs) g =
case match v g of
(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.
type FEdge = Double
......@@ -106,7 +116,7 @@ iteration gr cs = xdfsFoldWith suc' step cs (nodes gr) gr
step :: CFunFold a FEdge CGr
step (p, v, l, s) cgr = cgr
where
mNc = nodeCommunity v cgr
mNc = nodeCommunity v cgr
ncs = nodeNeighbours v cgr
-- We move node from community nc into ncs
moves :: Maybe (LNode Community, [LNode Community])
......@@ -162,30 +172,39 @@ moveNode gr n direction c = moveNodeWithNeighbours lnNeighbors n direction c
lnNeighbors :: Adj FEdge
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 lnNeighbors n direction (Community (ns, inwsum, totwsum)) = Community (newNs, newInWsum, newTotWsum)
where
newNs = case direction of
Into -> n:ns
OutOf -> DL.delete n ns
comNeighbors :: Adj FEdge
comNeighbors = filter (\ln -> snd ln `elem` ns) lnNeighbors
nonComNeighbors :: Adj FEdge
nonComNeighbors = filter (\ln -> snd ln `notElem` ns) lnNeighbors
-- Update InWeightSum with connections between node and the community
sumN :: InWeightSum
sumN = sum $ map fst comNeighbors
-- Update TotWeightSum, subtracting connections between node and community
-- and adding connections of node to non-community
sumNonCom :: TotWeightSum
sumNonCom = sum $ map fst nonComNeighbors
directionN = case direction of
Into -> 1
OutOf -> -1
newInWsum = inwsum + directionN * sumN
newTotWsum = totwsum + directionN * (sumN - sumNonCom)
moveNodeWithNeighbours lnNeighbors n direction (Community (ns, inwsum, totwsum)) =
Community (newNs, newInWsum, newTotWsum)
where
newNs = case direction of
Into -> n:ns
OutOf -> DL.delete n ns
newInWsum = inwsum + directionN * sumN
newTotWsum = totwsum + directionN * (sumN - sumNonCom)
directionN :: Double
directionN = case direction of
Into -> 1
OutOf -> -1
-- Update InWeightSum with connections between node and the community
sumN :: InWeightSum
sumN = sum $ map fst comNeighbors
-- 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