Skip to content

Commit 480a562

Browse files
Mantas Mikaitishigham
andcommitted
First version of Anymatrix
co-authored-by: Nicholas. J. Higham <[email protected]> co-authored-by: Mantas Mikaitis <[email protected]>
0 parents  commit 480a562

File tree

747 files changed

+30184
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

747 files changed

+30184
-0
lines changed

anymatrix.m

Lines changed: 474 additions & 0 deletions
Large diffs are not rendered by default.

anymatrix_alias.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
% Template for an alias of anymatrix.
2+
% Rename the file and the function name as appropriate.
3+
% For example am.m and am().
4+
function varargout = anymatrix_alias(varargin)
5+
[varargout{1:nargout}] = anymatrix(varargin{1:nargin});
6+
end

contest/anymatrix_contest.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
% A function that links anymatrix to the matrix-generating functions
2+
% inside the /private folders of each group.
3+
function varargout = anymatrix_contest(matrix_name, varargin)
4+
handle = str2func(matrix_name);
5+
[varargout{1:nargout}] = handle(varargin{1:nargin-1});
6+
end

contest/private/Contents.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
% CONTEST.
2+
% Version 1.2 20-May-2008
3+
% Alan Taylor and Des Higham
4+
%
5+
% baitsample - Bait and prey subsampling
6+
% curvature - Curvatures (clustering coefficients)
7+
% erdrey - Erdos-Renyi model graph
8+
% geo - Geometric random graph
9+
% gilbert - Gilbert model graph
10+
% kleinberg - Kleinberg model graph
11+
% lap - Laplacian matrix (normalized or unnormalized)
12+
% lockandkey - Lock and key model graph
13+
% mht - Mean hitting times
14+
% pagerank - PageRank matrix
15+
% pathlength - Minimum path lengths
16+
% pref - Scale free random graph
17+
% renga - Range dependent random graph
18+
% rewire - Redirect edges
19+
% short - Add shortcuts
20+
% smallw - Small world random graph
21+
% sticky - Stickiness model random graph
22+
% unisample - Uniform subsampling
23+
For documentation see
24+
- https://www.maths.ed.ac.uk/~dhigham/CONTEST_package.html
25+
- Alan Taylor and Desmond J. Higham. CONTEST: A Controllable Test Matrix
26+
Toolbox for MATLAB. ACM Trans. Math. Software, 35(4):26:1--26:17, 2009.
27+
https://doi.org/10.1145/1462173.1462175
28+
Included with permission.

contest/private/am_properties.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function P = am_properties
2+
3+
P = {
4+
'baitsample', {'random', 'symmetric', 'sparse'}
5+
'curvature', {}
6+
'erdrey', {'random', 'symmetric', 'sparse'}
7+
'geo', {'random', 'symmetric', 'sparse'}
8+
'gilbert', {'random', 'symmetric', 'sparse'}
9+
'kleinberg', {'random', 'symmetric', 'sparse'}
10+
'lap', {'random', 'symmetric', 'sparse'}
11+
'lockandkey', {'random', 'symmetric', 'sparse'}
12+
'mht', {'random', 'sparse'}
13+
'pagerank', {'random', 'sparse'}
14+
'pathlength', {'random', 'symmetric', 'sparse'}
15+
'pref', {'random', 'symmetric', 'sparse'}
16+
'renga', {'random', 'symmetric', 'sparse'}
17+
'rewire', {'random', 'symmetric', 'sparse'}
18+
'short', {'random', 'symmetric', 'sparse'}
19+
'smallw', {'random', 'symmetric', 'sparse'}
20+
'sticky', {'random', 'symmetric', 'sparse'}
21+
'unisample', {'random', 'symmetric', 'sparse'}
22+
};

contest/private/baitsample.m

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
function B = baitsample(A,bait,prey)
2+
3+
%BAITSAMPLE bait and prey subsampling
4+
%
5+
% Input A: n by n adjacency matrix
6+
% bait: proportion of nodes to sample. Defaults to 0.5.
7+
% prey: proportion of edges to retain from each "bait" node.
8+
% Defaults to 0.5.
9+
%
10+
% Output B: adjacency matrix with the attribute sparse.
11+
% Dimension of B cannot be predicted.
12+
%
13+
% Description: The adjacency matrix A is considered, and a
14+
% proportion, bait, of its rows/columns are retained.
15+
% Of these, a proportion, prey, of the outgoing edges
16+
% are retained. All other entries are set to zero.
17+
% Disconnected nodes are then removed.
18+
%
19+
% Reference: J. Han, D. Dupuy, N. Bertin, M. Cusick, M. Vidal,
20+
% Effect of sampling on topology predictions of
21+
% protein-protein interaction networks,
22+
% Nature Biotechnology 23 (2005), pp. 839-844.
23+
%
24+
% Example: B = baitsample(A,0.1,0.4);
25+
26+
if nargin <= 2
27+
prey = 0.5;
28+
if nargin == 1
29+
bait = 0.5;
30+
end
31+
end
32+
33+
n = length(A);
34+
B = sparse(n,n);
35+
36+
rp = randperm(n);
37+
ibait = rp(1:ceil(bait*n)); % bait rows/columns to be retained
38+
39+
for i = 1:length(ibait)
40+
41+
B(ibait(i),:) = A(ibait(i),:);
42+
B(:,ibait(i)) = A(:,ibait(i));
43+
iprey = find(B(ibait(i),:));
44+
psum = ceil(prey*length(iprey)); % number of prey nodes for ith bait
45+
46+
if length(iprey) ~= 0
47+
dist = (1/length(iprey)) : (1/length(iprey)) : 1;
48+
49+
while length(find(B(ibait(i),:))) > psum
50+
51+
r = rand;
52+
pos = min(find(r<=dist));
53+
B(ibait(i),iprey(pos))=0;
54+
B(iprey(pos),ibait(i))=0;
55+
56+
end
57+
58+
end
59+
60+
end
61+
62+
for i = 1 : n % trim isolated nodes
63+
if ~any(B(:,(n-i+1)))
64+
B(:,(n-i+1)) = [];
65+
B((n-i+1),:) = [];
66+
end
67+
end

