Skip to content

JaineelVora08/graph-analyse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

DSA-Project β€” Social Network Graph Analysis Platform

License: MIT C++ React

A full-stack social network analysis platform demonstrating advanced graph algorithms and data structures. Features a high-performance C++ backend with RESTful API and modern React frontend.

🌟 Features

Core Functionality

  • User Management: Create and delete users with unique usernames
  • Social Graph: Follow/unfollow users, create posts, like content
  • Shortest Path: BFS algorithm to find connections between users
  • Recommendations: Jaccard similarity-based friend suggestions
  • User Rankings: Bipartite PageRank over users and posts
  • Community Detection: DSU algorithm for social groups
  • Content Moderation: Multi-layer vulgar content detection
  • Full-Text Search: Inverted index with conjunctive queries
  • Autocomplete: Fast prefix matching for usernames

Data Structures & Algorithms

  • Graph Representation: Adjacency list (unordered_map)
  • BFS: Shortest path finding
  • Jaccard Similarity: Recommendation engine
  • Disjoint Set Union (DSU): Community detection
  • Inverted Index: Fast text search
  • Weighted Interactions: Like/view edges with 72-hour time decay
  • Trending Posts: Heap-backed Top-K ranking by post PageRank
  • Unique View Estimation: HyperLogLog-based approximate distinct viewers
  • Trie: Autocomplete
  • Aho-Corasick: Pattern matching
  • HyperLogLog: Probabilistic unique counting
  • Min-Heap: Efficient Top-K trending maintenance

πŸš€ Quick Start

Prerequisites

  • C++ Compiler: g++ 7.0+ with C++17 support
  • Node.js: v14+ with npm
  • Make: For building backend

Setup & Run

# Clone repository
git clone https://github.com/harsh-c-16/DSA-Project.git
cd DSA-Project

# Terminal 1: Start Backend
cd backend
make clean && make
./graph_engine        # Runs on port 8080

# Terminal 2: Start Frontend
cd frontend
npm install
npm start             # Opens http://localhost:3000

Deployment

Backend on Render

This repo includes render.yaml and backend/Dockerfile. Create a Render Blueprint from the repository, or create a Docker web service with:

  • Root/context: backend
  • Dockerfile: backend/Dockerfile
  • Health check path: /health

The backend reads Render's PORT environment variable automatically and falls back to 8080 locally.

Frontend on Vercel

Deploy the frontend directory as a Create React App project. Set this Vercel environment variable to your Render backend URL:

REACT_APP_API_BASE_URL=https://your-render-service.onrender.com

For local development, leave REACT_APP_API_BASE_URL blank and the CRA proxy in frontend/package.json will continue to route API calls to http://localhost:8080.

Linux/Mac One-Liner:

./start.sh

πŸ“ Project Structure

DSA-Project/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ main.cpp              # HTTP server & API routes
β”‚   β”‚   β”œβ”€β”€ graph.hpp             # Graph class interface
β”‚   β”‚   β”œβ”€β”€ graph_impl_final.cpp  # Main graph implementation
β”‚   β”‚   β”œβ”€β”€ dsu.cpp/hpp           # Disjoint Set Union
β”‚   β”‚   β”œβ”€β”€ hll.cpp/hpp           # HyperLogLog unique counting
β”‚   β”‚   β”œβ”€β”€ trie.cpp/hpp          # Trie autocomplete
β”‚   β”‚   └── aho_corasick.cpp/hpp  # Aho-Corasick matching
β”‚   β”œβ”€β”€ CMakeLists.txt            # Primary build configuration
β”‚   β”œβ”€β”€ Makefile                  # Convenience wrapper around CMake
β”‚   β”œβ”€β”€ db/                       # Persistent storage
β”‚   └── graph_engine              # Compiled binary
β”‚
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.jsx               # Main component
β”‚   β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”‚   β”œβ”€β”€ ManagementPage.jsx    # User/post management
β”‚   β”‚   β”‚   └── AnalyticsPage.jsx     # Rankings & analytics
β”‚   β”‚   └── components/
β”‚   β”‚       β”œβ”€β”€ GraphManager.jsx      # Add users/posts/interactions
β”‚   β”‚       β”œβ”€β”€ UserList.jsx          # Paginated user list
β”‚   β”‚       β”œβ”€β”€ UserRanking.jsx       # User leaderboard
β”‚   β”‚       β”œβ”€β”€ TopPosts.jsx          # Most liked posts
β”‚   β”‚       β”œβ”€β”€ PathExplorer.jsx      # Find connections
β”‚   β”‚       β”œβ”€β”€ CommunityViewer.jsx   # Community detection
β”‚   β”‚       └── SearchBar.jsx         # Full-text search
β”‚   β”œβ”€β”€ package.json
β”‚   └── public/
β”‚
β”œβ”€β”€ start.sh                      # Quick start script
β”œβ”€β”€ .gitignore
β”œβ”€β”€ LICENSE
└── README.md                     # This file

πŸ”§ API Endpoints

Backend server runs on port 8080 with the following REST API:

User Management

