import GraphNode from "./GraphNode"; export default class Graph { protected isWeighted: boolean; protected isDirected: boolean; protected points: GraphNode[]; protected count = 0; constructor(isWeighted: boolean, isDirected: boolean, points?: GraphNode[]) { this.isWeighted = isWeighted; this.isDirected = isDirected this.points = points || new Array>(); } createPoint(data: T | GraphNode) { let newPoint: GraphNode; if (data instanceof GraphNode) { data.setID(this.count); newPoint = data; } else { newPoint = new GraphNode(data, this.count); } this.points.push(newPoint); this.count++; return newPoint; } addPoints(...points: T[] | GraphNode[]) { for (let point of points) { this.createPoint(point); } } getPointByData(data: T): GraphNode | null { for (let point of this.points) { if (point.data === data) { return point; } } return null; } removePoint(target: GraphNode) { this.points.filter((point: GraphNode) => point !== target); } removePointByData(data: T) { this.points.filter((point: GraphNode) => point.data !== data); } addEdge(start: GraphNode, end: GraphNode, weight?: number) { if (this.isDirected) { this.isWeighted ? start.addEdge(end, weight) : start.addEdge(end); } else { if (this.isWeighted) { start.addEdge(end, weight); end.addEdge(start, weight); } else { start.addEdge(end); end.addEdge(start); } } } removeEdge(start: GraphNode, end: GraphNode) { try { start.removeEdge(end); end.removeEdge(start); } catch(e: any) { throw new Error(e); } } print(excludeOrphans = false) { for (let each of this.points) { each.print(excludeOrphans); } } }