Commit c75e7b68 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[FLouvain] some refactoring

parent 2e1077dd
......@@ -19,6 +19,7 @@ exampleRemap gr = gmap remap gr
-- | Run FLouvain.iterate on an example graph
-- Example call:
-- putStrLn $ prettify $ iterateOnce cuiller
-- Prelude.map (fst3 . unCommunity . snd) $ labNodes $ iterateOnce karate
iterateOnce :: Gr () Double -> CGr
iterateOnce gr = iteration fgr cgr
where
......
......@@ -98,6 +98,8 @@ newtype Weight = Weight { unWeight :: Double }
type FEdge b = (Weight, b)
fedgeWeight :: FEdge b -> Double
fedgeWeight = unWeight . fst
sumEdgeWeights :: Adj (FEdge b) -> Double
sumEdgeWeights es = sum $ map (fedgeWeight . fst) es
type FGraph a b = Gr a (FEdge b)
-- Used for k_i in formula (2)
......@@ -143,7 +145,7 @@ type CGr = Gr Community ()
graphWeight :: FGraph a b -> GraphWeightSum
graphWeight gr = GraphWeightSum $ ufold weight' 0 gr
where
weight' (p, _, _, s) acc = acc + sum (map (fedgeWeight . fst) $ p <> s)
weight' (p, _, _, s) acc = acc + (sumEdgeWeights $ p <> s)
-- | Compute initial 'CGr' for a given 'FGraph a b'. This means, put each node
-- in a separate community.
......@@ -163,7 +165,7 @@ initialCGr gr = gmap singletonCom gr
-- no internal links
iws = InWeightSum 0.0
-- just sum over the edges coming into/out of node v
tws = TotWeightSum $ sum $ map (fedgeWeight . fst) edges
tws = TotWeightSum $ sumEdgeWeights edges
-- ALGORITHM
......@@ -178,7 +180,7 @@ delta com ki kiin m = DeltaQ $ acc - dec
where
inWeightSum = comInWeightSum com
totWeightSum = comTotWeightSum com
acc = accL - accR*accR
acc = accL - accR * accR
accL = 0.5 * (unInWeightSum inWeightSum + 2.0 * (unNodeComWeightSum kiin)) / (unGraphWeightSum m)
accR = 0.5 * (unTotWeightSum totWeightSum + unNodeWeightSum ki) / (unGraphWeightSum m)
dec = decL - decM * decM - decR * decR
......@@ -196,7 +198,7 @@ delta com ki kiin m = DeltaQ $ acc - dec
-- We could avoid the higher complexity, eg. by precomputing the whole graph
-- into a HashMap Node [Edge].
iteration :: FGraph a b -> CGr -> CGr
iteration gr cs = xdfsFoldWith suc' (\(_, v, _, _) -> step gw $ context gr $ v) cs (nodes gr) gr
iteration gr cs = xdfsFoldWith suc' (\(_, v, _, _) -> step gw $ context gr v) cs (nodes gr) gr
where
gw = graphWeight gr
--weightSum = ufold weightSum' 0 gr
......
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