Markov Cluster algorithm

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
Post Reply
koteth
Posts: 3
Joined: Wed Jan 17, 2018 5:58 pm

Markov Cluster algorithm

Post by koteth »

An attempt to implement the MCL (Markov Cluster) algorithm in APL.
I'm still a beginner in APL, any advice is welcome.

https://github.com/koteth/MCL_APL
User avatar
Adam|Dyalog
Posts: 143
Joined: Thu Jun 25, 2015 1:13 pm

Re: Markov Cluster algorithm

Post by Adam|Dyalog »

Looks really cool. How did you generate the graphics? Would you be interested in automatically generating those from within APL?
Roger|Dyalog
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Markov Cluster algorithm

Post by Roger|Dyalog »

Code from https://github.com/koteth/MCL_APL/blob/master/MCL.dyalog:

Code: Select all

matrix ← ⎕csv 'PATH TO MATRIX/matrix.csv'
InflateCoef ← 2

M ← ⍎¨ matrix
id←{⍵ ⍵⍴1,⍵⍴0}
Nor←{+⌿⍵}
Nmat ← {( ( ⍴ ⍵) ⍴ (Nor ⍵)) }
NormColumn ← {( ⍴ ⍵ ) ⍴ ((×/⍴⍵) ⍴⍵)÷((×/⍴⍵)  ⍴ Nmat  ⍵ )}

Expand ← {⍵ +.× ⍵}
Inflate  ←  NormColumn {  (⍴⍺) ⍴ (  (×/⍴⍺) ⍴  ⍺) * ⍵ }

M2 ← NormColumn (M + id (≢ M))
Markov ←  { ( Expand (NormColumn ⍵) ) Inflate InflateCoef }

FP ← (Markov ⍣ ≡ ⊢ M2)

R ← ((+/FP) >0) ⌿ FP
ClustMap ← ⌽↑⍸R

"Improved" version of as follows.
• separate code to create the matrix and to do the Markov iteration
• trivial functions included in-line and left unnamed

Code: Select all

MCL3←{                                                            
  ⍺←2                                 ⍝ inflation coefficient default
  id←{(⍵,⍵)⍴1,⍵⍴0}                    ⍝ identity matrix of order ⍵   
  NormColumn←{⍵÷⍤1+⌿⍵}                                           
  Markov←{⍺ *⍨ +.×⍨ NormColumn ⍵}     ⍝ one Markov iteration         
  FP←⍺ Markov⍣≡NormColumn ⍵+id≢⍵                                 
  ⌽↑⍸(0<+/FP)⌿FP                                                 
}
Roger|Dyalog
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Markov Cluster algorithm

Post by Roger|Dyalog »

Since NormColumn is idempotent and is the first function to be applied in Markov, it is not necessary to apply it before launching the iteration. Whence NormColumn is used just once and can be inserted in-line. Therefore:

      MCL4 ← {⍺←2 ⋄ ⌽↑⍸(0<+/FP)⌿FP← ⍺ {⍺ *⍨ +.×⍨ ⍵÷⍤1+⌿⍵}⍣≡ ⍵+∘.=⍨⍳≢⍵}
koteth
Posts: 3
Joined: Wed Jan 17, 2018 5:58 pm

Re: Markov Cluster algorithm

Post by koteth »

Interesting rewriting. Now is more clear to me how to use the ⍨ operator , thanks.

Roger|Dyalog wrote:Since NormColumn is idempotent and is the first function to be applied in Markov, it is not necessary to apply it before launching the iteration. Whence NormColumn is used just once and can be inserted in-line. Therefore:

      MCL4 ← {⍺←2 ⋄ ⌽↑⍸(0<+/FP)⌿FP← ⍺ {⍺ *⍨ +.×⍨ ⍵÷⍤1+⌿⍵}⍣≡ ⍵+∘.=⍨⍳≢⍵}
koteth
Posts: 3
Joined: Wed Jan 17, 2018 5:58 pm

Re: Markov Cluster algorithm

Post by koteth »

Adam|Dyalog wrote:Looks really cool. How did you generate the graphics? Would you be interested in automatically generating those from within APL?


For the image I've used python with networkx. It would be nice to use APL, i've never tried to generate graphics. I'm currently using Dyalog 16 for linux, would you be able to suggest some starting point?
Roger|Dyalog
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Markov Cluster algorithm

Post by Roger|Dyalog »

koteth wrote:Interesting rewriting. Now is more clear to me how to use the ⍨ operator , thanks.

There are illuminating parallels between dyadic f⍨ and the passive case and monadic f⍨ and the reflexive case for verbs in natural languages.
Nicolas|Dyalog
Posts: 17
Joined: Wed Sep 10, 2008 9:39 am

Re: Markov Cluster algorithm

Post by Nicolas|Dyalog »

koteth wrote:For the image I've used python with networkx. It would be nice to use APL, i've never tried to generate graphics. I'm currently using Dyalog 16 for linux, would you be able to suggest some starting point?


Network graphs are documented in https://sharpplot.com/NetworkMap.htm
If you've never used SharpPlot, see https://sharpplot.com/Languages.htm which should help you translate the C# samples of the rest of the documentation.
Post Reply