dear awk gurus,
i would need a fast (therefore) awk solution for the reformation of an uncomplete weighted adjacency list to a complete sorted adjacency matrix.
example (FS=OFS=,):
a,d,0.33
a,b,0.25
b,c,0.11
should give:
,a,b,c,d
a,1,0.25,0,0.33
b,0.25,1,0.11,0
c,0,0.11,1,0
d,0.33,0,0,1
I have found this thread but this does not work, because (as I think) there are missing combinations, which should be zero or 1 for self-combinations (a-a, b-b, ...).
my idea would be, to read in a 2-dimensional array with the first two columns as indices (already also with transposed indices), build the union of both indices (should not be necessary, if I already transpose the indices?!?), sort the indices, loop two times over all indices and write line for line the values of the array, including zeros for all missing index-pairs and '1' for all self-index pairs.
I can provide a few code snippets, but some problems are outside my awk-capabilities:
awk 'BEGIN { FS =","; OFS="," }
w[$1,$2]=$3; w[$2,$1]=$3
<if i am right, both dimensions of indices should be already complete?>
END {
<first i have to write the column headers>
print OFS; for (i in sorted(w)) {print i OFS }; print "\n";
<now i want loop over the alphabetically sorted indices>
for (i in sorted(w)) {
print i OFS; for (j in sorted(w)) {
if (w(i,j) not defined) print 0;
else if (i==j) print 1;
else print w(i,j)
} print "\n" }
}' ADJlist.txt > ADJmatrix.txt
could you please complete my script, or (as i always learn here) provide a much more elegant solution...
dietmar