Method Endpoint Body Description
POST /user username=<name> Create new user
POST /user/delete user_id=<id> Delete user and cleanup
GET /users-list?page=1&limit=50 - Paginated user list

Posts & Interactions

Method Endpoint Body Description
POST /post user_id=<id>&content=<text> Create post (with moderation)
POST /post/delete post_id=<id> Delete post
POST /interaction type=follow&user_id=<id>&target_id=<id> Follow user
POST /interaction type=like&user_id=<id>&target_id=<post_id> Like post (requires follow)
POST /interaction type=view&user_id=<id>&target_id=<post_id> View post (updates HLL)
GET /posts/all - Get all posts
GET /posts/top10 - Top 10 trending posts by post PageRank
GET /post/metrics/<id> - Post likes, unique views, score, decayed weight
GET /post/unique-views/<id> - Estimated unique viewers for a post

Analytics & Graph Operations

Method Endpoint Query Description
GET /users/ranked?page=1&limit=10 - User leaderboard
GET /path?u1=<id>&u2=<id> - Shortest path (BFS)
GET /recommendations?u=<id> - Friend suggestions (Jaccard)
GET /communities - Detect communities (DSU)
GET /search?q=<keyword> - Search posts (inverted index)
GET /autocomplete/users?prefix=<text> - Username autocomplete
GET /user/metrics/<id> - User statistics
GET /user/followers/<id> - List followers
GET /user/followings/<id> - List followings

Tech Stack

Layer Technology Purpose
Frontend React 18.2 UI framework
Styling Tailwind CSS 3.4 Utility-first CSS
HTTP Client Axios 1.4 API requests
Backend C++17 Graph engine
Build System Make/g++ Compilation
HTTP Server Raw sockets Single-threaded server
Storage File-based DB Pipe-delimited text

πŸ§ͺ Usage Example

Sample Workflow

# 1. Create users
curl -X POST http://localhost:8080/user -d "username=Alice"
# Response: {"user_id":11,"username":"Alice"}

curl -X POST http://localhost:8080/user -d "username=Bob"
# Response: {"user_id":12,"username":"Bob"}

# 2. Create post
curl -X POST http://localhost:8080/post -d "user_id=11&content=Hello DSA!"
# Response: {"post_id":1}

# 3. Follow user
curl -X POST http://localhost:8080/interaction -d "type=follow&user_id=12&target_id=11"
# Response: {"status":"ok"}

# 4. Like post (must follow author first)
curl -X POST http://localhost:8080/interaction -d "type=like&user_id=12&target_id=1"
# Response: {"status":"ok"}

# 5. Get recommendations
curl http://localhost:8080/recommendations?u=12
# Response: [13,14,15] (user IDs)

# 6. Find path
curl http://localhost:8080/path?u1=11&u2=12
# Response: [11,12]

🎯 Key Algorithms Explained

1. Jaccard Similarity (Recommendations)

Similarity(A, B) = |A ∩ B| / |A βˆͺ B|

Compares follow sets to find users with similar interests.

2. BFS (Shortest Path)

queue<int> q; q.push(start);
map<int,int> parent;
while (!q.empty()) {
    int u = q.front(); q.pop();
    for (int v : followees[u]) {
        if (!parent.count(v)) {
            parent[v] = u;
            q.push(v);
        }
    }
}

3. DSU (Community Detection)

int find(int x) {
    return parent[x] == x ? x : parent[x] = find(parent[x]);
}
void unite(int a, int b) {
    parent[find(b)] = find(a);
}

4. Bipartite PageRank (Influence)

users --weighted, time-decayed like/view edges--> posts
posts --authorship edges--> users

Each iteration pushes influence from users to posts, boosts posts by HyperLogLog-estimated unique viewers, then returns influence from posts to their authors until convergence.

5. Time Decay

effective_weight = interaction_weight Γ— 0.5^(age / 72 hours)

Recent interactions matter more than stale ones, and the Top-K heap tracks the strongest post scores after each analytics refresh.

Performance Notes

  • In-memory operations: O(1) lookups with hash maps
  • BFS complexity: O(V + E) where V = users, E = follows
  • Recommendations: O(VΒ²) for small graphs (<1000 users)
  • Storage format: Pipe-delimited text file
  • Concurrency: Reader-writer locks for thread safety

πŸ› Troubleshooting

Backend won't compile?

g++ --version  # Should be 7.0+ for C++17
cd backend && make clean && make

Port 8080 already in use?

pkill graph_engine  # Kill old process
lsof -i :8080       # Check what's using the port

Frontend can't connect?

  • Verify backend is running: curl http://localhost:8080/users-list?page=1&limit=1
  • Check frontend/package.json has: "proxy": "http://localhost:8080"

Data persistence?

  • Database saved to backend/db/social_graph.db
  • Validated on save (orphaned records removed)
  • Delete file to reset: rm backend/db/social_graph.db

πŸ“„ License

MIT License - see LICENSE file

Acknowledgments

  • Educational project demonstrating DSA concepts
  • C++17 STL and modern React patterns
  • Graph theory and social network analysis

πŸš€ Ready to start? Run ./start.sh and visit http://localhost:3000

About

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors