03/12/2011: Custom routine to generate total flow matrix (via class extension of SGL).  SGLX contains a new concept, sglx (coded as sglx.mata), which inherits (Wiki link) class sgl in its entirety.  The new concept implements a new routine that generates total flow matrix, a matrix that contains aggregate weight information based on results from breadth-first search (Wiki link).  That is, finding that nodes i and k are connected via node j (based on unweighted network), we may want to know the total flow for nodes j and k, w_ij and w_ik=w_ij+w_jk, respectively, given we have separate data on edge weights.  Thus the process is carried out in two stages - in the first stage we generate a weighted directed/undirected adjacency matrix, W.  In the second stage, we use the new option flow() included in sglx.ado, specifying matrix W as the argument, to construct the total flow matrix, T.  Furthermore, using path matrix P, we can calculate the average flow matrix F=T:/P (metaspeak).  Thus example commands may be
. sgl W=adjacency(v1 v2 w), directed replace
. sglx T=flow(v1 v2), directed flow(W) replace infinity(0)
. sgl P=path(v1 v2), directed replace
. mata F=T:/P

Note that infinity() option can be specified to replace missing values in the total flow matrix with a real number.  Also, since sglx inherits sgl, we could have used sglx to produce the weighted adjacency and path matrices. 

Mata code to generate total flow matrix, flow_matrix.mata, is a modification of breadth-first search found in bfs.mata, and both mata files can be found in folder lsgl in the below attachment.  flow_matrix.mata is included in, and thus the compiled Mata library lsgl.mlib contains the compiled code to generate total flow matrix.  sglx.mata, which inherits class sgl and makes a call to function flow_matrix(), is also listed in

The contents of the attachments are identical to SGL except for flow_matrix.mata and sglx.mata in folder lsgl, sglx.ado, and wflorentine_marriages2.dta which has randomly assigned edge weights (as a test dataset).

R code snippet to test total flow matrix routine is pasted below.  'igraph' package is used.  Matrix A is unweighted directed/undirected adjacency matrix and W is weighted directed/undirected adjacency matrix.  A and W are matrices read in using read.csv() and are generated using sgl/sglx.

    # Generate igraph matrix.
    # Define return matrix.
    # Loop through each node.
    for(u in c(1:nrow(A))){
      # Calculate shortest paths to all nodes.
      # Loop shortest paths. 
      for(k in c(1:nrow(g2))){
    t=2; sum=0; exit=0
    if([[k]][t])==TRUE) next

Hirotaka Miura,
Mar 12, 2011, 1:12 PM