Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Relieq/kruskal-mst-visualizer

Repository files navigation

Kruskal MST Visualizer

Vietnamese

An interactive web-based visualization tool for Kruskal's Minimum Spanning Tree (MST) algorithm. This educational tool helps students and developers understand how Kruskal's algorithm works step-by-step, with support for both Disjoint Set Union (DSU) and DFS-based cycle detection approaches.

๐Ÿ”— Live Demo: https://kruskal-mst-visualizer.vercel.app/


โœจ Features

๐ŸŽฏ Two Algorithm Modes

  • Kruskal + DSU (Union-Find): Visualizes the classic implementation using Disjoint Set Union with union by rank and optional path compression
  • Kruskal + DFS: Demonstrates cycle detection using Depth-First Search on the current MST

๐Ÿ“Š Interactive Visualization

  • Graph Rendering: Dynamic graph visualization powered by Sigma.js and Graphology
  • Step-by-Step Execution: Navigate through each step of the algorithm with Prev/Next buttons or use the slider
  • Auto-Play Mode: Watch the algorithm run automatically with configurable speed
  • Edge Tooltips: Hover over edges to see their details (endpoints and weight)

๐ŸŽจ Visual Feedback

  • Edge States:

    • ๐ŸŸก Yellow: Current edge being considered
    • ๐ŸŸข Green: Edge chosen for MST
    • โšช Gray: Edge rejected (would create a cycle)
  • DSU Node States (when using DSU mode):

    • ๐ŸŸข Cyan: Node calling find(u) / Root found
    • ๐ŸŸก Yellow: Node being traversed during find operation
    • ๐ŸŸฃ Purple: Root node of the set
  • DFS Overlay (when using DFS mode):

    • ๐ŸŸ  Orange: Candidate neighbors to explore
    • ๐ŸŸข Cyan: Active DFS path
    • โšซ Gray: Dead branch (backtracked)

๐Ÿ”ง Configuration Options

DSU Mode:

  • Toggle Detailed DSU to see individual find/union operations
  • Toggle Path Compression on/off to compare implementations
  • Set Max Find Hops to limit step generation for deep trees

DFS Mode:

  • Toggle Detailed DFS to see individual DFS traversal steps
  • Set Max DFS Steps to limit step generation for complex searches

๐Ÿ“ฑ Responsive Design

  • Collapsible panels for mobile-friendly experience
  • Adaptive layout for different screen sizes
  • Touch-friendly controls

๐Ÿ–ฑ๏ธ Drag & Drop Panels

  • Reorder panels by dragging the grip handle (โ‹ฎโ‹ฎ) on each panel header
  • Desktop (>1024px): Drag panels within their column (left, middle, right) desktop_ver.png
  • Mobile/Tablet (โ‰ค1024px): Freely drag any panel to any position in the single-column layout mobile_ver.png
  • Customize your workspace to focus on what matters most to you

๐Ÿ“ธ Screenshots

Main Interface

main_interface.png

DSU Mode - Path Compression

dsu_mode_path_compression.png

DFS Mode - Cycle Detection

dfs_mode.png

Help Modal - Color Legend

help_modal_color_legend.png


๐Ÿš€ Getting Started

Prerequisites

  • Node.js (v18 or higher recommended)
  • npm or yarn

Installation

# Clone the repository
git clone https://github.com/yourusername/kruskal-mst-visualizer.git
cd kruskal-mst-visualizer

# Install dependencies
npm install

# Start development server
npm run dev

The app will be available at http://localhost:5173

Build for Production

npm run build
npm run preview

๐Ÿ“– How to Use

1. Input Your Graph

Enter your graph in the input panel using the format:

N M
u1 v1 w1
u2 v2 w2
...

Where:

  • N = number of vertices
  • M = number of edges
  • Each edge line contains: u v w (start vertex, end vertex, weight)

Example:

5 8
1 2 1
1 3 4
1 5 1
2 4 2
2 5 1
3 4 3
3 5 3
4 5 2

2. Load the Graph

Click "Tแบฃi ฤ‘แป“ thแป‹" (Load Graph) to visualize your graph.

