algo_072
Problem Statement
Problem Statement
Write an algorithm to convert a sparse matrix to triplet form.
Algorithm
procedure SparseToTriplet(MAT, ROW, COL, TRIPLET)
begin
TRIPLET[1, 1] ← ROW
TRIPLET[1, 2] ← COL
k ← 2
count ← 0
for i ← 1 to ROW do
for j ← 1 to COL do
if MAT[i, j] != 0 then
TRIPLET[k, 1] ← i
TRIPLET[k, 2] ← j
TRIPLET[k, 3] ← MAT[i, j]
k ← k + 1
count ← count + 1
endif
endfor
endfor
TRIPLET[1, 3] ← count
end procedure1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21