069
LVL 03 — MID DEVELOPERSESSION 069DAY 69

GRAPH S

🎫 PIXELCRAFT-056
Feature | 🟠 Hard | Priority: 🟡 Medium

Users want to see the edit history of an image as a visual tree: original → v1 → v2 (branched from v1) → v3 (branched from original). Also: "similar images" feature — images sharing the same tags or filters.
CONCEPTS.UNLOCKED
🕸
Graph Data Structure
Nodes (vertices) and edges (connections). Unlike trees, graphs can have cycles, multiple paths, and arbitrary connections. The most general way to model relationships.
Directed vs Undirected
Directed: edges have direction (parent → child, like version history). Undirected: edges go both ways (similar images — if A is similar to B, B is similar to A).
📋
Adjacency List
Map: nodeId → [connectedNodeIds]. The most common graph representation. Space-efficient for sparse graphs. Fast neighbor lookup with O(1) per node.
🌊
BFS (Breadth-First Search)
Explore level by level using a queue. Find all versions reachable from an image. Find shortest paths. "Images 2 hops away" = recommendations.
🏊
DFS (Depth-First Search)
Explore as deep as possible using recursion/stack. Find the longest edit chain. Detect cycles. Topological sorting for dependencies.
🔗
Practical Graph Problems
Version trees, recommendations, dependencies. Social networks, Google Maps, package managers, AI knowledge graphs — all graphs. One of the most versatile structures in CS.
HANDS-ON.TASKS
01
Model Version History as Directed Graph
class VersionGraph { constructor() { this.adjacencyList = new Map(); // nodeId → [childIds] } addVersion(imageId, parentId = null) { if (!this.adjacencyList.has(imageId)) { this.adjacencyList.set(imageId, []); } if (parentId && this.adjacencyList.has(parentId)) { this.adjacencyList.get(parentId) .push(imageId); } } // BFS: find all versions reachable // from an image getAllVersions(startId) { const visited = new Set(); const queue = [startId]; const result = []; while (queue.length > 0) { const current = queue.shift(); if (visited.has(current)) continue; visited.add(current); result.push(current); const children = this.adjacencyList.get(current) || []; queue.push(...children); } return result; } // DFS: find the longest edit chain longestChain(startId, visited = new Set()) { visited.add(startId); const children = this.adjacencyList.get(startId) || []; let maxDepth = 0; for (const child of children) { if (!visited.has(child)) { maxDepth = Math.max(maxDepth, this.longestChain(child, visited)); } } return maxDepth + 1; } }
02
"Similar Images" with Undirected Graph
// Images connected by shared tags function buildSimilarityGraph(images) { const graph = new Map(); // imageId → Set of connected imageIds const tagIndex = new Map(); // tag → Set of imageIds // Build tag index for (const img of images) { for (const tag of img.tags) { if (!tagIndex.has(tag)) tagIndex.set(tag, new Set()); tagIndex.get(tag).add(img.id); } } // Connect images sharing tags for (const [tag, imageIds] of tagIndex) { const ids = [...imageIds]; for (let i = 0; i < ids.length; i++) { for (let j = i + 1; j < ids.length; j++) { if (!graph.has(ids[i])) graph.set(ids[i], new Set()); if (!graph.has(ids[j])) graph.set(ids[j], new Set()); graph.get(ids[i]).add(ids[j]); graph.get(ids[j]).add(ids[i]); } } } return graph; }
03
Build VersionTree Component

Build a <VersionTree> React component visualizing the graph as a tree diagram. Each node shows a thumbnail. Lines connect parent to children. Click a node to load that version.

04
BFS Recommendations

Use BFS to find "images 2 hops away" — recommendations! If image A shares tags with B, and B shares tags with C, then C is recommended for viewers of A. The same algorithm that powers social network suggestions.

05
Close the Ticket
git switch -c feature/PIXELCRAFT-056-graphs git add src/ server/ git commit -m "Add version graph + similarity with BFS/DFS (PIXELCRAFT-056)" git push origin feature/PIXELCRAFT-056-graphs # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

Graphs model relationships.

One of the most versatile data structures in all of CS.

// Graphs are everywhere:

Social networks   → users = nodes,
                     friendships = edges
Google Maps       → intersections = nodes,
                     roads = edges
The internet      → computers = nodes,
                     connections = edges
Package deps      → DAG (directed acyclic)

BFS → shortest paths (GPS navigation)
DFS → deep exploration (maze solving)

// PageRank, circuit design, AI knowledge
// graphs — all graph problems.
"Graph Lab"
[A]Add cycle detection to the version graph: if someone tries to set a child as the parent of its ancestor, detect and reject it. Use DFS with a "currently visiting" set.
[B]Build a dependency graph visualizer: given PixelCraft's package.json, parse dependencies and build a graph. Visualize which packages depend on which. Use topological sort for install order.
[C]Research Dijkstra's algorithm: find the "shortest path" between two images in the similarity graph, where edge weight = number of shared tags. Implement and visualize the path.
REF.MATERIAL
VIDEO
freeCodeCamp
Comprehensive graph tutorial: representation, BFS, DFS, shortest path, topological sort. All the fundamentals in one video.
GRAPHSCOMPREHENSIVEESSENTIAL
VIDEO
Fireship
Ultra-fast graph overview: nodes, edges, directed vs undirected, weighted, BFS vs DFS, and real-world applications.
GRAPHSQUICK
ARTICLE
Wikipedia
Graph theory: adjacency lists vs matrices, traversal algorithms, properties, and the mathematical foundation behind graph structures.
GRAPHSTHEORYCS
ARTICLE
VisuAlgo
Interactive visualization of BFS and DFS on graphs. Watch the algorithms explore step by step. The best way to build intuition.
BFSDFSINTERACTIVE
VIDEO
Abdul Bari
Clear visual explanation of BFS: queue-based traversal, level ordering, shortest path finding. The algorithm behind GPS navigation.
BFSVISUAL
// LEAVE EXCITED BECAUSE
Users can see how their image evolved through edits — a visual version tree. "Similar images" uses the same algorithm that powers social network recommendations.