Skip to content

Commit 9d59085

Browse files
committed
final release
1 parent 6293d3d commit 9d59085

13 files changed

+378
-118
lines changed

ConvertAdjLists2AdjMatrix.m

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
%{
2+
***************************************************************************************
3+
* Abstract: Returns an Adjacency Matrix from Adjacency Lists
4+
* Uses: This file has been compiled using Matlab R2017b
5+
* Author: Michael Vasquez Otazu
6+
* Email: mitxael@hotmail.it
7+
* History: V1.0 - first release
8+
********************************* START LICENSE BLOCK *********************************
9+
* The MIT License (MIT)
10+
* Copyright (C) 2017 Michael Vasquez Otazu
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
13+
* software and associated documentation files (the "Software"), to deal in the Software
14+
* without restriction, including without limitation the rights to use, copy, modify, merge,
15+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
16+
* to whom the Software is furnished to do so, subject to the following conditions:
17+
*
18+
* The above Copyright notice and this Permission Notice shall be included in all copies
19+
* or substantial portions of the Software.
20+
********************************** END LICENSE BLOCK **********************************
21+
%}
22+
23+
function AdjMatrix = ConvertAdjLists2AdjMatrix(G)
24+
25+
%% Convert to Adjacency Matrix
26+
edgeList = G.Edges{:, {'EndNodes','Weight'}};
27+
nEdge = G.numedges;
28+
nVert = G.numnodes;
29+
AdjMatrix = zeros( nVert, nVert );
30+
31+
for ix = 1 : nEdge
32+
vert1 = edgeList( ix, 1 );
33+
vert2 = edgeList( ix, 2 );
34+
AdjMatrix( vert1, vert2 ) = 1;
35+
AdjMatrix( vert2, vert1 ) = 1;
36+
end
37+
38+
return
39+
40+
end

