Skip to content

Commit

Permalink
💥 📝 ✨
Browse files Browse the repository at this point in the history
- CSV export docs
- Added ranked neighborhood
- removed previous_vertex property from Vertex class
  • Loading branch information
rodigu committed Mar 31, 2022
1 parent 1360320 commit e13e2f6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
8 changes: 8 additions & 0 deletions algorithms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ function parseCSV(csv_file: string[]): ParsedCSV {
return parsed_csv;
}

/**
* Write to a CSV file.
*/
async function writeCSV(
rows: Array<Array<string | number>>,
file_name = "adjacencyMatrix.csv"
Expand All @@ -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"
Expand Down
57 changes: 15 additions & 42 deletions network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion vertex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e13e2f6

Please sign in to comment.