Skip to content

Commit

Permalink
Merge pull request #5 from AdiletBaimyrza/all/test-and-fix
Browse files Browse the repository at this point in the history
All/test and fix
  • Loading branch information
andrii-venher authored Nov 13, 2023
2 parents a85377c + 46e8e13 commit bf72b9d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
11 changes: 10 additions & 1 deletion dijkstra-prim-visualization/src/algorithms/graph.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
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);
};

const createGraph = function (nodes, edges) {
this.nodes = nodes;
this.edges = new Array(edges.length);
Expand Down Expand Up @@ -38,4 +47,4 @@ const buildAdjacencyList = (graph) => {
return adjacencyList;
};

export { createGraph, buildAdjacencyList };
export { createGraphFromComponent, buildAdjacencyList };
2 changes: 1 addition & 1 deletion dijkstra-prim-visualization/src/algorithms/mstPrim.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ const computeMst = () => {
console.log("------- MST PRIM END -------");
};

export default computeMst;
export default prim;
9 changes: 5 additions & 4 deletions dijkstra-prim-visualization/src/algorithms/spDijkstra.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { createGraph, buildAdjacencyList } from "./graph.js";
import createMinHeap from "./minHeap.js";

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

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

const fringe = new createMinHeap(nodesCount);
const keys = new Array(nodesCount);
const parents = new Array(nodesCount);
Expand Down Expand Up @@ -90,6 +93,4 @@ const computeSp = () => {
console.log("------- SP DIJKSTRA END -------");
};

computeSp();

export default computeSp;
export default dijkstra;
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default Node;
// Ensures that the required props are passed and have the correct type
Node.propTypes = {
onNodeClick: PropTypes.func.isRequired,
id: PropTypes.string.isRequired,
id: PropTypes.number.isRequired,
cx: PropTypes.number.isRequired,
cy: PropTypes.number.isRequired,
};
18 changes: 18 additions & 0 deletions dijkstra-prim-visualization/src/components/Navbar/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { useContext } from "react";
import { GraphParamsContext } from "../../GraphParamsContext";
import { createGraphFromComponent } from "../../algorithms/graph";
import prim from "../../algorithms/mstPrim";
import dijkstra from "../../algorithms/spDijkstra";

/**
* Navbar component displays buttons to print nodes and edges.
* @returns {JSX.Element} Navbar component
*/
const Navbar = () => {
const { nodes, edges } = useContext(GraphParamsContext);

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

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

return (
<>
<button onClick={runPrim}>Run prim's algorithm</button>
<button onClick={runDijkstra}>Run dijkstra's algorithm</button>
<button
onClick={() => {
console.log(nodes);
Expand Down
3 changes: 0 additions & 3 deletions dijkstra-prim-visualization/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import "./index.css";
import computeMst from "./algorithms/mstPrim.js";

ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<App />
</React.StrictMode>,
);

computeMst();

0 comments on commit bf72b9d

Please sign in to comment.