contest/private/curvature.m

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function C = curvature(A,ind)
2+
3+
%CURVATURE Compute the curvatures (clustering coefficients) for a given
4+
% adjacency matrix.
5+
%
6+
% Input A: n by n adjacency matrix.
7+
% ind: node index (optional). Can also take string values
8+
% 'max' for maximum and 'ave' for average, ignoring those
9+
% that are undefined.
10+
%
11+
% Output C: n by 1 vector of curvatures (scalar if ind is provided).
12+
% Undefined values are returned as NaN.
13+
%
14+
% Description: Calculates the curvatures of the nodes in a graph.
15+
% Defined for each node by the frequency with which
16+
% its neighbours are themselves connected.
17+
%
18+
% Examples: C = curvature(A) vector of curvatures
19+
% C = curvature(A,4) curvature of 4th node
20+
% C = curvature(A,'max') largest curvature
21+
% C = curvature(A,'ave') average curvature
22+
23+
n = length(A);
24+
25+
v = sum(A);
26+
27+
b = diag(A^3);
28+
d = v.*(v-1);
29+
30+
ccoefs = zeros(1,n);
31+
32+
for i = 1:n
33+
if d(i) <= 1
34+
ccoefs(i) = NaN;
35+
else
36+
ccoefs(i) = b(i)/d(i);
37+
end
38+
end
39+
40+
if nargin==1
41+
C = ccoefs;
42+
else
43+
if strcmp(ind,'max')
44+
C = max(ccoefs);
45+
else
46+
47+
if strcmp(ind,'ave')
48+
C = mean(ccoefs(find(~isnan(ccoefs))));
49+
else
50+
51+
C = ccoefs(ind);
52+
end
53+
end
54+
end

contest/private/erdrey.m

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function A = erdrey(n,m)
2+
3+
%ERDREY Generate adjacency matrix for a G(n,m) type random graph.
4+
%
5+
% Input n: dimension of matrix (number of nodes in graph).
6+
% m: 2*m is the number of 1's in matrix (number of edges in graph).
7+
% Defaults to the smallest integer larger than n*log(n)/2.
8+
%
9+
% Output A: n by n symmetric matrix with the attribute sparse.
10+
%
11+
%
12+
% Description: An undirected graph is chosen uniformly at random from
13+
% the set of all symmetric graphs with n nodes and m
14+
% edges.
15+
%
16+
% Reference: P. Erdos, A. Renyi,
17+
% On Random Graphs,
18+
% Publ. Math. Debrecen, 6 1959, pp. 290-297.
19+
%
20+
% Example: A = erdrey(100,10);
21+
22+
if nargin == 1
23+
m = ceil(n*log(n)/2);
24+
end
25+
26+
nonzeros = ceil(0.5*n*(n-1)*rand(m,1));
27+
v = zeros(n,1);
28+
for count = 1:n
29+
v(count) = count*(count-1)/2;
30+
end
31+
32+
I = zeros(m,1);
33+
J = zeros(m,1);
34+
S = ones(m,1);
35+
36+
for count = 1:m
37+
i = min(find(v >= nonzeros(count)));
38+
j = nonzeros(count) - (i-1)*(i-2)/2;
39+
I(count) = i;
40+
J(count) = j;
41+
end
42+
43+
A = sign(sparse([I;J],[J;I],[S;S],n,n));
44+
45+
while nnz(A) ~= 2*m
46+
47+
difference = m-nnz(A)/2;
48+
Inew = zeros(difference,1);
49+
Jnew = zeros(difference,1);
50+
for count = 1:difference
51+
index = ceil(0.5*n*(n-1)*rand);
52+
Inew(count) = min(find(v>=index));
53+
Jnew(count) = index - (Inew(count)-1)*(Inew(count)-2)/2;
54+
end
55+
I = cat(1,I,Inew);
56+
J = cat(1,J,Jnew);
57+
S = ones(length(I),1);
58+
A = sign(sparse([I;J],[J;I],[S;S],n,n));
59+
60+
end

