Skip to content

Commit

Permalink
Integrate algorithms with navbar
Browse files Browse the repository at this point in the history
Integrate algorithms with navbar
  • Loading branch information
mitrich2004 committed Nov 13, 2023
1 parent aa3f07d commit 46e8e13
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 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,
};
10 changes: 7 additions & 3 deletions dijkstra-prim-visualization/src/components/Navbar/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext } from "react";
import { GraphParamsContext } from "../../GraphParamsContext";
import { createGraph } from "../../algorithms/graph";
import { createGraphFromComponent } from "../../algorithms/graph";
import prim from "../../algorithms/mstPrim";
import dijkstra from "../../algorithms/spDijkstra";

Expand All @@ -12,11 +12,15 @@ const Navbar = () => {
const { nodes, edges } = useContext(GraphParamsContext);

const runPrim = () => {
// you can run your code there
const graph = createGraphFromComponent(nodes, edges);
const result = prim(graph);
console.log(result);
};

const runDijkstra = () => {
// you can run your code there
const graph = createGraphFromComponent(nodes, edges);
const result = dijkstra(graph);
console.log(result);
};

return (
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 46e8e13

Please sign in to comment.