Skip to content

Commit

Permalink
✨ Write to adjacency matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
rodigu committed Mar 31, 2022
1 parent e546199 commit 1360320
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
38 changes: 37 additions & 1 deletion algorithms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Network } from "./network.ts";
import { ParsedCSV } from "./enums.ts";
import { ParsedCSV, base_id } from "./enums.ts";

/**
* Tries to generate a network with the given number of nodes and edges.
Expand Down Expand Up @@ -104,3 +104,39 @@ function parseCSV(csv_file: string[]): ParsedCSV {

return parsed_csv;
}

async function writeCSV(
rows: Array<Array<string | number>>,
file_name = "adjacencyMatrix.csv"
) {
let csv = "";
rows.forEach((row) => {
row.forEach((element, i) => {
csv += `${element + (i === row.length - 1 ? "\n" : ",")}`;
});
});
await Deno.writeTextFile(file_name, csv);
}

export async function writeAdjacencyMatrix(
network: Network,
file_name = "adjacencyMatrix.csv"
) {
const number_of_rows = network.vertices.size + 1;
const rows: Array<Array<base_id>> = [...Array(number_of_rows)].map(() =>
Array(number_of_rows).fill(0)
);

network.vertex_list.forEach((vertex, i) => {
rows[0][i + 1] = vertex.id;
rows[i + 1][0] = vertex.id;
network.vertex_list.forEach((vertex2, j) => {
if (network.hasEdge(vertex2.id, vertex.id)) {
rows[i + 1][j + 1] = 1;
rows[j + 1][i + 1] = 1;
}
});
});

await writeCSV(rows, file_name);
}
7 changes: 7 additions & 0 deletions tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@ Deno.writeTextFile(
`./data/test_${getTestTime()}_${Math.floor(200 * Math.random())}.txt`,
test_data
);

const randomNet = nex.randomNetworkGen({
number_vertices: 20,
number_edges: 40,
});

nex.writeAdjacencyMatrix(randomNet);

0 comments on commit 1360320

Please sign in to comment.