diff --git a/algorithms.ts b/algorithms.ts index f4a51e8..1dbaf0d 100644 --- a/algorithms.ts +++ b/algorithms.ts @@ -105,6 +105,9 @@ function parseCSV(csv_file: string[]): ParsedCSV { return parsed_csv; } +/** + * Write to a CSV file. + */ async function writeCSV( rows: Array>, file_name = "adjacencyMatrix.csv" @@ -118,6 +121,11 @@ async function writeCSV( await Deno.writeTextFile(file_name, csv); } +/** + * Exports given network into an adjacency matrix in the form of a CSV file. + * @param {Network} network + * @param {} file_name="adjacencyMatrix.csv" + */ export async function writeAdjacencyMatrix( network: Network, file_name = "adjacencyMatrix.csv" diff --git a/network.ts b/network.ts index 6e57f5e..b2aa6e6 100644 --- a/network.ts +++ b/network.ts @@ -151,6 +151,21 @@ export class Network { return this.edges.size / this.max_edges; } + /** + * Ranked neighborhood: list sorted by the number of neighbors by vertex + * @returns { vertex: base_id; neighbors: number }[] + */ + get ranked_neighborhood(): { vertex: base_id; neighbors: number }[] { + return this.vertex_list + .map((vertex) => { + return { + vertex: vertex.id, + neighbors: this.neighbors(vertex.id).length, + }; + }) + .sort((a, b) => (a.neighbors < b.neighbors ? 1 : -1)); + } + /** * @param {EdgeArgs} args */ @@ -598,48 +613,6 @@ export class Network { return average_clustering; } - /** - * Returns a new network with all weighted paths between id and other vertices in the network. - * @param {base_id} id - * @returns Network - */ - weightedPaths(id: base_id): Network { - const weighted_net = this.copy(); - const { vertices } = weighted_net; - vertices.forEach((vertex) => { - vertex.weight = vertex.id === id ? 0 : -1; - }); - const get_path = (initial_vertex_id: base_id) => { - const vertex_neighbors = weighted_net.neighbors(initial_vertex_id); - const initial_vertex = - weighted_net.vertices.get(initial_vertex_id) ?? - new Vertex({ id: initial_vertex_id }); - vertex_neighbors.forEach((vertex_id) => { - const has_edge = weighted_net.hasEdge( - initial_vertex_id, - vertex_id, - weighted_net.is_directed - ); - const vertex = - weighted_net.vertices.get(vertex_id) ?? new Vertex({ id: vertex_id }); - const edge = - weighted_net.edgeBetween(initial_vertex_id, vertex_id) ?? - new Edge({ from: initial_vertex_id, to: vertex_id }); - if ( - has_edge && - (vertex?.weight === -1 || - initial_vertex.weight + edge.weight < vertex.weight) - ) { - vertex.weight = edge.weight + initial_vertex.weight; - vertex.previous_vertex = initial_vertex_id; - get_path(vertex_id); - } - }); - }; - get_path(id); - return weighted_net; - } - /** * Creates a [k-core](https://www.wikiwand.com/en/Degeneracy_(graph_theory)) decomposition of a network. * @param {number} k diff --git a/vertex.ts b/vertex.ts index d9637d8..8d328d8 100644 --- a/vertex.ts +++ b/vertex.ts @@ -3,7 +3,6 @@ import { base_id, VertexArgs } from "./enums.ts"; export class Vertex { readonly id: base_id; weight: number; - previous_vertex?: base_id; /** * Vertex constructor