|
| 1 | +import React, { useState, useMemo, useEffect, useRef } from "react"; |
| 2 | +import { knapsackTopDown, knapsackBottomUp } from "../../algorithms/dynamic-programming/knapsackAlgo"; |
| 3 | + |
| 4 | +// Simple DP grid renderer for 2D dp/memo tables |
| 5 | +const DPGrid = ({ array, currentIndex }) => { |
| 6 | + if (!Array.isArray(array) || array.length === 0) return null; |
| 7 | + |
| 8 | + const rows = array.length; |
| 9 | + const cols = array[0].length; |
| 10 | + |
| 11 | + return ( |
| 12 | + <div className="mt-4 overflow-auto"> |
| 13 | + <h3 className="text-xl font-semibold mb-3 text-blue-400">DP Table</h3> |
| 14 | + <div className="inline-block border rounded-lg bg-gray-800 p-3"> |
| 15 | + <table className="table-auto border-collapse"> |
| 16 | + <thead> |
| 17 | + <tr> |
| 18 | + <th className="p-2 border text-sm text-gray-300">i \ w</th> |
| 19 | + {Array.from({ length: cols }).map((_, c) => ( |
| 20 | + <th key={c} className="p-2 border text-sm text-gray-300">{c}</th> |
| 21 | + ))} |
| 22 | + </tr> |
| 23 | + </thead> |
| 24 | + <tbody> |
| 25 | + {array.map((row, r) => ( |
| 26 | + <tr key={r} className="text-center"> |
| 27 | + <td className="p-2 border text-sm text-gray-300">{r}</td> |
| 28 | + {row.map((cell, c) => { |
| 29 | + const isActive = currentIndex && currentIndex.i === r && currentIndex.w === c; |
| 30 | + return ( |
| 31 | + <td |
| 32 | + key={c} |
| 33 | + className={`p-3 border w-20 h-12 align-middle ${isActive ? "bg-blue-600 text-white font-bold scale-105 transform" : "bg-gray-700 text-gray-200"}`} |
| 34 | + > |
| 35 | + {cell === null ? "?" : cell} |
| 36 | + </td> |
| 37 | + ); |
| 38 | + })} |
| 39 | + </tr> |
| 40 | + ))} |
| 41 | + </tbody> |
| 42 | + </table> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + ); |
| 46 | +}; |
| 47 | + |
| 48 | +const SPEED_OPTIONS = { |
| 49 | + "Slow": 1500, |
| 50 | + "Medium": 500, |
| 51 | + "Fast": 200, |
| 52 | +}; |
| 53 | + |
| 54 | +export default function KnapsackVisualizer() { |
| 55 | + const [weightsInput, setWeightsInput] = useState("3,4,2"); |
| 56 | + const [valuesInput, setValuesInput] = useState("4,5,3"); |
| 57 | + const [capacity, setCapacity] = useState(6); |
| 58 | + const [algorithm, setAlgorithm] = useState("topDown"); |
| 59 | + const [steps, setSteps] = useState([]); |
| 60 | + const [currentStep, setCurrentStep] = useState(0); |
| 61 | + const [isPlaying, setIsPlaying] = useState(false); |
| 62 | + const [speed, setSpeed] = useState(SPEED_OPTIONS["Medium"]); |
| 63 | + const timerRef = useRef(null); |
| 64 | + |
| 65 | + // Parse inputs |
| 66 | + const weights = useMemo(() => weightsInput.split(",").map(s => Number(s.trim())).filter(v => !Number.isNaN(v)), [weightsInput]); |
| 67 | + const values = useMemo(() => valuesInput.split(",").map(s => Number(s.trim())).filter(v => !Number.isNaN(v)), [valuesInput]); |
| 68 | + |
| 69 | + const handleCompute = () => { |
| 70 | + if (weights.length === 0 || values.length === 0) { |
| 71 | + alert("Please provide weights and values."); |
| 72 | + return; |
| 73 | + } |
| 74 | + if (weights.length !== values.length) { |
| 75 | + alert("Weights and values arrays must have the same length."); |
| 76 | + return; |
| 77 | + } |
| 78 | + if (capacity < 0 || capacity > 200) { |
| 79 | + alert("Please enter capacity between 0 and 200 for visualization."); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + setIsPlaying(false); |
| 84 | + |
| 85 | + const { steps: newSteps, result } = algorithm === "topDown" |
| 86 | + ? knapsackTopDown(weights, values, capacity) |
| 87 | + : knapsackBottomUp(weights, values, capacity); |
| 88 | + |
| 89 | + setSteps(newSteps); |
| 90 | + setCurrentStep(0); |
| 91 | + }; |
| 92 | + |
| 93 | + useEffect(() => { |
| 94 | + handleCompute(); |
| 95 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 96 | + }, [algorithm]); |
| 97 | + |
| 98 | + useEffect(() => { |
| 99 | + if (isPlaying && currentStep < steps.length - 1) { |
| 100 | + timerRef.current = setInterval(() => { |
| 101 | + setCurrentStep(prev => prev + 1); |
| 102 | + }, speed); |
| 103 | + } else if (currentStep === steps.length - 1) { |
| 104 | + setIsPlaying(false); |
| 105 | + } |
| 106 | + |
| 107 | + return () => clearInterval(timerRef.current); |
| 108 | + }, [isPlaying, currentStep, steps.length, speed]); |
| 109 | + |
| 110 | + const togglePlay = () => { |
| 111 | + if (currentStep === steps.length - 1) { |
| 112 | + setCurrentStep(0); |
| 113 | + setIsPlaying(true); |
| 114 | + } else setIsPlaying(!isPlaying); |
| 115 | + }; |
| 116 | + |
| 117 | + const handleNext = () => { |
| 118 | + setIsPlaying(false); |
| 119 | + if (currentStep < steps.length - 1) setCurrentStep(currentStep + 1); |
| 120 | + }; |
| 121 | + |
| 122 | + const handlePrev = () => { |
| 123 | + setIsPlaying(false); |
| 124 | + if (currentStep > 0) setCurrentStep(currentStep - 1); |
| 125 | + }; |
| 126 | + |
| 127 | + const currentState = useMemo(() => steps[currentStep] || {}, [steps, currentStep]); |
| 128 | + const isFinalStep = steps.length > 0 && currentStep === steps.length - 1; |
| 129 | + |
| 130 | + const finalResult = isFinalStep && currentState.array ? ( |
| 131 | + // For bottom-up dp, result is dp[n][W] (last row, last col). For top-down memo, we may need to compute same. |
| 132 | + (() => { |
| 133 | + const arr = currentState.array; |
| 134 | + if (!Array.isArray(arr)) return null; |
| 135 | + const lastRow = arr[arr.length - 1]; |
| 136 | + return Array.isArray(lastRow) ? lastRow[lastRow.length - 1] : null; |
| 137 | + })() |
| 138 | + ) : null; |
| 139 | + |
| 140 | + return ( |
| 141 | + <div className="p-6 min-h-screen bg-gray-900 text-gray-100 font-sans"> |
| 142 | + <div className="max-w-5xl mx-auto"> |
| 143 | + <h1 className="text-4xl font-extrabold mb-8 text-indigo-400 text-center drop-shadow-lg">Knapsack Variants</h1> |
| 144 | + |
| 145 | + <div className="mb-8 p-4 rounded-xl bg-gray-800 border border-gray-700 shadow-inner group"> |
| 146 | + <p className="text-gray-300">This visualizer supports 0/1 Knapsack (Top-Down memoization and Bottom-Up tabulation). Provide comma-separated arrays for weights and values, and a capacity (W).</p> |
| 147 | + </div> |
| 148 | + |
| 149 | + <div className="flex flex-wrap justify-center items-center gap-5 mb-8 p-6 rounded-xl bg-gray-800 shadow-2xl border border-gray-700"> |
| 150 | + <div className="w-full md:w-1/3"> |
| 151 | + <label className="text-gray-300">Weights (comma separated):</label> |
| 152 | + <input value={weightsInput} onChange={(e) => setWeightsInput(e.target.value)} className="w-full mt-2 p-2 rounded-lg bg-gray-700 text-white border border-gray-600" /> |
| 153 | + </div> |
| 154 | + |
| 155 | + <div className="w-full md:w-1/3"> |
| 156 | + <label className="text-gray-300">Values (comma separated):</label> |
| 157 | + <input value={valuesInput} onChange={(e) => setValuesInput(e.target.value)} className="w-full mt-2 p-2 rounded-lg bg-gray-700 text-white border border-gray-600" /> |
| 158 | + </div> |
| 159 | + |
| 160 | + <div className="flex items-center gap-3"> |
| 161 | + <label className="text-gray-300">Capacity (W):</label> |
| 162 | + <input type="number" value={capacity} onChange={(e) => setCapacity(Math.max(0, Number(e.target.value)))} className="w-24 p-2 rounded-lg bg-gray-700 text-white border border-gray-600" /> |
| 163 | + </div> |
| 164 | + |
| 165 | + <div className="flex items-center gap-3"> |
| 166 | + <label className="text-gray-300">Algorithm:</label> |
| 167 | + <select value={algorithm} onChange={(e) => setAlgorithm(e.target.value)} className="p-2 rounded-lg bg-gray-700 text-white border border-gray-600"> |
| 168 | + <option value="topDown">Top-Down (Memoization)</option> |
| 169 | + <option value="bottomUp">Bottom-Up (Tabulation)</option> |
| 170 | + </select> |
| 171 | + </div> |
| 172 | + |
| 173 | + <button onClick={handleCompute} className="bg-indigo-600 hover:bg-indigo-700 text-white font-bold px-6 py-3 rounded-xl">Re-Visualize</button> |
| 174 | + </div> |
| 175 | + |
| 176 | + {steps.length > 0 ? ( |
| 177 | + <> |
| 178 | + <div className="flex flex-wrap justify-between items-center mb-6 p-4 rounded-xl bg-gray-800 border border-gray-700 shadow-lg"> |
| 179 | + <button onClick={togglePlay} className={`px-5 py-2 rounded-lg font-semibold text-lg ${isPlaying ? "bg-red-600" : "bg-green-600"} text-white`}>{isFinalStep && !isPlaying ? "Replay ▶️" : isPlaying ? "Pause ⏸️" : "Play ▶️"}</button> |
| 180 | + |
| 181 | + <div className="flex gap-2"> |
| 182 | + <button onClick={handlePrev} disabled={currentStep === 0} className={`px-3 py-2 rounded-lg font-semibold ${currentStep > 0 ? "bg-purple-600 text-white" : "bg-gray-600 text-gray-400"}`}>< Prev</button> |
| 183 | + <button onClick={handleNext} disabled={currentStep === steps.length - 1} className={`px-3 py-2 rounded-lg font-semibold ${currentStep < steps.length - 1 ? "bg-purple-600 text-white" : "bg-gray-600 text-gray-400"}`}>Next ></button> |
| 184 | + </div> |
| 185 | + |
| 186 | + <div className="flex items-center gap-2"> |
| 187 | + <label className="text-gray-300">Speed:</label> |
| 188 | + <select value={speed} onChange={(e) => setSpeed(Number(e.target.value))} className="p-2 rounded-lg bg-gray-700 text-white border border-gray-600"> |
| 189 | + {Object.entries(SPEED_OPTIONS).map(([label, ms]) => ( |
| 190 | + <option key={label} value={ms}>{label}</option> |
| 191 | + ))} |
| 192 | + </select> |
| 193 | + </div> |
| 194 | + </div> |
| 195 | + |
| 196 | + <div className="text-center mb-4"> |
| 197 | + <p className="text-2xl font-bold text-yellow-400">Step <b>{currentStep + 1}</b> / <b>{steps.length}</b></p> |
| 198 | + </div> |
| 199 | + |
| 200 | + <div className="border border-gray-700 p-6 rounded-xl bg-gray-800 shadow-2xl"> |
| 201 | + <div className="mb-6 p-4 rounded-lg bg-gray-700 border-l-4 border-teal-400 shadow-inner"> |
| 202 | + <p className="text-teal-400 font-medium text-md uppercase tracking-wide">Current Action</p> |
| 203 | + <p className="text-xl mt-2 text-gray-200 leading-relaxed">{currentState.message || 'Starting computation...'}</p> |
| 204 | + </div> |
| 205 | + |
| 206 | + {currentState.array && ( |
| 207 | + <DPGrid array={currentState.array} currentIndex={currentState.currentIndex || currentState.index} /> |
| 208 | + )} |
| 209 | + |
| 210 | + {isFinalStep && finalResult !== null && ( |
| 211 | + <div className="mt-8 p-5 rounded-xl bg-green-900 border border-green-700 text-center shadow-lg"> |
| 212 | + <p className="text-green-400 text-2xl font-extrabold flex items-center justify-center gap-3">🎉 Final Result: Max Value = <span className="text-green-200 text-3xl">{finalResult}</span></p> |
| 213 | + </div> |
| 214 | + )} |
| 215 | + |
| 216 | + <details className="mt-8 text-sm text-gray-400"> |
| 217 | + <summary className="cursor-pointer hover:text-gray-200 text-md font-medium">Click to View Raw Step Data (for debugging)</summary> |
| 218 | + <pre className="bg-gray-900 p-4 rounded-lg mt-3 overflow-auto text-xs border border-gray-700 shadow-inner max-h-60">{JSON.stringify(currentState, null, 2)}</pre> |
| 219 | + </details> |
| 220 | + </div> |
| 221 | + </> |
| 222 | + ) : ( |
| 223 | + <div className="text-center p-12 bg-gray-800 rounded-xl text-gray-400 text-xl shadow-xl border border-gray-700"> |
| 224 | + <p className="mb-4">Welcome to the Knapsack Variants Visualizer!</p> |
| 225 | + <p>Provide weights and values (comma-separated) and a capacity to start.</p> |
| 226 | + </div> |
| 227 | + )} |
| 228 | + </div> |
| 229 | + </div> |
| 230 | + ); |
| 231 | +} |
0 commit comments