feat: prims algorithm
This commit is contained in:
parent
66e1e54c52
commit
8b5cbd2e91
19 changed files with 160 additions and 62 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
"clearMocks": true,
|
"clearMocks": true,
|
||||||
"moduleNameMapper": {
|
"moduleNameMapper": {
|
||||||
"@code/(.*)": [
|
"@code/(.*)": [
|
||||||
"<rootDir>/src/day3/$1"
|
"<rootDir>/src/day5/$1"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"preset": "ts-jest"
|
"preset": "ts-jest"
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ Literal Improvement Gaining Master and Tutelage on Algorithms
|
||||||
Let's Intelligently Generate Multiple Algorithm Training Assessments // permdaddy
|
Let's Intelligently Generate Multiple Algorithm Training Assessments // permdaddy
|
||||||
|
|
||||||
### Sugma Nuts
|
### Sugma Nuts
|
||||||
Studious Users Get Major Abilities. New Useful Training for Students
|
Studious Users Get Major Abilities. New Useful Training for Students
|
||||||
|
|
||||||
### Ligma Fart
|
### Ligma Fart
|
||||||
Learn Intermediate Groundbreaking Massive Algorithms. Free Algorithm Research & Training System
|
Learn Intermediate Groundbreaking Massive Algorithms. Free Algorithm Research & Training System
|
||||||
|
|
|
||||||
16
package.json
16
package.json
|
|
@ -6,6 +6,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/jest": "^28.1.3",
|
"@types/jest": "^28.1.3",
|
||||||
"jest": "^28.1.1",
|
"jest": "^28.1.1",
|
||||||
|
"prettier": "^2.7.1",
|
||||||
"ts-jest": "^28.0.5",
|
"ts-jest": "^28.0.5",
|
||||||
"ts-node": "^10.8.1",
|
"ts-node": "^10.8.1",
|
||||||
"tsconfig-paths": "^4.0.0",
|
"tsconfig-paths": "^4.0.0",
|
||||||
|
|
@ -15,16 +16,17 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "jest InsertionSort MergeSort Queue Stack QuickSort DijkstraList",
|
"test": "jest InsertionSort MergeSort Queue Stack QuickSort DijkstraList",
|
||||||
"clear": "./scripts/clear",
|
"clear": "./scripts/clear",
|
||||||
|
"prettier": "prettier --write ./src",
|
||||||
"generate": "./scripts/generate",
|
"generate": "./scripts/generate",
|
||||||
"day": "echo /home/mpaulson/personal/kata/src/day3"
|
"day": "echo /home/mpaulson/personal/kata/src/day5"
|
||||||
},
|
},
|
||||||
"kata_stats": {
|
"kata_stats": {
|
||||||
"ArrayList": 0,
|
"ArrayList": 0,
|
||||||
"DijkstraList": 2,
|
"DijkstraList": 4,
|
||||||
"InsertionSort": 1,
|
"InsertionSort": 3,
|
||||||
"MergeSort": 1,
|
"MergeSort": 3,
|
||||||
"Queue": 1,
|
"Queue": 3,
|
||||||
"Stack": 1,
|
"Stack": 3,
|
||||||
"QuickSort": 1
|
"QuickSort": 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
prettier.config.js
Normal file
10
prettier.config.js
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
module.exports = {
|
||||||
|
tabWidth: 4,
|
||||||
|
printWidth: 80,
|
||||||
|
proseWrap: "never",
|
||||||
|
trailingComma: "all",
|
||||||
|
singleQuote: false,
|
||||||
|
semi: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,10 +1,7 @@
|
||||||
import ArrayList from "@code/ArrayList";
|
import ArrayList from "@code/ArrayList";
|
||||||
import { test_list } from "./ListTest";
|
import { test_list } from "./ListTest";
|
||||||
|
|
||||||
test("array-list", function() {
|
test("array-list", function () {
|
||||||
const list = new ArrayList<number>(3);
|
const list = new ArrayList<number>(3);
|
||||||
test_list(list);
|
test_list(list);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 = [];
|
const list: WeightedAdjacencyList = [];
|
||||||
|
|
||||||
// (1) --- (4) ---- (5)
|
// (1) --- (4) ---- (5)
|
||||||
|
|
@ -8,23 +8,41 @@ test("dijkstra via adj list", function() {
|
||||||
// (0) | ------|------- |
|
// (0) | ------|------- |
|
||||||
// \ |/ | |
|
// \ |/ | |
|
||||||
// (1) --- (4) ---- (5)
|
// (1) --- (4) ---- (5)
|
||||||
list[0] = [{to: 1, weight: 3}, {to: 2, weight: 1}];
|
list[0] = [
|
||||||
list[1] = [{to: 0, weight: 3}, {to: 2, weight: 4}, {to: 4, weight: 1}];
|
{ to: 1, weight: 3 },
|
||||||
list[2] = [{to: 1, weight: 4}, {to: 3, weight: 7}, {to: 0, weight: 1}];
|
{ to: 2, 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[1] = [
|
||||||
list[5] = [{to: 6, weight: 1}, {to: 4, weight: 2}];
|
{ to: 0, weight: 3 },
|
||||||
list[6] = [{to: 3, weight: 1}, {to: 5, weight: 1}];
|
{ 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?
|
/// waht?
|
||||||
// what..
|
// what..
|
||||||
// what...
|
// what...
|
||||||
expect(dijkstra_list(0, 6, list)).toEqual([
|
expect(dijkstra_list(0, 6, list)).toEqual([0, 1, 4, 5, 6]);
|
||||||
0,
|
|
||||||
1,
|
|
||||||
4,
|
|
||||||
5,
|
|
||||||
6,
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,7 @@
|
||||||
import LinkedList from "@code/DoublyLinkedList";
|
import LinkedList from "@code/DoublyLinkedList";
|
||||||
import { test_list } from "./ListTest";
|
import { test_list } from "./ListTest";
|
||||||
|
|
||||||
test("DoublyLinkedList", function() {
|
test("DoublyLinkedList", function () {
|
||||||
const list = new LinkedList<number>(3);
|
const list = new LinkedList<number>(3);
|
||||||
test_list(list);
|
test_list(list);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
import insertion_sort from "@code/InsertionSort";
|
import insertion_sort from "@code/InsertionSort";
|
||||||
|
|
||||||
test("insertion-sort", function() {
|
test("insertion-sort", function () {
|
||||||
const arr = [9, 3, 7, 4, 69, 420, 42];
|
const arr = [9, 3, 7, 4, 69, 420, 42];
|
||||||
debugger
|
debugger;
|
||||||
// where is my debugger
|
// where is my debugger
|
||||||
insertion_sort(arr);
|
insertion_sort(arr);
|
||||||
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
|
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,5 +13,4 @@ export function test_list(list: List<number>): void {
|
||||||
expect(list.removeAt(0)).toEqual(5);
|
expect(list.removeAt(0)).toEqual(5);
|
||||||
expect(list.removeAt(0)).toEqual(11);
|
expect(list.removeAt(0)).toEqual(11);
|
||||||
expect(list.length).toEqual(0);
|
expect(list.length).toEqual(0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
import merge_sort from "@code/MergeSort";
|
import merge_sort from "@code/MergeSort";
|
||||||
|
|
||||||
test("merge-sort", function() {
|
test("merge-sort", function () {
|
||||||
const arr = [9, 3, 7, 4, 69, 420, 42];
|
const arr = [9, 3, 7, 4, 69, 420, 42];
|
||||||
merge_sort(arr);
|
merge_sort(arr);
|
||||||
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
|
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
69
src/__tests__/PrimsAlgorithm.ts
Normal file
69
src/__tests__/PrimsAlgorithm.ts
Normal 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 },
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import Queue from "@code/Queue";
|
import Queue from "@code/Queue";
|
||||||
|
|
||||||
test("queue", function() {
|
test("queue", function () {
|
||||||
const list = new Queue<number>();
|
const list = new Queue<number>();
|
||||||
|
|
||||||
list.enqueue(5);
|
list.enqueue(5);
|
||||||
|
|
@ -11,7 +11,7 @@ test("queue", function() {
|
||||||
expect(list.length).toEqual(2);
|
expect(list.length).toEqual(2);
|
||||||
|
|
||||||
// this must be wrong..?
|
// this must be wrong..?
|
||||||
debugger
|
debugger;
|
||||||
|
|
||||||
// i hate using debuggers
|
// i hate using debuggers
|
||||||
list.enqueue(11);
|
list.enqueue(11);
|
||||||
|
|
@ -29,5 +29,3 @@ test("queue", function() {
|
||||||
expect(list.peek()).toEqual(69);
|
expect(list.peek()).toEqual(69);
|
||||||
expect(list.length).toEqual(1);
|
expect(list.length).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,9 @@
|
||||||
import quick_sort from "@code/QuickSort";
|
import quick_sort from "@code/QuickSort";
|
||||||
|
|
||||||
test("quick-sort", function() {
|
test("quick-sort", function () {
|
||||||
const arr = [9, 3, 7, 4, 69, 420, 42];
|
const arr = [9, 3, 7, 4, 69, 420, 42];
|
||||||
|
|
||||||
debugger;
|
debugger;
|
||||||
quick_sort(arr);
|
quick_sort(arr);
|
||||||
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
|
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
import LinkedList from "@code/SingleLinkedList";
|
import LinkedList from "@code/SingleLinkedList";
|
||||||
import { test_list } from "./ListTest";
|
import { test_list } from "./ListTest";
|
||||||
|
|
||||||
test("linked-list", function() {
|
test("linked-list", function () {
|
||||||
const list = new LinkedList<number>(3);
|
const list = new LinkedList<number>(3);
|
||||||
test_list(list);
|
test_list(list);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import Stack from "@code/Stack";
|
import Stack from "@code/Stack";
|
||||||
|
|
||||||
test("stack", function() {
|
test("stack", function () {
|
||||||
const list = new Stack<number>();
|
const list = new Stack<number>();
|
||||||
|
|
||||||
list.push(5);
|
list.push(5);
|
||||||
|
|
@ -25,6 +25,3 @@ test("stack", function() {
|
||||||
|
|
||||||
//yayaya
|
//yayaya
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
9
src/global.d.ts
vendored
9
src/global.d.ts
vendored
|
|
@ -6,7 +6,8 @@ declare interface List<T> {
|
||||||
add(item: T): void;
|
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 WeightedAdjacencyList = GraphEdge[][];
|
||||||
declare type WeightedAdjacencyMatrix = number[][]; // A number means weight
|
declare type WeightedAdjacencyMatrix = number[][]; // A number means weight
|
||||||
|
|
||||||
|
|
@ -17,9 +18,9 @@ declare type BinaryNode<T> = {
|
||||||
value: T;
|
value: T;
|
||||||
left: BinaryNode<T>;
|
left: BinaryNode<T>;
|
||||||
right: BinaryNode<T>;
|
right: BinaryNode<T>;
|
||||||
}
|
};
|
||||||
|
|
||||||
declare type GeneralNode<T> = {
|
declare type GeneralNode<T> = {
|
||||||
value: T;
|
value: T;
|
||||||
children: GeneralNode<T>[]
|
children: GeneralNode<T>[];
|
||||||
}
|
};
|
||||||
|
|
|
||||||
13
src/graph.ts
Normal file
13
src/graph.ts
Normal 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 });
|
||||||
|
}
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
"baseUrl": "src",
|
"baseUrl": "src",
|
||||||
"paths": {
|
"paths": {
|
||||||
"@code/*": [
|
"@code/*": [
|
||||||
"day3/*"
|
"day5/*"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -22,4 +22,4 @@
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"node_modules"
|
"node_modules"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1879,6 +1879,11 @@ pkg-dir@^4.2.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
find-up "^4.0.0"
|
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:
|
pretty-format@^28.0.0, pretty-format@^28.1.1:
|
||||||
version "28.1.1"
|
version "28.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-28.1.1.tgz#f731530394e0f7fcd95aba6b43c50e02d86b95cb"
|
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-28.1.1.tgz#f731530394e0f7fcd95aba6b43c50e02d86b95cb"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue