Skip to content

Commit

Permalink
Merge pull request #7 from AdiletBaimyrza/andrii/measurement
Browse files Browse the repository at this point in the history
Andrii/measurement
  • Loading branch information
andrii-venher authored Nov 19, 2023
2 parents 5c53874 + 3654c52 commit 81b1a23
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { createGraphFromComponent, buildAdjacencyList } from "./graph.js";
import {
buildAdjacencyList,
buildAdjacencyListFromComponent,
} from "./graph.js";
import createMinHeap from "./minHeap.js";
import createLinkedList from "./linkedList.js";

const dijkstra = (graph) => {
const adjacencyList = buildAdjacencyList(graph);
const dijkstra = (adjacencyList, createFringe) => {
const nodesCount = adjacencyList.length;

const startNode = 0;
const finishNode = nodesCount - 1;

const fringe = new createLinkedList(nodesCount);
const fringe = new createFringe(nodesCount);
const keys = new Array(nodesCount);
const parents = new Array(nodesCount);

Expand Down Expand Up @@ -39,11 +40,11 @@ const dijkstra = (graph) => {
});
}

const sp = new Array();
const steps = new Array();
let node = finishNode;

while (node != startNode) {
sp.push({
steps.push({
from: parents[node].key,
to: node,
weight: parents[node].weight,
Expand All @@ -52,11 +53,24 @@ const dijkstra = (graph) => {
}

return {
steps: sp.reverse(),
spTotalWeight: keys[finishNode],
steps: steps,
total: keys[finishNode],
};
};

const transformResult = (result) => {
return {
steps: result.steps.reverse(),
total: result.total,
};
};

const dijkstraWrapper = (nodes, edges) => {
const adjacencyList = buildAdjacencyListFromComponent(nodes, edges);
const result = dijkstra(adjacencyList, createMinHeap);
return transformResult(result);
};

const displayDijkstraResult = (result) => {
console.log("SP steps:");

Expand Down Expand Up @@ -87,11 +101,14 @@ const computeSp = () => {
],
);

const result = dijkstra(graph, 0, 5);
const adjacencyList = buildAdjacencyList(graph);

const result = dijkstra(adjacencyList, createMinHeap);
const transformedResult = transformResult(result);

displayDijkstraResult(result);
displayDijkstraResult(transformedResult);

console.log("------- SP DIJKSTRA END -------");
};

export default dijkstra;
export { dijkstra, dijkstraWrapper };
63 changes: 47 additions & 16 deletions dijkstra-prim-visualization/src/algorithms/graph.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
const createGraphFromComponent = function (nodes, edges) {
const mappedNodes = nodes.map((node) => node.id);
const mappedEdges = edges.map((edge) => {
return [edge.firstNode.id, edge.secondNode.id, edge.weight];
});
// const createGraphFromComponent = function (nodes, edges) {
// const mappedNodes = nodes.map((node) => node.id);
// const mappedEdges = edges.map((edge) => {
// return [edge.firstNode.id, edge.secondNode.id, edge.weight];
// });

// return new createGraph(mappedNodes, mappedEdges);
// };

return new createGraph(mappedNodes, mappedEdges);
const createAdjacencyListEntry = (node, weight) => {
const entry = {
node: node,
weight: weight,
};
return entry;
};

const createGraph = function (nodes, edges) {
const createGraph = (nodes, edges) => {
this.nodes = nodes;
this.edges = new Array(edges.length);

Expand All @@ -24,14 +32,6 @@ const createGraph = function (nodes, edges) {
const buildAdjacencyList = (graph) => {
const adjacencyList = new Array(graph.nodes.length).fill(null);

const createAdjacencyListEntry = (node, weight) => {
const entry = {
node: node,
weight: weight,
};
return entry;
};

graph.edges.forEach((e) => {
if (!Array.isArray(adjacencyList[e.from])) {
adjacencyList[e.from] = new Array();
Expand All @@ -47,4 +47,35 @@ const buildAdjacencyList = (graph) => {
return adjacencyList;
};

export { createGraphFromComponent, buildAdjacencyList };
const buildAdjacencyListFromComponent = (nodes, edges) => {
const adjacencyList = new Array(nodes.length).fill(null);

edges
.map((e) => {
return {
from: e.firstNode.id,
to: e.secondNode.id,
weight: e.weight,
};
})
.forEach((e) => {
if (!Array.isArray(adjacencyList[e.from])) {
adjacencyList[e.from] = new Array();
}
adjacencyList[e.from].push(createAdjacencyListEntry(e.to, e.weight));

if (!Array.isArray(adjacencyList[e.to])) {
adjacencyList[e.to] = new Array();
}
adjacencyList[e.to].push(createAdjacencyListEntry(e.from, e.weight));
});

return adjacencyList;
};

export {
createAdjacencyListEntry,
createGraph,
buildAdjacencyList,
buildAdjacencyListFromComponent,
};
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import { createGraphFromComponent, buildAdjacencyList } from "./graph";
import createMinHeap from "./minHeap";
import createLinkedList from "./linkedList";

const prim = (graph) => {
const adjacencyList = buildAdjacencyList(graph);
import {
buildAdjacencyList,
buildAdjacencyListFromComponent,
} from "./graph.js";
import createMinHeap from "./minHeap.js";

const prim = (adjacencyList, createFringe) => {
const nodesCount = adjacencyList.length;

const fringe = new createLinkedList(nodesCount);
const isInHeap = new Array(nodesCount);
const results = new Array(nodesCount);
const fringe = new createFringe(nodesCount);
const isInFringe = new Array(nodesCount);
const steps = new Array(nodesCount);
const keys = new Array(nodesCount);

// Insert node 0 with value 0
fringe.insert(0, 0);
isInHeap[0] = true;
isInFringe[0] = true;
keys[0] = Infinity;
results[0] = {
steps[0] = {
parent: -1,
weight: null,
};

for (let index = 1; index < nodesCount; index++) {
isInHeap[index] = true;
isInFringe[index] = true;
keys[index] = Infinity;
fringe.insert(index, Infinity);
}

while (!fringe.isEmpty()) {
const extractedNode = fringe.extractMin();
isInHeap[extractedNode.key] = false;
isInFringe[extractedNode.key] = false;

const neightbours = adjacencyList[extractedNode.key];
neightbours.forEach((n) => {
if (isInHeap[n.node]) {
if (isInFringe[n.node]) {
if (keys[n.node] > n.weight) {
fringe.decreaseKey(n.node, n.weight);
keys[n.node] = n.weight;
results[n.node] = {
steps[n.node] = {
from: extractedNode.key,
to: n.node,
weight: n.weight,
Expand All @@ -47,24 +47,35 @@ const prim = (graph) => {
});
}

results.sort((a, b) => a.weight - b.weight);
return steps;
};

let mstTotal = 0;
const transformSteps = (steps) => {
steps.shift(1);
steps.sort((a, b) => a.weight - b.weight);

for (let index = 1; index < nodesCount; index++) {
mstTotal += results[index].weight;
let total = 0;
for (let i = 0; i < steps.length; i++) {
total += steps[i].weight;
}

return {
steps: results,
mstTotalWeight: mstTotal,
steps: steps,
total: total,
};
};

const primWrapper = (nodes, edges) => {
const adjacencyList = buildAdjacencyListFromComponent(nodes, edges);
const steps = prim(adjacencyList, createMinHeap);

return transformSteps(steps);
};

const displayPrimResult = (result) => {
console.log("MST steps:");

for (let index = 1; index < result.steps.length; index++) {
for (let index = 0; index < result.steps.length; index++) {
console.log(
`From ${result.steps[index].from} to ${result.steps[index].to} with weight ${result.steps[index].weight}`,
);
Expand All @@ -89,11 +100,14 @@ const computeMst = () => {
],
);

const result = prim(graph);
const adjacencyList = buildAdjacencyList(graph);

const steps = prim(adjacencyList);
const transformedSteps = transformSteps(steps);

displayPrimResult(result);
displayPrimResult(transformedSteps);

console.log("------- MST PRIM END -------");
};

export default prim;
export { prim, primWrapper };
11 changes: 4 additions & 7 deletions dijkstra-prim-visualization/src/components/Navbar/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useContext } from "react";
import { GraphParamsContext } from "../../GraphParamsContext";
import { createGraphFromComponent } from "../../algorithms/graph";
import prim from "../../algorithms/mstPrim";
import dijkstra from "../../algorithms/spDijkstra";
import { primWrapper } from "../../algorithms/prim";
import { dijkstraWrapper } from "../../algorithms/dijkstra";

/**
* Navbar component displays buttons to print nodes and edges.
Expand All @@ -12,14 +11,12 @@ const Navbar = () => {
const { nodes, edges } = useContext(GraphParamsContext);

const runPrim = () => {
const graph = createGraphFromComponent(nodes, edges);
const result = prim(graph);
const result = primWrapper(nodes, edges);
console.log(result);
};

const runDijkstra = () => {
const graph = createGraphFromComponent(nodes, edges);
const result = dijkstra(graph);
const result = dijkstraWrapper(nodes, edges);
console.log(result);
};

Expand Down
Loading

0 comments on commit 81b1a23

Please sign in to comment.