contest/private/geo.m

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
function A = geo(n,r,m,per,pnorm)
2+
3+
%GEO Generate adjacency matrix for a geometric random graph.
4+
%
5+
% Input n: dimension of matrix (number of nodes in graph)
6+
% r: radius used to defined entries (edges). Defaults to the
7+
% square root of 1.44/n.
8+
% m: dimension of coordinate system. Defaults to 2.
9+
% per: periodicity of coordinate system. Periodic if per == 1,
10+
% not periodic if per == 0. Defaults to 0.
11+
% pnorm: norm to measure distance between nodes. Defaults to 2.
12+
%
13+
% Output A: n by n symmetric matrix with the attribute sparse
14+
%
15+
%
16+
% Description: nodes are placed randomly in the unit m-cube.
17+
% An edge is created if two nodes are within distance r.
18+
% Reference: M. Penrose, Geometric Random Graphs,
19+
% Oxford Univeristy Press, 2003.
20+
%
21+
% Example: A = geo(100,0.01,3,1,2);
22+
23+
if nargin <= 4
24+
pnorm = 2;
25+
if nargin <= 3
26+
per = 0;
27+
if nargin <= 2
28+
m = 2;
29+
if nargin == 1
30+
r = sqrt(1.44/n);
31+
end
32+
end
33+
end
34+
end
35+
36+
coords = rand(n,m);
37+
38+
I = [];
39+
J = [];
40+
41+
if per == 0
42+
43+
for i = 2:n
44+
for j = 1:(i-1)
45+
diff = abs(coords(i,:) - coords(j,:));
46+
if norm(diff,pnorm)<=r
47+
J = cat(1,J,j);
48+
I = cat(1,I,i);
49+
end
50+
end
51+
end
52+
53+
end
54+
55+
if per == 1
56+
57+
for i = 2:n
58+
for j = 1:(i-1)
59+
diff = min( abs( coords(i,:) - coords(j,:) ), abs( 1 - abs( coords(i,:) - coords(j,:) ) ) );
60+
if norm(diff,pnorm)<=r
61+
J = cat(1,J,j);
62+
I = cat(1,I,i);
63+
end
64+
end
65+
66+
end
67+
68+
end
69+
70+
S = ones(length(I),1);
71+
A = sparse([I;J],[J;I],[S;S],n,n);

contest/private/gilbert.m

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function A = gilbert(n,p);
2+
3+
%GILBERT Generate adjacency matrix for a G(n,p) type random graph.
4+
%
5+
% Input n: dimension of matrix (number of nodes in graph).
6+
% p: probability that any two nodes are neighbours. Defaults to
7+
% log(n)/n.
8+
%
9+
% Output A: n by n symmetric matrix with the attribute sparse.
10+
%
11+
%
12+
% Description: An undirected graph is created by considering pairs of
13+
% nodes and connecting them with independent probability p.
14+
%
15+
% Reference: E.N. Gilbert,
16+
% Random Graphs,
17+
% Ann. Math. Statist.,30, (1959) pp. 1141-1144.
18+
%
19+
% This code is a direct translation of Algorithm 1 in
20+
% V. Batagelj, U. Brandes,
21+
% Efficient generation of large random networks,
22+
% Phys. Rev. E, 71 (2005).
23+
% This algorithm uses a geometric method to skip over potential edges,
24+
% and has optimal complexity.
25+
%
26+
% Example: A = gilbert(100,0.1);
27+
28+
if nargin == 1
29+
p = log(n)/n;
30+
end
31+
32+
v = zeros(n,1); % Think of lower triangle of n-by-n array ordered
33+
for k = 1:n, % lexographically, row-wise. So v(k) is the biggest
34+
v(k) = k*(k-1)/2; % index appearing in row k.
35+
end
36+
37+
I = zeros(ceil(0.5*p*n^2),1); % We don't know exact length
38+
J = zeros(ceil(0.5*p*n^2),1); % Expected length is 0.5*p*n(n-1)
39+
40+
count = 0;
41+
w = 0;
42+
43+
w = w + 1 + floor(log(1-rand)/log(1-p)); %Lexographical index of next nonzero
44+
while w < n*(n-1)/2;
45+
i = min(find(v >= w)); % Recover i and j from
46+
j = w - (i-1)*(i-2)/2; % lexographical index w
47+
I(count+1) = i;
48+
J(count+1) = j;
49+
count = count + 1;
50+
w = w + 1 + floor(log(1-rand)/log(1-p)); %Lexographical index of next nonzero
51+
end
52+
53+
Ifind = find(I>0); % trim any left-over zeros
54+
I = I(Ifind); % from I and J
55+
Jfind = find(J>0);
56+
J = J(Jfind);
57+
58+
S = ones(length(I),1);
59+
A = sparse([I;J],[J;I],[S;S],n,n);

0 commit comments

Comments
 (0)