1 #build similarity matrix W (NOTE : sparse matrix ==> optimizations later)
2 getSimilarityMatrix = function(NI)
4 # using a local sigma would be nice, but break W symmetry,
5 # which cannot easily be repaired then (??!)
6 # ==> we use a global sigma, with a very simple heuristic
10 for (i in 1:n) distances = c(distances,NI$ds[[i]])
11 distances = unique(distances)
12 sigma2 = median(distances)^2 #for example...
14 W = matrix(0.0,nrow=n,ncol=n)
16 W[ i, NI$ix[[i]] ] = exp( - NI$ds[[i]]^2 / sigma2 )
21 #epsilon constant, used as a zero threshold
22 EPS = 100 * .Machine$double.eps
24 #Moore-Penrose pseudo inverse
28 sdiag = s$d ; sdiag[sdiag < EPS] = Inf
29 p = min(nrow(M),ncol(M))
30 sdiag = diag(1.0 / sdiag, p)
31 return ((s$v) %*% sdiag %*% t(s$u))
34 #get distance matrix from data and similarity : Commute Time
35 getECTDistances = function(NI)
37 n = length(NI$ix) ; seqVect = 1:n
38 if (n <= 1) return (0.0) #nothing to do...
40 #get laplacian (...inverse) :
41 W = getSimilarityMatrix(NI)
42 invLap = mppsinv(diag(rowSums(W)) - W)
45 ectd = matrix(0.0, nrow=n, ncol=n)
48 ectd[ij,] = ectd[ij,] + invLap[ij,ij]
49 ectd[,ij] = ectd[,ij] + invLap[ij,ij]
51 ectd = ectd - 2*invLap
55 # Call Dijsktra algorithm on every vertex to build distances matrix
56 getShortestPathDistances = function(NI)
59 distancesIn = matrix(NA,nrow=n,ncol=n)
61 distancesIn[i,NI$ix[[i]]] = NI$ds[[i]]
63 distancesOut = matrix(nrow=n, ncol=n)
65 distancesOut[i,] = .Call("dijkstra", distancesIn, i)
69 ## MAIN CALL to get distances matrix
70 getDistances = function(dtype, NI)
74 distances = getShortestPathDistances(NI)
75 else if (dtype=="ectd")
76 distances = getECTDistances(NI)
78 diag(distances) = 0.0 #distances to self are zero