-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility_read_matrix.m
89 lines (70 loc) · 2.04 KB
/
utility_read_matrix.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function [E, D, b] = utility_read_matrix(filename, seed, debug)
rng(seed);
graph = readDimacsFile(filename);
E = createIncidenceMatrix(graph);
E = sparse(E);
% Dimension of diagonal block
m = size(E,2);
n = graph.nNodes;
% Generate random elements
D = rand(m, 1);
E = E(1:n-1, 1:end);
[cost,flows] = get_b(graph);
b = [cost;flows];
if debug
fprintf("Number of nodes: %d\n Number of edges: %d\n", n-1, m)
end
end
function [cost ,flows] = get_b(graph)
nNodes = graph.nNodes;
flows = zeros(nNodes-1,1);
for i=1:length(graph.nodesList)-1
node_id = graph.nodesList(i,1);
flows(node_id)=graph.nodesList(i,2);
end
cost = graph.edgeList(:,4);
end
function graph = readDimacsFile(filename)
fid = fopen(filename, 'r');
if fid == -1
error('Unable to open file.');
end
graph = struct();
graph.edgeList = [];
graph.nodesList = [];
cCount = 0;
line = fgetl(fid);
while ~(line == -1)
tokens = strsplit(line);
if tokens{1} == 'c'
cCount = cCount + 1;
if cCount == 5
line = strrep(line, ' ', '');
line = split(line, ':');
nNodes = line(2);
nNodes = str2num(nNodes{1});
end
elseif tokens{1} == 'a'
edge = str2double(tokens(2:end));
graph.edgeList(end+1, :) = edge;
elseif tokens{1} == 'n'
node = str2double(tokens(2:end));
graph.nodesList(end+1, :) = node;
end
line = fgetl(fid);
end
graph.nNodes = nNodes;
fclose(fid);
end
function [indidenceMatrix] = createIncidenceMatrix(graph)
edgeList = graph.edgeList;
nNodes = graph.nNodes;
numEdges = size(edgeList, 1);
indidenceMatrix = zeros(nNodes, numEdges);
for i = 1 : numEdges
vertex1 = edgeList(i, 1);
vertex2 = edgeList(i, 2);
indidenceMatrix(vertex1, i) = 1;
indidenceMatrix(vertex2, i) = -1;
end
end