|
1 | 1 | import React, {useState, useEffect} from 'react'; |
2 | 2 | import { GraphCanvas } from 'reagraph'; |
3 | 3 | import GenerateRandomGraph from '../algorithms/graphs/GenerateRandomGraph'; |
| 4 | +import BreadthFirstSearch from '../algorithms/graphs/BreadthFirstSearch'; |
| 5 | +import DepthFirstSearch from '../algorithms/graphs/DepthFirstSearch'; |
| 6 | +import { Node } from '../algorithms/utils/GraphUtils'; |
| 7 | +import PopulateGraphMap from '../algorithms/graphs/PopulateGraphMap'; |
4 | 8 | import '../styles/graph.css'; |
| 9 | +import { GraphAlgorithm } from '../utils/GraphEnum'; |
5 | 10 |
|
6 | 11 | const GraphVisualiser: React.FC = () => { |
7 | | - const [edges, _] = useState([]); |
| 12 | + const [edges, setEdges] = useState([]); |
8 | 13 | const [nodes, setNodes] = useState<{ id: string }[]>([]); // Specify the type of the nodes state variable |
| 14 | + const [algorithm, setAlgorithm] = useState(GraphAlgorithm.BFS); |
| 15 | + |
| 16 | + const nodeMap: Node[] = [ |
| 17 | + new Node('0', '0', 'slateblue'), |
| 18 | + new Node('1', '1', 'slateblue'), |
| 19 | + new Node('2', '2', 'slateblue'), |
| 20 | + new Node('3', '3', 'slateblue'), |
| 21 | + new Node('4', '4', 'slateblue'), |
| 22 | + new Node('5', '5', 'slateblue'), |
| 23 | + new Node('6', '6', 'slateblue'), |
| 24 | + new Node('7', '7', 'slateblue'), |
| 25 | + new Node('8', '8', 'slateblue'), |
| 26 | + new Node('9', '9', 'slateblue'), |
| 27 | + ]; |
| 28 | + |
| 29 | + nodeMap[0].children.push(nodeMap[1]); |
| 30 | + nodeMap[1].children.push(nodeMap[2]); |
| 31 | + nodeMap[2].children.push(nodeMap[3]); |
| 32 | + nodeMap[3].children.push(nodeMap[4]); |
| 33 | + nodeMap[3].children.push(nodeMap[7]); |
| 34 | + nodeMap[4].children.push(nodeMap[8]); |
| 35 | + nodeMap[4].children.push(nodeMap[5]); |
| 36 | + nodeMap[4].children.push(nodeMap[6]); |
| 37 | + nodeMap[1].children.push(nodeMap[0]); |
| 38 | + nodeMap[5].children.push(nodeMap[9]); |
9 | 39 |
|
10 | 40 | useEffect(() => { |
11 | | - GenerateRandomGraph(10, setNodes); |
| 41 | + PopulateGraphMap(nodeMap, setNodes, setEdges); |
12 | 42 | }, []); |
13 | 43 |
|
| 44 | + |
14 | 45 | return ( |
15 | 46 | <div className="graph-canvas"> |
16 | | - <GraphCanvas |
17 | | - nodes={nodes} |
18 | | - edges={edges} |
19 | | - /> |
| 47 | + <div className="graph-canvas-control-panel"> |
| 48 | + <h4>Double Click Node to run algorithm.</h4> |
| 49 | + <select className="graph-canvas-control" onChange={(event) => {setAlgorithm(event.target.value as GraphAlgorithm)}}> |
| 50 | + <option value={GraphAlgorithm.DFS}>Depth First Search</option> |
| 51 | + <option value={GraphAlgorithm.BFS}>Breadth First Search</option> |
| 52 | + </select> |
| 53 | + </div> |
| 54 | + <div className="graph-canvas-art"> |
| 55 | + <GraphCanvas |
| 56 | + nodes={nodes} |
| 57 | + edges={edges} |
| 58 | + onNodeDoubleClick={node => DepthFirstSearch(nodeMap, setNodes, node.id)} |
| 59 | + /> |
| 60 | + </div> |
20 | 61 | </div> |
21 | 62 | ); |
22 | 63 | }; |
|
0 commit comments