Skip to content

Commit

Permalink
Merge pull request #7 from rodigu/cycles
Browse files Browse the repository at this point in the history
Implemented Cycle Class
  • Loading branch information
rodigu committed Apr 3, 2022
2 parents 34d869a + 905de07 commit aa491cd
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/data/
19 changes: 18 additions & 1 deletion edge.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { base_id, EdgeArgs } from "./enums.ts";

export class Edge {
private to: base_id;
private from: base_id;
private to: base_id;
weight: number;

/**
Expand All @@ -29,6 +29,23 @@ export class Edge {
return { from: this.from, to: this.to, weight: this.weight };
}

isSameAs(edge: Edge, is_directed = false): boolean {
const { vertices } = this;
return (
(edge.vertices.from === vertices.from &&
edge.vertices.to === vertices.to) ||
(!is_directed &&
edge.vertices.from === vertices.to &&
edge.vertices.to === vertices.from)
);
}

pairVertex(vertex_id: base_id): base_id | undefined {
if (vertex_id === this.to) return this.from;
else if (vertex_id === this.from) return this.to;
return undefined;
}

hasVertex(vertex_id: base_id): boolean {
if (this.from === vertex_id || this.to === vertex_id) return true;
return false;
Expand Down
3 changes: 1 addition & 2 deletions enums.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export type base_id = string | number;

export type Cycle = base_id[];

export interface EdgeNeighborhood {
from: {
id: base_id;
Expand Down Expand Up @@ -45,4 +43,5 @@ export const ERROR = {
NOT_MULTIGRAPH:
"Trying to add multiple edges between two vertices. Graph is not a multigraph!",
UNDEFINED_ID: "Tried to use undefined id as input",
SELF_LOOP: "No self-loops",
};
Loading

0 comments on commit aa491cd

Please sign in to comment.