diff --git a/dijkstra-prim-visualization/src/algorithms/graph.js b/dijkstra-prim-visualization/src/algorithms/graph.js index 23568b8..6d9780c 100644 --- a/dijkstra-prim-visualization/src/algorithms/graph.js +++ b/dijkstra-prim-visualization/src/algorithms/graph.js @@ -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); @@ -38,4 +47,4 @@ const buildAdjacencyList = (graph) => { return adjacencyList; }; -export { createGraph, buildAdjacencyList }; +export { createGraphFromComponent, buildAdjacencyList }; diff --git a/dijkstra-prim-visualization/src/algorithms/mstPrim.js b/dijkstra-prim-visualization/src/algorithms/mstPrim.js index ca44f02..7f3daa3 100644 --- a/dijkstra-prim-visualization/src/algorithms/mstPrim.js +++ b/dijkstra-prim-visualization/src/algorithms/mstPrim.js @@ -95,4 +95,4 @@ const computeMst = () => { console.log("------- MST PRIM END -------"); }; -export default computeMst; +export default prim; diff --git a/dijkstra-prim-visualization/src/algorithms/spDijkstra.js b/dijkstra-prim-visualization/src/algorithms/spDijkstra.js index 1f8b5c4..a524b77 100644 --- a/dijkstra-prim-visualization/src/algorithms/spDijkstra.js +++ b/dijkstra-prim-visualization/src/algorithms/spDijkstra.js @@ -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); @@ -90,6 +93,4 @@ const computeSp = () => { console.log("------- SP DIJKSTRA END -------"); }; -computeSp(); - -export default computeSp; +export default dijkstra; diff --git a/dijkstra-prim-visualization/src/components/Canvas/Nodes/Node/Node.jsx b/dijkstra-prim-visualization/src/components/Canvas/Nodes/Node/Node.jsx index 3347b3f..5e3c746 100644 --- a/dijkstra-prim-visualization/src/components/Canvas/Nodes/Node/Node.jsx +++ b/dijkstra-prim-visualization/src/components/Canvas/Nodes/Node/Node.jsx @@ -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, }; diff --git a/dijkstra-prim-visualization/src/components/Navbar/Navbar.jsx b/dijkstra-prim-visualization/src/components/Navbar/Navbar.jsx index 6517ecf..b6e26c1 100644 --- a/dijkstra-prim-visualization/src/components/Navbar/Navbar.jsx +++ b/dijkstra-prim-visualization/src/components/Navbar/Navbar.jsx @@ -1,5 +1,8 @@ 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. @@ -7,8 +10,23 @@ import { GraphParamsContext } from "../../GraphParamsContext"; */ 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 ( <> + +