|
2 | 2 | import { motion } from "framer-motion"; |
3 | 3 |
|
4 | 4 | export default function UnionFindVisualizer({ nodes, parent, highlights }) { |
| 5 | + // Calculate positions for nodes in a grid layout |
| 6 | + const getNodePosition = (index) => { |
| 7 | + const cols = Math.ceil(Math.sqrt(nodes.length)); |
| 8 | + const row = Math.floor(index / cols); |
| 9 | + const col = index % cols; |
| 10 | + return { row, col }; |
| 11 | + }; |
| 12 | + |
| 13 | + // Generate connections for visualization |
| 14 | + const connections = []; |
| 15 | + nodes.forEach((node, index) => { |
| 16 | + if (parent[index] !== index) { |
| 17 | + const parentPos = getNodePosition(parent[index]); |
| 18 | + const childPos = getNodePosition(index); |
| 19 | + connections.push({ |
| 20 | + from: childPos, |
| 21 | + to: parentPos, |
| 22 | + child: index, |
| 23 | + parent: parent[index] |
| 24 | + }); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
5 | 28 | return ( |
6 | | - <div className="flex flex-wrap justify-center gap-6 mt-6"> |
7 | | - {nodes.map((node, index) => { |
8 | | - const isRoot = parent[index] === index; |
9 | | - const color = |
10 | | - highlights.current === index |
11 | | - ? "bg-blue-500" |
12 | | - : highlights.root === index |
13 | | - ? "bg-green-500" |
14 | | - : highlights.union.includes(index) |
15 | | - ? "bg-red-500" |
16 | | - : "bg-gray-300"; |
| 29 | + <div className="relative"> |
| 30 | + {/* SVG for connections */} |
| 31 | + <svg |
| 32 | + className="absolute inset-0 w-full h-full pointer-events-none" |
| 33 | + style={{ minHeight: '300px' }} |
| 34 | + > |
| 35 | + {connections.map((conn, index) => { |
| 36 | + const fromX = (conn.from.col + 0.5) * (100 / Math.ceil(Math.sqrt(nodes.length))) + '%'; |
| 37 | + const fromY = (conn.from.row + 0.5) * (100 / Math.ceil(Math.sqrt(nodes.length))) + '%'; |
| 38 | + const toX = (conn.to.col + 0.5) * (100 / Math.ceil(Math.sqrt(nodes.length))) + '%'; |
| 39 | + const toY = (conn.to.row + 0.5) * (100 / Math.ceil(Math.sqrt(nodes.length))) + '%'; |
| 40 | + |
| 41 | + const isHighlighted = highlights.union.includes(conn.child) || |
| 42 | + highlights.current === conn.child || |
| 43 | + highlights.root === conn.parent; |
| 44 | + |
| 45 | + return ( |
| 46 | + <motion.line |
| 47 | + key={index} |
| 48 | + x1={fromX} |
| 49 | + y1={fromY} |
| 50 | + x2={toX} |
| 51 | + y2={toY} |
| 52 | + stroke={isHighlighted ? "#ef4444" : "#6b7280"} |
| 53 | + strokeWidth={isHighlighted ? "3" : "2"} |
| 54 | + strokeDasharray={isHighlighted ? "5,5" : "none"} |
| 55 | + initial={{ pathLength: 0 }} |
| 56 | + animate={{ pathLength: 1 }} |
| 57 | + transition={{ duration: 0.5, delay: index * 0.1 }} |
| 58 | + /> |
| 59 | + ); |
| 60 | + })} |
| 61 | + </svg> |
| 62 | + |
| 63 | + {/* Nodes */} |
| 64 | + <div className="grid gap-4 justify-center" style={{ |
| 65 | + gridTemplateColumns: `repeat(${Math.ceil(Math.sqrt(nodes.length))}, 1fr)`, |
| 66 | + maxWidth: '600px', |
| 67 | + margin: '0 auto' |
| 68 | + }}> |
| 69 | + {nodes.map((node, index) => { |
| 70 | + const isRoot = parent[index] === index; |
| 71 | + const color = |
| 72 | + highlights.current === index |
| 73 | + ? "bg-blue-500" |
| 74 | + : highlights.root === index |
| 75 | + ? "bg-green-500" |
| 76 | + : highlights.union.includes(index) |
| 77 | + ? "bg-red-500" |
| 78 | + : highlights.path.includes(index) |
| 79 | + ? "bg-purple-500" |
| 80 | + : "bg-gray-600"; |
| 81 | + |
| 82 | + return ( |
| 83 | + <motion.div |
| 84 | + key={index} |
| 85 | + className={`w-20 h-20 flex flex-col items-center justify-center text-white font-semibold rounded-full shadow-lg border-2 border-white/20 ${color} relative`} |
| 86 | + layout |
| 87 | + animate={{ |
| 88 | + scale: highlights.current === index ? 1.2 : 1, |
| 89 | + boxShadow: highlights.current === index ? "0 0 20px rgba(59, 130, 246, 0.5)" : "0 4px 6px rgba(0, 0, 0, 0.1)" |
| 90 | + }} |
| 91 | + transition={{ duration: 0.3 }} |
| 92 | + > |
| 93 | + <div className="text-lg font-bold">{node}</div> |
| 94 | + <div className="text-xs opacity-80">p:{parent[index]}</div> |
| 95 | + {isRoot && ( |
| 96 | + <div className="absolute -top-2 -right-2 w-6 h-6 bg-yellow-400 rounded-full flex items-center justify-center"> |
| 97 | + <span className="text-[10px] font-bold text-black">R</span> |
| 98 | + </div> |
| 99 | + )} |
| 100 | + {highlights.current === index && ( |
| 101 | + <div className="absolute -top-3 -left-3 w-6 h-6 bg-blue-400 rounded-full flex items-center justify-center animate-pulse"> |
| 102 | + <span className="text-[10px] font-bold text-white">F</span> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + </motion.div> |
| 106 | + ); |
| 107 | + })} |
| 108 | + </div> |
17 | 109 |
|
18 | | - return ( |
19 | | - <motion.div |
20 | | - key={index} |
21 | | - className={`w-16 h-16 flex flex-col items-center justify-center text-white font-semibold rounded-full shadow-md ${color}`} |
22 | | - layout |
23 | | - animate={{ scale: highlights.current === index ? 1.2 : 1 }} |
24 | | - > |
25 | | - <div>{node}</div> |
26 | | - <div className="text-xs">p:{parent[index]}</div> |
27 | | - {isRoot && <div className="text-[10px]">(root)</div>} |
28 | | - </motion.div> |
29 | | - ); |
30 | | - })} |
| 110 | + {/* Additional Info */} |
| 111 | + <div className="mt-6 text-center text-sm text-gray-300"> |
| 112 | + <p>Each node shows: <span className="text-white font-semibold">Node Number</span> | <span className="text-gray-400">Parent</span></p> |
| 113 | + <p className="mt-1"> |
| 114 | + <span className="text-yellow-400">R</span> = Root | |
| 115 | + <span className="text-blue-400"> F</span> = Finding | |
| 116 | + <span className="text-red-400"> Red</span> = Unioning |
| 117 | + </p> |
| 118 | + </div> |
31 | 119 | </div> |
32 | 120 | ); |
33 | 121 | } |
0 commit comments