3. Choose Algorithm Mode

Select between:

  • Kruskal + DSU: Uses Union-Find data structure
  • Kruskal + DFS: Uses DFS for cycle detection

4. Navigate Steps

  • Use Prev/Next buttons to step through manually
  • Use the slider to jump to any step
  • Click Play for automatic playback

5. Understand the Visualization

  • Watch the Code Panel to see which line is being executed
  • Read the Explanation Panel for Vietnamese explanations of each step
  • Check the DSU/DFS State Panel for internal data structure states

๐Ÿ› ๏ธ Tech Stack

speed_insight.png


๐Ÿ“ Project Structure

kruskal-mst-visualizer/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ components/          # React components
โ”‚   โ”‚   โ”œโ”€โ”€ CodeViewer.tsx       # Python code display with highlighting
โ”‚   โ”‚   โ”œโ”€โ”€ CollapsiblePanel.tsx # Collapsible wrapper for panels
โ”‚   โ”‚   โ”œโ”€โ”€ ControlPanel.tsx     # Playback controls
โ”‚   โ”‚   โ”œโ”€โ”€ DFSPanel.tsx         # DFS state visualization
โ”‚   โ”‚   โ”œโ”€โ”€ DraggablePanel.tsx   # Drag & drop wrapper for panels
โ”‚   โ”‚   โ”œโ”€โ”€ DSUPanel.tsx         # DSU state table
โ”‚   โ”‚   โ”œโ”€โ”€ EdgeListPanel.tsx    # Sorted edge list
โ”‚   โ”‚   โ”œโ”€โ”€ ExplanationPanel.tsx # Step explanations
โ”‚   โ”‚   โ”œโ”€โ”€ GraphInput.tsx       # Graph input form
โ”‚   โ”‚   โ”œโ”€โ”€ GraphRenderer.tsx    # Sigma.js graph visualization
โ”‚   โ”‚   โ””โ”€โ”€ HelpModal.tsx        # Color legend modal
โ”‚   โ”œโ”€โ”€ engine/              # Algorithm implementations
โ”‚   โ”‚   โ”œโ”€โ”€ kruskalDsu.ts        # Kruskal + DSU step generator
โ”‚   โ”‚   โ”œโ”€โ”€ kruskalDfs.ts        # Kruskal + DFS step generator
โ”‚   โ”‚   โ”œโ”€โ”€ parser.ts            # Graph input parser
โ”‚   โ”‚   โ””โ”€โ”€ types.ts             # TypeScript type definitions
โ”‚   โ”œโ”€โ”€ App.tsx              # Main application component
โ”‚   โ”œโ”€โ”€ App.css              # Application styles
โ”‚   โ””โ”€โ”€ main.tsx             # Entry point
โ”œโ”€โ”€ scripts/
โ”‚   โ””โ”€โ”€ test-all.ts          # Automated testing script
โ”œโ”€โ”€ test-cases/              # Test input/output files
โ””โ”€โ”€ package.json

๐Ÿงช Testing

Run the automated tests to verify algorithm correctness:

npx tsx scripts/test-all.ts

This will run the algorithm against all test cases in src/test-cases/ and compare outputs.

test_3_ver_code.png


๐Ÿค Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests

๐Ÿ“„ License

This project is open source and available under the MIT License.


๐Ÿ“ Project Report

This project was developed as part of Project I course at Hanoi University of Science and Technology. The full LaTeX report is available in kruskal_mst_visualizer_report_en.pdf.


๐Ÿ™ Acknowledgments

  • Inspired by the need for better algorithm visualization tools in computer science education
  • Built with modern web technologies for accessibility and performance
  • Ms. Nguyแป…n Khรกnh Phฦฐฦกng โ€“ the instructor who guided the Project I course at Hanoi University of Science and Technology, and whose guidance was instrumental throughout the development of this project.

Made with โค๏ธ for algorithm learners everywhere

About

A visualization tool for Kruskal's Minimum Spanning Tree algorithm. Helps students and developers understand graph theory by providing step-by-step interactive demonstrations.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages