feat: prims algorithm

This commit is contained in:
mpaulson 2022-06-26 08:09:22 -06:00
parent 66e1e54c52
commit 8b5cbd2e91
19 changed files with 160 additions and 62 deletions

View file

@ -1,10 +1,7 @@
import ArrayList from "@code/ArrayList";
import { test_list } from "./ListTest";
test("array-list", function() {
test("array-list", function () {
const list = new ArrayList<number>(3);
test_list(list);
});

View file

@ -1,6 +1,6 @@
import dijkstra_list from "@code/DijkstraList"
import dijkstra_list from "@code/DijkstraList";
test("dijkstra via adj list", function() {
test("dijkstra via adj list", function () {
const list: WeightedAdjacencyList = [];
// (1) --- (4) ---- (5)
@ -8,23 +8,41 @@ test("dijkstra via adj list", function() {
// (0) | ------|------- |
// \ |/ | |
// (1) --- (4) ---- (5)
list[0] = [{to: 1, weight: 3}, {to: 2, weight: 1}];
list[1] = [{to: 0, weight: 3}, {to: 2, weight: 4}, {to: 4, weight: 1}];
list[2] = [{to: 1, weight: 4}, {to: 3, weight: 7}, {to: 0, weight: 1}];
list[3] = [{to: 2, weight: 7}, {to: 4, weight: 5}, {to: 6, weight: 1}];
list[4] = [{to: 1, weight: 1}, {to: 3, weight: 5}, {to: 5, weight: 2}];
list[5] = [{to: 6, weight: 1}, {to: 4, weight: 2}];
list[6] = [{to: 3, weight: 1}, {to: 5, weight: 1}];
list[0] = [
{ to: 1, weight: 3 },
{ to: 2, weight: 1 },
];
list[1] = [
{ to: 0, weight: 3 },
{ to: 2, weight: 4 },
{ to: 4, weight: 1 },
];
list[2] = [
{ to: 1, weight: 4 },
{ to: 3, weight: 7 },
{ to: 0, weight: 1 },
];
list[3] = [
{ to: 2, weight: 7 },
{ to: 4, weight: 5 },
{ to: 6, weight: 1 },
];
list[4] = [
{ to: 1, weight: 1 },
{ to: 3, weight: 5 },
{ to: 5, weight: 2 },
];
list[5] = [
{ to: 6, weight: 1 },
{ to: 4, weight: 2 },
];
list[6] = [
{ to: 3, weight: 1 },
{ to: 5, weight: 1 },
];
/// waht?
// what..
// what...
expect(dijkstra_list(0, 6, list)).toEqual([
0,
1,
4,
5,
6,
]);
expect(dijkstra_list(0, 6, list)).toEqual([0, 1, 4, 5, 6]);
});

View file

@ -1,10 +1,7 @@
import LinkedList from "@code/DoublyLinkedList";
import { test_list } from "./ListTest";
test("DoublyLinkedList", function() {
test("DoublyLinkedList", function () {
const list = new LinkedList<number>(3);
test_list(list);
});

View file

@ -1,10 +1,9 @@
import insertion_sort from "@code/InsertionSort";
test("insertion-sort", function() {
test("insertion-sort", function () {
const arr = [9, 3, 7, 4, 69, 420, 42];
debugger
debugger;
// where is my debugger
insertion_sort(arr);
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
});

View file

@ -13,5 +13,4 @@ export function test_list(list: List<number>): void {
expect(list.removeAt(0)).toEqual(5);
expect(list.removeAt(0)).toEqual(11);
expect(list.length).toEqual(0);
}

View file

@ -1,9 +1,7 @@
import merge_sort from "@code/MergeSort";
test("merge-sort", function() {
test("merge-sort", function () {
const arr = [9, 3, 7, 4, 69, 420, 42];
merge_sort(arr);
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
});

View file

@ -0,0 +1,69 @@
import prims from "@code/PrimsAlgorithm";
test("PrimsAlgorithm", function () {
const list: WeightedAdjacencyList = [];
// (1) --- (4) ---- (5)
// / | | /|
// (0) | ------|------- |
// \ |/ | |
// (1) --- (4) ---- (5)
list[0] = [
{ to: 1, weight: 3 },
{ to: 2, weight: 1 },
];
list[1] = [
{ to: 0, weight: 3 },
{ to: 2, weight: 4 },
{ to: 4, weight: 1 },
];
list[2] = [
{ to: 1, weight: 4 },
{ to: 3, weight: 7 },
{ to: 0, weight: 1 },
];
list[3] = [
{ to: 2, weight: 7 },
{ to: 4, weight: 5 },
{ to: 6, weight: 1 },
];
list[4] = [
{ to: 1, weight: 1 },
{ to: 3, weight: 5 },
{ to: 5, weight: 2 },
];
list[5] = [
{ to: 6, weight: 1 },
{ to: 4, weight: 2 },
];
list[6] = [
{ to: 3, weight: 1 },
{ to: 5, weight: 1 },
];
// there is only one right answer for this graph
expect(prims(list)).toEqual([
[
{ to: 2, weight: 1 },
{ to: 1, weight: 3 },
],
[
{ to: 0, weight: 3 },
{ to: 4, weight: 1 },
],
[{ to: 0, weight: 1 }],
[{ to: 6, weight: 1 }],
[
{ to: 1, weight: 1 },
{ to: 5, weight: 2 },
],
[
{ to: 4, weight: 2 },
{ to: 6, weight: 1 },
],
[
{ to: 5, weight: 1 },
{ to: 3, weight: 1 },
],
]);
});

View file

@ -1,6 +1,6 @@
import Queue from "@code/Queue";
test("queue", function() {
test("queue", function () {
const list = new Queue<number>();
list.enqueue(5);
@ -11,7 +11,7 @@ test("queue", function() {
expect(list.length).toEqual(2);
// this must be wrong..?
debugger
debugger;
// i hate using debuggers
list.enqueue(11);
@ -29,5 +29,3 @@ test("queue", function() {
expect(list.peek()).toEqual(69);
expect(list.length).toEqual(1);
});

View file

@ -1,12 +1,9 @@
import quick_sort from "@code/QuickSort";
test("quick-sort", function() {
test("quick-sort", function () {
const arr = [9, 3, 7, 4, 69, 420, 42];
debugger;
quick_sort(arr);
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
});

View file

@ -1,9 +1,7 @@
import LinkedList from "@code/SingleLinkedList";
import { test_list } from "./ListTest";
test("linked-list", function() {
test("linked-list", function () {
const list = new LinkedList<number>(3);
test_list(list);
});

View file

@ -1,6 +1,6 @@
import Stack from "@code/Stack";
test("stack", function() {
test("stack", function () {
const list = new Stack<number>();
list.push(5);
@ -25,6 +25,3 @@ test("stack", function() {
//yayaya
});

9
src/global.d.ts vendored
View file

@ -6,7 +6,8 @@ declare interface List<T> {
add(item: T): void;
}
declare type GraphEdge = {to: number, weight: number};
declare type CompleteGraphEdge = { from: number; to: number; weight: number };
declare type GraphEdge = { to: number; weight: number };
declare type WeightedAdjacencyList = GraphEdge[][];
declare type WeightedAdjacencyMatrix = number[][]; // A number means weight
@ -17,9 +18,9 @@ declare type BinaryNode<T> = {
value: T;
left: BinaryNode<T>;
right: BinaryNode<T>;
}
};
declare type GeneralNode<T> = {
value: T;
children: GeneralNode<T>[]
}
children: GeneralNode<T>[];
};

13
src/graph.ts Normal file
View file

@ -0,0 +1,13 @@
export function add_node_list(
list: WeightedAdjacencyList,
from: number,
to: number,
weight: number,
): void {
let l = list[from];
if (!l) {
l = list[from] = [];
}
l.push({ to, weight });
}