testing graph structure

This commit is contained in:
Mikayla Dobson
2022-12-19 18:10:39 -06:00
parent b16f19e500
commit b377cefa78
5 changed files with 52 additions and 22 deletions

View File

@@ -9,7 +9,7 @@
</head> </head>
<body> <body>
<div id="app"> <div id="app">
<h1>Wew</h1> <h1>Workshopping DS/A for Procedural Music Generation</h1>
<div id="where-thing-go"> <div id="where-thing-go">
</div> </div>

View File

@@ -3,10 +3,12 @@ import Edge from "./Edge";
export default class GraphNode<T> extends Node<T> { export default class GraphNode<T> extends Node<T> {
edges: Edge<T>[] edges: Edge<T>[]
id: number
constructor(data: T, edges?: Edge<T>[]) { constructor(data: T, id: number, edges?: Edge<T>[]) {
super(data); super(data);
this.edges = edges || new Array<Edge<T>>(); this.edges = edges || new Array<Edge<T>>();
this.id = id;
} }
addEdge(destination: GraphNode<T>, weight?: number) { addEdge(destination: GraphNode<T>, weight?: number) {
@@ -19,9 +21,11 @@ export default class GraphNode<T> extends Node<T> {
override print() { override print() {
if (this.edges.length) { if (this.edges.length) {
for (let edge of this.edges) {
console.log(`Event ${this.id} --> Event ${edge.end.id}`);
}
} else { } else {
console.log(this.data); console.log(`Event ${this.id} -->`);
} }
} }
} }

View File

@@ -1,9 +1,10 @@
import GraphNode from "./GraphNode"; import GraphNode from "./GraphNode";
export default class Graph<T> { export default class Graph<T> {
isWeighted: boolean; private isWeighted: boolean;
isDirected: boolean; private isDirected: boolean;
points: GraphNode<T>[]; private points: GraphNode<T>[];
private count = 0;
constructor(isWeighted: boolean, isDirected: boolean, points?: GraphNode<T>[]) { constructor(isWeighted: boolean, isDirected: boolean, points?: GraphNode<T>[]) {
this.isWeighted = isWeighted; this.isWeighted = isWeighted;
@@ -12,17 +13,28 @@ export default class Graph<T> {
} }
createPoint(data: T) { createPoint(data: T) {
const newPoint = new GraphNode<T>(data); const newPoint = new GraphNode<T>(data, this.count);
this.points.push(newPoint); this.points.push(newPoint);
this.count++;
return newPoint; return newPoint;
} }
addPoints(...points: GraphNode<T>[]) { addPoints(...points: T[]) {
for (let point of points) { for (let point of points) {
this.points.push(point); this.createPoint(point);
} }
} }
getPointByData(data: T): GraphNode<T> | null {
for (let point of this.points) {
if (point.data === data) {
return point;
}
}
return null;
}
removePoint(target: GraphNode<T>) { removePoint(target: GraphNode<T>) {
this.points.filter((point: GraphNode<T>) => point !== target); this.points.filter((point: GraphNode<T>) => point !== target);
} }
@@ -32,12 +44,16 @@ export default class Graph<T> {
} }
addEdge(start: GraphNode<T>, end: GraphNode<T>, weight?: number) { addEdge(start: GraphNode<T>, end: GraphNode<T>, weight?: number) {
if (this.isWeighted && weight) { if (this.isDirected) {
start.addEdge(end, weight); this.isWeighted ? start.addEdge(end, weight) : start.addEdge(end);
end.addEdge(start, weight);
} else { } else {
start.addEdge(end); if (this.isWeighted) {
end.addEdge(start); start.addEdge(end, weight);
end.addEdge(start, weight);
} else {
start.addEdge(end);
end.addEdge(start);
}
} }
} }

View File

@@ -1,6 +1,8 @@
import { ChordQuality, EventData } from "./index"; import { ChordQuality, EventData } from "./index";
import LinkedList from "../LinkedList/index"; import LinkedList from "../LinkedList/index";
import ListNode from "../LinkedList/ListNode"; import ListNode from "../LinkedList/ListNode";
import Graph from "../Graph/index";
import GraphNode from "../Graph/GraphNode";
const firstEvent: EventData = { const firstEvent: EventData = {
root: "C", root: "C",
@@ -43,16 +45,23 @@ const fourthEvent: EventData = {
const eventlist = new LinkedList<EventData>(); const eventlist = new LinkedList<EventData>();
eventlist.addManyToTail(firstEvent, secondEvent, thirdEvent, fourthEvent); eventlist.addManyToTail(firstEvent, secondEvent, thirdEvent, fourthEvent);
const thirdnode = eventlist.findByData(thirdEvent) as ListNode<EventData>;
// console.log(eventlist.findByNode(thirdnode));
// eventlist.removeHead(); // eventlist.removeHead();
// eventlist.removeTail(); // eventlist.removeTail();
// eventlist.removeAtPosition(3); // eventlist.removeAtPosition(3);
// eventlist.removeByData(fourthEvent); // eventlist.removeByData(fourthEvent);
// const thirdnode = eventlist.findByData(thirdEvent) as ListNode<EventData>; const eventgraph = new Graph<EventData>(false, true);
// console.log(eventlist.findByNode(thirdnode));
console.log(eventlist); eventgraph.addPoints(firstEvent, secondEvent, thirdEvent, fourthEvent);
export default { eventlist } const secondstop = eventgraph.getPointByData(secondEvent) as GraphNode<EventData>;
const thirdstop = eventgraph.getPointByData(thirdEvent) as GraphNode<EventData>;
const fourthstop = eventgraph.getPointByData(fourthEvent) as GraphNode<EventData>;
eventgraph.addEdge(secondstop, thirdstop);
eventgraph.addEdge(thirdstop, secondstop);
eventgraph.addEdge(secondstop, fourthstop);
export default { eventlist, eventgraph }

View File

@@ -1,7 +1,8 @@
import data from "./lib/helpers/sample"; import data from "./lib/helpers/sample";
const { eventlist } = data; const { eventlist, eventgraph } = data;
eventlist.print(); // eventlist.print();
eventgraph.print();
// export default content; // export default content;