ConvertAdjMatrix2AugmentedMatrix.m

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
%{
2+
***************************************************************************************
3+
* Abstract: Returns a Weight-augmented Matrix of edges from an AdjacencyMatrix
4+
* Uses: This file has been compiled using Matlab R2017b
5+
* Author: Michael Vasquez Otazu
6+
* Email: mitxael@hotmail.it
7+
* History: V1.0 - first release
8+
********************************* START LICENSE BLOCK *********************************
9+
* The MIT License (MIT)
10+
* Copyright (C) 2017 Michael Vasquez Otazu
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
13+
* software and associated documentation files (the "Software"), to deal in the Software
14+
* without restriction, including without limitation the rights to use, copy, modify, merge,
15+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
16+
* to whom the Software is furnished to do so, subject to the following conditions:
17+
*
18+
* The above Copyright notice and this Permission Notice shall be included in all copies
19+
* or substantial portions of the Software.
20+
********************************** END LICENSE BLOCK **********************************
21+
%}
22+
23+
function AugMatrix = ConvertAdjMatrix2AugmentedMatrix(G, AdjMatrix)
24+
25+
%% PREPARE DATA
26+
E = G.Edges{:, {'EndNodes','Weight'}};
27+
En = [(1:G.numedges)',E];
28+
[rows,cols] = size(AdjMatrix);
29+
AdjMatrix_weighted = [AdjMatrix (1:rows)'];
30+
31+
%% SET WEIGHTS
32+
for i = 1:rows
33+
w = 0;
34+
for j = 1:cols
35+
if (AdjMatrix(i,j) > 0)
36+
idx = find( ((En(:,2)==j) & (En(:,3)==AdjMatrix(i,j))) | ((En(:,2)==AdjMatrix(i,j)) & (En(:,3)==j)) );
37+
w = w + En(idx,4);
38+
end
39+
end
40+
AdjMatrix_weighted(i,j+1) = string(w);
41+
end
42+
43+
%% SORT AUGMENTED MATRIX BY WEIGHT IN NON-INCREASING ORDER
44+
AdjMatrix_sorted = sortrows(AdjMatrix_weighted, j+1);
45+
%AugMatrix = CS_tmp(:,1:j); % Remove cycle weights
46+
AugMatrix = AdjMatrix_sorted;
47+
48+
return
49+
50+
end

CycleSpace.m

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
%{
2+
***************************************************************************************
3+
* Abstract: Determine Cycle Space of a Graph (weighted & undirected)
4+
* Uses: This file has been compiled using Matlab R2017b
5+
* Author: Michael Vasquez Otazu
6+
* Email: mitxael@hotmail.it
7+
* History: V1.0 - first release
8+
********************************* START LICENSE BLOCK *********************************
9+
* The MIT License (MIT)
10+
* Copyright (C) 2017 Michael Vasquez Otazu
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
13+
* software and associated documentation files (the "Software"), to deal in the Software
14+
* without restriction, including without limitation the rights to use, copy, modify, merge,
15+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
16+
* to whom the Software is furnished to do so, subject to the following conditions:
17+
*
18+
* The above Copyright notice and this Permission Notice shall be included in all copies
19+
* or substantial portions of the Software.
20+
********************************** END LICENSE BLOCK **********************************
21+
%}
22+
23+
function CS = CycleSpace(G, varargin)
24+
25+
global A B limit cycleCount;
26+
27+
%% Convert graph to matrix
28+
A = ConvertAdjLists2AdjMatrix(G);
29+
30+
%% Check consistency
31+
if nargin<2, varargin = 1e-10; end
32+
if ( isempty( A ) ), return; end
33+
34+
%% Set variables
35+
nVert = G.numnodes;
36+
B = zeros( 0, nVert );
37+
limit = nVert;
38+
cycleCount = zeros( 1, nVert );
39+
40+
%% Generate all unique triples of connected vertices which have
41+
% indices v1 < v2 < v3 and connections v2 - v1 - v3, then
42+
% search for paths which connect v2 to v3
43+
for ix = 1 : nVert - 2 % v1
44+
for jx = ix + 1 : nVert - 1 % v2
45+
if ( A( ix, jx ) == 1 ) % there's an edge (v1,v2)
46+
pathV = [ zeros( 1, ix ), ones( 1, nVert - ix ) ]; % Initialize pathV, and block v1
47+
pathE = zeros( 1, nVert);
48+
pathV(jx) = 0; % block v2
49+
pathE(jx) = ix; % add edge (v2,v1)
50+
for kx = jx + 1 : nVert % v3
51+
if ( A( kx, ix ) == 1 ) % there's an edge (v3,v1)
52+
% initial path length = 2; now look for extensions
53+
pathE(ix) = kx; % add edge (v1,v3)
54+
nextVert( pathV, pathE, 2, ix, jx, kx );
55+
end
56+
end
57+
end
58+
end
59+
end
60+
61+
CS = B;
62+
63+
end
64+
65+
66+
67+
%% Scope: Extend current path by one additional vertex
68+
% - pathVrtx: Boolean vector of vertices:
69+
% 1 = vertex available for extension
70+
% 0 = vertex blocked (already in path or index lower than v1)
71+
% - pathEdgs: Vector of edges (index=source; value=destination)
72+
function nextVert( pathVrtx, pathEdgs, pathLength, root, v2, v3 )
73+
74+
global A B limit cycleCount;
75+
pathLength = pathLength + 1;
76+
77+
% get candidates for extension
78+
edgesV2 = A( v2, : ); % extract all edges of v2
79+
pathVrtx_A = pathVrtx .* edgesV2; % multiply (elem by elem) pathV and A (i.e. set to "0" all edges regarding blocked vertices)
80+
candS = find( pathVrtx_A ); % return col_indexes of non-zero values (i.e. non-blocked vertices with edges to v2)
81+
82+
for mx = 1 : size( candS, 2 )
83+
cand = candS( mx );
84+
if ( cand == v3 ) % found a cycle!
85+
cycleCount( pathLength ) = cycleCount( pathLength ) + 1;
86+
pathVrtx(cand) = 0; % add vertex into pathV
87+
pathEdgs(cand) = v2; % add edge (v3,v2)
88+
%pathE(v3) = root;
89+
%{
90+
fprintf( 'Cycle of %4d edges:\n', pathLength);
91+
%cycleDim = sum(pathV~=1,2);
92+
disp(pathV);
93+
%}
94+
cycleN = pathEdgs; % save the cycle
95+
%for idx = 1:root-1, cycleN(idx)=1; end % clean from old %"blocked" vertices % only if B is generated from pathV
96+
B = [B;cycleN]; % INSERT CYCLE into cycleSpace
97+
[rr,cc] = size(B);
98+
if (rr==17)
99+
disp('hello');
100+
end
101+
elseif ( pathLength < limit ) % extend again
102+
pathVrtx_new = pathVrtx;
103+
pathEdgs_new = pathEdgs;
104+
pathVrtx_new(cand) = 0; % block vertex just added to path
105+
pathEdgs_new(cand) = v2; % Add edge (v3,v2)
106+
nextVert( pathVrtx_new, pathEdgs_new, pathLength, root, cand, v3 );
107+
end
108+
end
109+
end

FundamentalCycleBasis.m

Lines changed: 0 additions & 54 deletions
This file was deleted.

ImportGraph.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525

2626
G = graph(zeros(0,0)); % create empty graph
2727

28-
%%% READ FROM FILE %%%
28+
%% OPEN FILE
2929
fid = fopen(strcat(path,filename));
3030

31+
%% READ GRAPH SIZE
3132
m = fgets(fid); % number of nodes
3233
n = fgets(fid); % number of edges
3334

35+
%% IMPORT DATA
3436
while ~feof(fid) % read and add edges to G
3537
edge = textscan(fid,'%d %d %f *[^\n]','Delimiter','\b');
3638
u = edge{1}+1;
@@ -39,6 +41,7 @@
3941
G = addedge(G, u, v, w);
4042
end
4143

44+
%% CLOSE FILE
4245
fclose(fid);
4346

4447
return

Lindependent.m

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
%{
2+
***************************************************************************************
3+
* Abstract: Get a linearly independent set of columns from a matrix X
4+
* Uses: This file has been compiled using Matlab R2017b
5+
* Author: Michael Vasquez Otazu
6+
* Email: mitxael@hotmail.it
7+
* History: V1.0 - first release
8+
********************************* START LICENSE BLOCK *********************************
9+
* The MIT License (MIT)
10+
* Copyright (C) 2017 Michael Vasquez Otazu
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
13+
* software and associated documentation files (the "Software"), to deal in the Software
14+
* without restriction, including without limitation the rights to use, copy, modify, merge,
15+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
16+
* to whom the Software is furnished to do so, subject to the following conditions:
17+
*
18+
* The above Copyright notice and this Permission Notice shall be included in all copies
19+
* or substantial portions of the Software.
20+
********************************** END LICENSE BLOCK **********************************
21+
%}
22+
23+
function [Xsub,Xidx] = Lindependent(X,tol)
24+
25+
%% Check if X has no non-zeros and hence no independent columns
26+
if ~nnz(X)
27+
Xsub=[]; Xidx=[];
28+
return
29+
end
30+
31+
%% Set tol, the rank estimation tolerance (default=1e-10)
32+
if nargin<2, tol=1e-10; end
33+
34+
%% Orthogonal-triangular-decomposition so that X*E = Q*R
35+
% Unitary Q, Upper-triangular R, Permutation E
36+
[Q, R, E] = qr(X,0);
37+
38+
%% Estimate the rank of the independent set
39+
if ~isvector(R)
40+
diagr = abs(diag(R)); % Diagonal matrix of R (with ABSolute values)
41+
else
42+
diagr = R(1);
43+
end
44+
r = find(diagr >= tol*diagr(1), 1, 'last');
45+
46+
%% Select linearly-independent columns
47+
[rows, cols] = size(X);
48+
elemN = 1;
49+
idx = 1;
50+
Xsub = zeros(rows,0);
51+
Xidx = [];
52+
while (elemN < r & idx < cols)
53+
if R(rows, idx) == 0
54+
tmp = X(:,idx);
55+
Xsub = [Xsub tmp];
56+
Xidx = [Xidx idx];
57+
elemN = elemN + 1;
58+
end
59+
idx = idx + 1;
60+
end
61+
62+
%Xidx = sort(E(1:r));
63+
%Xsub = X(:,Xidx);
64+
65+
return
66+
67+
end

0 commit comments

Comments
 (0)