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

@ -2,7 +2,7 @@
"clearMocks": true,
"moduleNameMapper": {
"@code/(.*)": [
"<rootDir>/src/day3/$1"
"<rootDir>/src/day5/$1"
]
},
"preset": "ts-jest"

View file

@ -6,6 +6,7 @@
"dependencies": {
"@types/jest": "^28.1.3",
"jest": "^28.1.1",
"prettier": "^2.7.1",
"ts-jest": "^28.0.5",
"ts-node": "^10.8.1",
"tsconfig-paths": "^4.0.0",
@ -15,16 +16,17 @@
"scripts": {
"test": "jest InsertionSort MergeSort Queue Stack QuickSort DijkstraList",
"clear": "./scripts/clear",
"prettier": "prettier --write ./src",
"generate": "./scripts/generate",
"day": "echo /home/mpaulson/personal/kata/src/day3"
"day": "echo /home/mpaulson/personal/kata/src/day5"
},
"kata_stats": {
"ArrayList": 0,
"DijkstraList": 2,
"InsertionSort": 1,
"MergeSort": 1,
"Queue": 1,
"Stack": 1,
"QuickSort": 1
"DijkstraList": 4,
"InsertionSort": 3,
"MergeSort": 3,
"Queue": 3,
"Stack": 3,
"QuickSort": 3
}
}

10
prettier.config.js Normal file
View file

@ -0,0 +1,10 @@
module.exports = {
tabWidth: 4,
printWidth: 80,
proseWrap: "never",
trailingComma: "all",
singleQuote: false,
semi: true,
};

View file

@ -5,6 +5,3 @@ test("array-list", function() {
const list = new ArrayList<number>(3);
test_list(list);
});

View file

@ -1,4 +1,4 @@
import dijkstra_list from "@code/DijkstraList"
import dijkstra_list from "@code/DijkstraList";
test("dijkstra via adj list", function () {
const list: WeightedAdjacencyList = [];
@ -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

@ -5,6 +5,3 @@ test("DoublyLinkedList", function() {
const list = new LinkedList<number>(3);
test_list(list);
});

View file

@ -2,9 +2,8 @@ import insertion_sort from "@code/InsertionSort";
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

@ -5,5 +5,3 @@ test("merge-sort", function() {
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

@ -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

@ -7,6 +7,3 @@ test("quick-sort", function() {
quick_sort(arr);
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
});

View file

@ -5,5 +5,3 @@ test("linked-list", function() {
const list = new LinkedList<number>(3);
test_list(list);
});

View file

@ -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 });
}

View file

@ -12,7 +12,7 @@
"baseUrl": "src",
"paths": {
"@code/*": [
"day3/*"
"day5/*"
]
}
},

View file

@ -1879,6 +1879,11 @@ pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"
prettier@^2.7.1:
version "2.7.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==
pretty-format@^28.0.0, pretty-format@^28.1.1:
version "28.1.1"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-28.1.1.tgz#f731530394e0f7fcd95aba6b43c50e02d86b95cb"