A comprehensive graph algorithm analysis framework for modeling stock market correlation networks and detecting market fragmentation in real-time.
Stock Market Tangle implements and benchmarks 7 graph algorithms to analyze stock correlation networks across different market conditions (stable, volatile, crash, normal). The project demonstrates practical applications of graph theory in financial analysis, particularly for:
- Real-time fragmentation detection using Union-Find (sub-millisecond performance)
- Contagion risk assessment via BFS shortest paths
- Market influence ranking with PageRank
- Community detection using Louvain and Girvan-Newman methods
- Stock similarity embeddings through Node2Vec
| Algorithm | Runtime (500 stocks) | Use Case | Complexity |
|---|---|---|---|
| DFS | 1-5ms | Component discovery | O(V+E) |
| Union-Find | 0.7-1.3ms | Fragmentation detection | O(Ξ±(n)) β O(1) |
| BFS | 2-25ms | Contagion paths | O(V+E) |
| PageRank | 8-105ms | Influence ranking | O(kΒ·E) |
| Louvain | 20-100ms | Community detection | O(V log V) |
| Node2Vec | 1.7-24s | Embeddings | O(walksΒ·length) |
| Girvan-Newman | 150ms+ | Hierarchical clustering | O(VΒ²E) |
- Python 3.8+
- pip package manager
# Clone repository
git clone https://github.com/Astr0Lynx/Stock_Market_Tangle.git
cd Stock_Market_Tangle
# Create virtual environment
python -m venv venv
# Activate virtual environment
# Windows:
venv\Scripts\activate
# macOS/Linux:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt# Generate sample stock correlation data
python src/data_generation.py
# Run individual algorithms
python src/union_find.py # Fastest - fragmentation detection
python src/bfs.py # Shortest paths / contagion
python src/dfs.py # Component discovery
python src/pagerank.py # Market influence ranking
python src/louvain.py # Community detection
python src/node2vec.py # Stock embeddings
python src/girvan_newman.py # Hierarchical clustering# Run comprehensive benchmarks (60+ test scenarios)
python benchmarks.py
# Generate performance visualizations
python visualize_results.py
# View results in results/ directory
ls results/*/ # Algorithm-specific charts and metrics- Union-Find: Path compression + union by rank optimization
- BFS: Multi-source shortest path analysis
- DFS: Connected component traversal
- PageRank: Iterative influence propagation
- Louvain: Modularity-optimized community detection
- Girvan-Newman: Edge betweenness-based divisive clustering
- Node2Vec: Biased random walks + Skip-gram embeddings
- 3 graph sizes: 100, 250, 500 stocks
- 4 market scenarios: stable, normal, volatile, crash
- Precision timing with
time.perf_counter() - Memory profiling with
psutil - Automated JSON result export
- Runtime comparison (2Γ2 subplot grids)
- Memory usage analysis
- Component distribution charts
- Graph density plots
- Scalability curves (linear vs quadratic)
- Summary tables with quality metrics
| Scenario | Threshold | Description | Expected Structure |
|---|---|---|---|
| Crash | 0.10 | Panic selling | 1 giant component |
| Stable | 0.40 | Normal correlation | Few large sectors |
| Normal | 0.44 | Moderate fragmentation | ~40-70 components |
| Volatile | 0.68 | High uncertainty | Near-complete isolation |
Stock_Market_Tangle/
βββ src/ # Algorithm implementations
β βββ union_find.py # Disjoint-set union (O(Ξ±(n)))
β βββ bfs.py # Breadth-first search
β βββ dfs.py # Depth-first search
β βββ pagerank.py # Influence ranking
β βββ louvain.py # Community detection
β βββ node2vec.py # Graph embeddings
β βββ girvan_newman.py # Hierarchical clustering
β βββ data_generation.py # Stock correlation generator
β βββ graph.py # Graph data structure
βββ results/ # Benchmark outputs & visualizations
β βββ union_find/ # Algorithm-specific charts
β βββ bfs/
β βββ ...
βββ benchmarks.py # Comprehensive benchmark runner
βββ visualize_results.py # Chart generation system
βββ analysis_report.md # Detailed performance analysis
βββ TESTCASES.md # Standardized test specifications
βββ requirements.txt # Python dependencies
# Run all test cases
pytest
# Run specific algorithm tests
pytest src/test_union_find.py
# View standardized test cases
cat TESTCASES.mdSpeed Champions (β€10ms):
- DFS: 5.0ms - Component discovery
- Union-Find: 1.2ms - Fragmentation detection
- BFS: 25ms - Contagion analysis
Production Ready (10-100ms):
- PageRank: 10-105ms - Converges in 13-68 iterations
- Louvain: 20-100ms - Near-linear scaling
Batch Processing (>100ms):
- Node2Vec: 1.7-24s - Training-dominated
- Girvan-Newman: 150ms+ - O(VΒ²E) limits use
All algorithms maintain <1MB memory overhead for 500-stock graphs, making them suitable for embedded/real-time systems.
For detailed analysis, see analysis_report.md.
This project demonstrates:
- Algorithm complexity analysis: O(1) vs O(n) vs O(nΒ²) practical differences
- Optimization techniques: Path compression, union by rank, lazy evaluation
- Benchmarking best practices: Reproducible timing, memory profiling, statistical analysis
- Real-world graph applications: Financial networks, social networks, infrastructure modeling
- TESTCASES.md - Standardized test specifications for all algorithms
- analysis_report.md - Comprehensive performance analysis with tables and insights
- Python 3.8+: Core language
- NumPy: Numerical operations & matrix computations
- Matplotlib: Visualization generation
- psutil: Memory profiling
- pytest: Testing framework
Contributions welcome! Areas for enhancement:
- Additional graph algorithms (Dijkstra, A*, Kruskal)
- GPU acceleration for large graphs
- Interactive visualization dashboard
- Real-time stock data integration
This project is licensed under the MIT License - see LICENSE file for details.
Guntesh Singh
- GitHub: @Astr0Lynx
- Project: Algorithm Analysis & Design Course Work
Developed as part of the Algorithm Analysis and Design course project (Team: Queue-ties).
Special thanks to collaborators: Avani, Khushi, Saanvi, Vaibhavi for algorithm implementations and testing contributions. course-project-queue-ties/ βββ src/ # Shared modules (main branch) β βββ data_generation.py # Stock data generator (Guntesh) β βββ graph.py # Graph representation (Guntesh) βββ benchmarks/ # Benchmark scripts βββ tests/ # Unit tests βββ results/ # Benchmark results (gitignored) βββ visualize_results.py # Dynamic visualization script βββ USAGE_GUIDE.md # Usage instructions βββ TESTCASES.md # Standardized test cases βββ requirements.txt # Python dependencies
---
## Team Members & Algorithms
- **Guntesh Singh** - Data Foundation, Union-Find, BFS
- **Avani Sood** - Girvan-Newman
- **Khushi Dhingra** - DFS, PageRank
- **Saanvi Jain** - Louvain
- **Vaibhavi Kolipaka** - Node2Vec, Recommendations
---
## Notes
- Each algorithm is implemented in a separate file to ensure modularity.
- The repository includes detailed documentation for setup, usage, and testing.
- Please refer to the `USAGE_GUIDE.md` for instructions on running benchmarks and visualizing results.
- Test cases are standardized and can be found in `TESTCASES.md`.
- If you encounter any issues, feel free to reach out to the team members listed above.