Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All/test and fix #5

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();