From 8b5cbd2e9156bde770a130c436c62e65c79b12a6 Mon Sep 17 00:00:00 2001 From: mpaulson Date: Sun, 26 Jun 2022 08:09:22 -0600 Subject: [PATCH] feat: prims algorithm --- .jest.config.json | 2 +- README.md | 2 +- package.json | 16 +++---- prettier.config.js | 10 +++++ src/__tests__/ArrayList.ts | 5 +-- src/__tests__/DijkstraList.ts | 52 +++++++++++++++-------- src/__tests__/DoublyLinkedList.ts | 5 +-- src/__tests__/InsertionSort.ts | 5 +-- src/__tests__/ListTest.ts | 1 - src/__tests__/MergeSort.ts | 4 +- src/__tests__/PrimsAlgorithm.ts | 69 +++++++++++++++++++++++++++++++ src/__tests__/Queue.ts | 6 +-- src/__tests__/QuickSort.ts | 5 +-- src/__tests__/SinglyLinkedList.ts | 4 +- src/__tests__/Stack.ts | 5 +-- src/global.d.ts | 9 ++-- src/graph.ts | 13 ++++++ tsconfig.json | 4 +- yarn.lock | 5 +++ 19 files changed, 160 insertions(+), 62 deletions(-) create mode 100644 prettier.config.js create mode 100644 src/__tests__/PrimsAlgorithm.ts create mode 100644 src/graph.ts diff --git a/.jest.config.json b/.jest.config.json index 9ad17ab..14b7c3c 100644 --- a/.jest.config.json +++ b/.jest.config.json @@ -2,7 +2,7 @@ "clearMocks": true, "moduleNameMapper": { "@code/(.*)": [ - "/src/day3/$1" + "/src/day5/$1" ] }, "preset": "ts-jest" diff --git a/README.md b/README.md index 2e7a679..7911315 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Literal Improvement Gaining Master and Tutelage on Algorithms Let's Intelligently Generate Multiple Algorithm Training Assessments // permdaddy ### Sugma Nuts -Studious Users Get Major Abilities. New Useful Training for Students +Studious Users Get Major Abilities. New Useful Training for Students ### Ligma Fart Learn Intermediate Groundbreaking Massive Algorithms. Free Algorithm Research & Training System diff --git a/package.json b/package.json index c3fae46..a7685a9 100644 --- a/package.json +++ b/package.json @@ -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 } } diff --git a/prettier.config.js b/prettier.config.js new file mode 100644 index 0000000..d00c0d1 --- /dev/null +++ b/prettier.config.js @@ -0,0 +1,10 @@ +module.exports = { + tabWidth: 4, + printWidth: 80, + proseWrap: "never", + trailingComma: "all", + singleQuote: false, + semi: true, +}; + + diff --git a/src/__tests__/ArrayList.ts b/src/__tests__/ArrayList.ts index 40de2da..4f1f3d6 100644 --- a/src/__tests__/ArrayList.ts +++ b/src/__tests__/ArrayList.ts @@ -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(3); test_list(list); }); - - - diff --git a/src/__tests__/DijkstraList.ts b/src/__tests__/DijkstraList.ts index 696b10b..fa5587f 100644 --- a/src/__tests__/DijkstraList.ts +++ b/src/__tests__/DijkstraList.ts @@ -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]); }); - diff --git a/src/__tests__/DoublyLinkedList.ts b/src/__tests__/DoublyLinkedList.ts index b696784..f7429b3 100644 --- a/src/__tests__/DoublyLinkedList.ts +++ b/src/__tests__/DoublyLinkedList.ts @@ -1,10 +1,7 @@ import LinkedList from "@code/DoublyLinkedList"; import { test_list } from "./ListTest"; -test("DoublyLinkedList", function() { +test("DoublyLinkedList", function () { const list = new LinkedList(3); test_list(list); }); - - - diff --git a/src/__tests__/InsertionSort.ts b/src/__tests__/InsertionSort.ts index b52c2e5..76273ce 100644 --- a/src/__tests__/InsertionSort.ts +++ b/src/__tests__/InsertionSort.ts @@ -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]); }); - diff --git a/src/__tests__/ListTest.ts b/src/__tests__/ListTest.ts index de20a84..2169e75 100644 --- a/src/__tests__/ListTest.ts +++ b/src/__tests__/ListTest.ts @@ -13,5 +13,4 @@ export function test_list(list: List): void { expect(list.removeAt(0)).toEqual(5); expect(list.removeAt(0)).toEqual(11); expect(list.length).toEqual(0); - } diff --git a/src/__tests__/MergeSort.ts b/src/__tests__/MergeSort.ts index 7b76929..0ffcdb1 100644 --- a/src/__tests__/MergeSort.ts +++ b/src/__tests__/MergeSort.ts @@ -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]); }); - - diff --git a/src/__tests__/PrimsAlgorithm.ts b/src/__tests__/PrimsAlgorithm.ts new file mode 100644 index 0000000..f1a874f --- /dev/null +++ b/src/__tests__/PrimsAlgorithm.ts @@ -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 }, + ], + ]); +}); diff --git a/src/__tests__/Queue.ts b/src/__tests__/Queue.ts index e28bf7b..4fb11c1 100644 --- a/src/__tests__/Queue.ts +++ b/src/__tests__/Queue.ts @@ -1,6 +1,6 @@ import Queue from "@code/Queue"; -test("queue", function() { +test("queue", function () { const list = new Queue(); 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); }); - - diff --git a/src/__tests__/QuickSort.ts b/src/__tests__/QuickSort.ts index e6c62e1..ca777ec 100644 --- a/src/__tests__/QuickSort.ts +++ b/src/__tests__/QuickSort.ts @@ -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]); }); - - - diff --git a/src/__tests__/SinglyLinkedList.ts b/src/__tests__/SinglyLinkedList.ts index 99e565c..762419a 100644 --- a/src/__tests__/SinglyLinkedList.ts +++ b/src/__tests__/SinglyLinkedList.ts @@ -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(3); test_list(list); }); - - diff --git a/src/__tests__/Stack.ts b/src/__tests__/Stack.ts index b478b68..9e74a4e 100644 --- a/src/__tests__/Stack.ts +++ b/src/__tests__/Stack.ts @@ -1,6 +1,6 @@ import Stack from "@code/Stack"; -test("stack", function() { +test("stack", function () { const list = new Stack(); list.push(5); @@ -25,6 +25,3 @@ test("stack", function() { //yayaya }); - - - diff --git a/src/global.d.ts b/src/global.d.ts index 4d5f63a..b22f2f7 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -6,7 +6,8 @@ declare interface List { 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 = { value: T; left: BinaryNode; right: BinaryNode; -} +}; declare type GeneralNode = { value: T; - children: GeneralNode[] -} + children: GeneralNode[]; +}; diff --git a/src/graph.ts b/src/graph.ts new file mode 100644 index 0000000..8b783e6 --- /dev/null +++ b/src/graph.ts @@ -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 }); +} diff --git a/tsconfig.json b/tsconfig.json index 56dd8c6..b689ceb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,7 @@ "baseUrl": "src", "paths": { "@code/*": [ - "day3/*" + "day5/*" ] } }, @@ -22,4 +22,4 @@ "exclude": [ "node_modules" ] -} \ No newline at end of file +} diff --git a/yarn.lock b/yarn.lock index af2279e..9cf63bb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"