From b992a3b4342f983d6cdfdd49ae2592504542fa21 Mon Sep 17 00:00:00 2001 From: Rodrigo Morais <58960796+rodigu@users.noreply.github.com> Date: Wed, 6 Apr 2022 18:34:06 -0700 Subject: [PATCH] :bug::sparkles: cycle product now part of Cycle --- network.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/network.ts b/network.ts index 6e9f4b6..653054b 100644 --- a/network.ts +++ b/network.ts @@ -902,6 +902,7 @@ export class Cycle extends Network { private loop_vertex: base_id; private tip_vertex: base_id; private is_closed: boolean; + private dynamic_product: number; constructor(args: { is_directed: boolean; @@ -922,6 +923,8 @@ export class Cycle extends Network { this.tip_vertex = this.edge_list[0].pairVertex(loop_vertex)!; } this.is_closed = false; + this.dynamic_product = 1; + this.updateProduct(initial_edge); } /** @@ -936,6 +939,11 @@ export class Cycle extends Network { return this.loop_vertex; } + get product(): number { + if (this.is_closed) return this.dynamic_product; + return 0; + } + /** * Returns true if the cycle is closed, otherwise returns false. * @returns boolean @@ -961,6 +969,8 @@ export class Cycle extends Network { this.tip_vertex = edge.from; else this.tip_vertex = edge.to; + this.updateProduct(edge); + return true; } @@ -978,6 +988,7 @@ export class Cycle extends Network { super.addEdge(edge); this.is_closed = true; this.tip_vertex = this.loop_vertex; + this.updateProduct(edge); return true; } @@ -1004,6 +1015,11 @@ export class Cycle extends Network { }); } + private updateProduct(edge: EdgeArgs) { + const weight = edge.weight ?? 1; + this.dynamic_product *= this.tip_vertex === edge.to ? weight : 1 / weight; + } + private canCloseWith(edge: EdgeArgs): boolean { const edge_has_tip_and_loop_vertex = (edge.from === this.tip_vertex && edge.to === this.loop_vertex) ||