VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 11: Graphs

Lesson 11.4 of 16 in this module Unrolled Pro

Clone Graph

Deep-copy a connected undirected graph given one node. From copying recursively without remembering what was copied (infinite recursion on any cycle, and duplicated nodes even without one), to a map from original nodes to their copies that is filled before recursing, so every node is copied exactly once (O(V + E)), done with DFS or BFS. Then copying a model's graph of shared modules (what deepcopy's memo does), copying only part of a graph, and graphs too big to copy.

about 10 min 3 Python examples 2 quick checks

Problem. Each node of a connected undirected graph has a value and a list of neighbours. Given one node, return a deep copy of the whole graph: new node objects with the same values and the same connections, sharing nothing with the original.

text
1 — 2
|   |          clone(node 1)  →  a new graph 1' — 2', 1' — 4', 2' — 3', 3' — 4'
4 — 3

Copying a tree is easy (copy the node, recurse on the children). A graph has cycles and shared nodes (a node reachable along several paths), and copying must preserve both: the copy of a shared node must itself be shared, not duplicated.

ML context. Model objects are graphs: a decoder may tie its output embedding to the input embedding (one shared module used twice), and modules hold references to each other. copy.deepcopy(model) must copy each shared object once and keep it shared; that's exactly the memo map in this lesson (Change A). Copying the wrong way silently unties weights and doubles memory.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Clone Graph"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee