From 965d15d4545dd731f080db49144493223bd43b61 Mon Sep 17 00:00:00 2001 From: Sergey Miachkov Date: Wed, 15 Nov 2023 18:13:12 +0100 Subject: [PATCH] Implemented a linked list Implemented a linked list --- .../src/algorithms/linkedList.js | 72 +++++++++++++++++++ .../src/algorithms/mstPrim.js | 5 +- .../src/algorithms/spDijkstra.js | 7 +- 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 dijkstra-prim-visualization/src/algorithms/linkedList.js diff --git a/dijkstra-prim-visualization/src/algorithms/linkedList.js b/dijkstra-prim-visualization/src/algorithms/linkedList.js new file mode 100644 index 0000000..b4b6023 --- /dev/null +++ b/dijkstra-prim-visualization/src/algorithms/linkedList.js @@ -0,0 +1,72 @@ +const createLinkedList = function (capacity) { + const createNode = (key, value, next = null) => { + const node = { + key: key, + value: value, + next: next, + }; + + return node; + }; + + let head = null; + let currentSize = 0; + + this.isEmpty = () => currentSize == 0; + + this.insert = (key, value) => { + currentSize++; + const newNode = createNode(key, value); + + if (head == null) { + head = newNode; + } else { + let tmp = head; + + while (tmp.next != null) { + tmp = tmp.next; + } + + tmp.next = newNode; + } + }; + + this.extractMin = () => { + let minValue = Infinity; + let minNode = null; + let tmp = createNode(-1, -1, head); + + //find the min node + while (tmp.next) { + if (tmp.next.value < minValue) { + minValue = tmp.next.value; + minNode = tmp; + } + + tmp = tmp.next; + } + tmp = minNode.next; + + //delete the min node + if (minNode.next == head) { + head = head.next; + } else { + minNode.next = minNode.next.next; + } + --currentSize; + + return tmp; + }; + + this.decreaseKey = (key, newValue) => { + let tmp = createNode(-1, -1, head); + + while (tmp.next.key != key) { + tmp = tmp.next; + } + + tmp.next.value = newValue; + }; +}; + +export default createLinkedList; diff --git a/dijkstra-prim-visualization/src/algorithms/mstPrim.js b/dijkstra-prim-visualization/src/algorithms/mstPrim.js index 7f3daa3..9c54294 100644 --- a/dijkstra-prim-visualization/src/algorithms/mstPrim.js +++ b/dijkstra-prim-visualization/src/algorithms/mstPrim.js @@ -1,12 +1,13 @@ -import { createGraph, buildAdjacencyList } from "./graph"; +import { createGraphFromComponent, buildAdjacencyList } from "./graph"; import createMinHeap from "./minHeap"; +import createLinkedList from "./linkedList"; const prim = (graph) => { const adjacencyList = buildAdjacencyList(graph); const nodesCount = adjacencyList.length; - const fringe = new createMinHeap(nodesCount); + const fringe = new createLinkedList(nodesCount); const isInHeap = new Array(nodesCount); const results = new Array(nodesCount); const keys = new Array(nodesCount); diff --git a/dijkstra-prim-visualization/src/algorithms/spDijkstra.js b/dijkstra-prim-visualization/src/algorithms/spDijkstra.js index a524b77..5cfb709 100644 --- a/dijkstra-prim-visualization/src/algorithms/spDijkstra.js +++ b/dijkstra-prim-visualization/src/algorithms/spDijkstra.js @@ -1,5 +1,6 @@ -import { createGraph, buildAdjacencyList } from "./graph.js"; +import { createGraphFromComponent, buildAdjacencyList } from "./graph.js"; import createMinHeap from "./minHeap.js"; +import createLinkedList from "./linkedList.js"; const dijkstra = (graph) => { const adjacencyList = buildAdjacencyList(graph); @@ -8,7 +9,7 @@ const dijkstra = (graph) => { const startNode = 0; const finishNode = nodesCount - 1; - const fringe = new createMinHeap(nodesCount); + const fringe = new createLinkedList(nodesCount); const keys = new Array(nodesCount); const parents = new Array(nodesCount); @@ -24,7 +25,7 @@ const dijkstra = (graph) => { const node = fringe.extractMin(); const neighbours = adjacencyList[node.key]; - if (node == finishNode) { + if (node.key == finishNode) { break; }