public static Pair<double, double> COC(CliqueCollection cc) { double simple_sum = 0, complex_sum = 0; Matrix CBCO = cc.CliqueByCliqueOverlap; Vector v = CBCO as Vector; if (CBCO.Cols == 1) return new Pair<double, double>(0.0, 0.0); double denominator = (double)(((double)CBCO.Cols * ((double)CBCO.Cols - 1)) / 2.0); if (v != null) { Vector w = new Vector(v.Size); w.Clear(); for (int i = 0; i < v.Size; ++i) { for (int j = i + 1; j < v.Size; ++j) w[j] += cc.GetCliqueByCliqueOverlap(i, j); } for (int j = 1; j < v.Size; ++j) { double sum = w[j]; simple_sum += (sum / v[j]); if (v[j] > 1) complex_sum += (sum / (v[j] - 1)); } } else { for (int j = 1; j < CBCO.Rows; ++j) { double sum = 0.0; for (int i = 0; i < j; ++i) sum += CBCO[i, j]; simple_sum += sum / CBCO[j, j]; if (CBCO[j, j] > 1) complex_sum += sum / (CBCO[j, j] - 1); } } return new Pair<double, double>(simple_sum / denominator, complex_sum / denominator); }
public Vector GetDiagonalVector() { if (!IsSquareMatrix) { throw new MatrixException("Cannot get diagonal matrix from non-square matrix."); } Vector tmp = new Vector(this.Rows); tmp.Clear(); for (int i = 0; i < _rows; ++i) { tmp[i] = this[i, i]; } tmp.Labels.CopyFrom(_rowLabels); tmp.NetworkId = NetworkId; return(tmp); }
public static Vector operator *(Matrix lhs, Vector rhs) { if (lhs.Cols != rhs.Size) { throw new MatrixException("Dimensions of matrices do not match for multiplication."); } Vector result = new Vector(rhs.Size); result.Labels.CopyFrom(rhs.Labels); result.Clear(); for (int r = 0; r < lhs.Rows; r++) { for (int i = 0; i < lhs.Cols; i++) { result[r] += lhs[r, i] * rhs[i]; } } return(result); }
public static Vector BetweennessCentrality(Matrix m) { int N = m.Rows; Vector BV = new Vector(N); BV.Clear(); for (int s = 0; s < N; ++s) { Queue <int> Q = new Queue <int>(); Q.Enqueue(s); Stack <int> S = new Stack <int>(); List <List <int> > P = new List <List <int> >(N); for (int i = 0; i < N; ++i) { P.Add(new List <int>()); } Vector d = new Vector(N); Algorithms.Fill <double>(d, -1); d[s] = 0; Vector sigma = new Vector(N); sigma[s] = 1; while (Q.Count > 0) { int v = Q.Dequeue(); S.Push(v); for (int w = 0; w < N; ++w) { if (m[v, w] > 0) { if (d[w] < 0) { Q.Enqueue(w); d[w] = d[v] + 1; } if (d[w] == d[v] + 1) { sigma[w] = sigma[w] + sigma[v]; P[w].Add(v); } } } } Vector delta = new Vector(N); delta.Clear(); while (S.Count > 0) { int w = S.Pop(); foreach (int v in P[w]) { delta[v] += (sigma[v] / sigma[w]) * (1 + delta[w]); } if (w != s) { BV[w] += delta[w]; } } } for (int i = 0; i < BV.Size; ++i) { BV[i] *= 100.0 / (((double)N - 1) * ((double)N - 2)); } return(BV); }
private static Matrix SimulateLiberalStageTwo(Matrix M, Matrix EA1, Matrix C, Matrix D, Matrix JC, double br) { Matrix JCC = JC * C; Matrix DC = D * C; int n = M.Rows; Matrix AM = M * EA1; Matrix R = new Matrix(n); for (int i = 0; i < R.Rows; ++i) for (int j = 0; j < R.Cols; ++j) if (i != j && (M[i, j] > 0 || AM[i, j] > 0)) R[i, j] = 1; else R[i, j] = 0; Matrix EE = M * M; Matrix BEE = new Matrix(n); for (int i = 0; i < R.Rows; ++i) for (int j = 0; j < R.Cols; ++j) if (i != j && EE[i, j] > 0) BEE[i, j] = 1; else BEE[i, j] = 0; Matrix CR = R * C; Vector AO = new Vector(n); for (int i = 0; i < AO.Size; ++i) { double sum = br * (CR.GetRowSum(i) - C[i, i]); if (sum <= C[i, i]) AO[i] = 0; else AO[i] = sum; } Matrix F = Matrix.Ones(n, n); F.SetDiagonalFromVector(Vector.Zero(n)); Matrix PAN = (F - R) * C; Matrix PAC = BEE * C; Matrix EA = Matrix.Zero(n, n); UpdateEAMatrix(AO, DC, EA); UpdateEAMatrix(AO, PAC, EA); UpdateEAMatrix(AO, PAN, EA); UpdateEAMatrix(AO, JCC, EA); Matrix BEA = new Matrix(n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) BEA[i, j] = EA[i, j] < double.Epsilon ? 0 : 1; Matrix BEAT = BEA.GetTranspose(); AO.Clear(); PAC.Clear(); EA.Clear(); DC.Clear(); return (BEA + BEAT) / 2; }
public static Vector BetweennessCentrality(Matrix m) { int N = m.Rows; Vector BV = new Vector(N); BV.Clear(); for (int s = 0; s < N; ++s) { Queue<int> Q = new Queue<int>(); Q.Enqueue(s); Stack<int> S = new Stack<int>(); List<List<int>> P = new List<List<int>>(N); for (int i = 0; i < N; ++i) P.Add(new List<int>()); Vector d = new Vector(N); Algorithms.Fill<double>(d, -1); d[s] = 0; Vector sigma = new Vector(N); sigma[s] = 1; while (Q.Count > 0) { int v = Q.Dequeue(); S.Push(v); for (int w = 0; w < N; ++w) { if (m[v, w] > 0) { if (d[w] < 0) { Q.Enqueue(w); d[w] = d[v] + 1; } if (d[w] == d[v] + 1) { sigma[w] = sigma[w] + sigma[v]; P[w].Add(v); } } } } Vector delta = new Vector(N); delta.Clear(); while (S.Count > 0) { int w = S.Pop(); foreach (int v in P[w]) { delta[v] += (sigma[v] / sigma[w]) * (1 + delta[w]); } if (w != s) { BV[w] += delta[w]; } } } for (int i = 0; i < BV.Size; ++i) BV[i] *= 100.0 / (((double)N - 1) * ((double)N - 2)); return BV; }
public static Vector operator *(Matrix lhs, Vector rhs) { if (lhs.Cols != rhs.Size) throw new MatrixException("Dimensions of matrices do not match for multiplication."); Vector result = new Vector(rhs.Size); result.Labels.CopyFrom(rhs.Labels); result.Clear(); for (int r = 0; r < lhs.Rows; r++) for (int i = 0; i < lhs.Cols; i++) result[r] += lhs[r, i] * rhs[i]; return result; }