Lesson 12 — Intermediate

Trees & Spanning Trees

Tree graph theory ka sabse important special case hai — connected graph bina cycle ke. Is lesson mein seekhenge tree ki properties (n−1 edges), spanning tree, minimum spanning tree (Kruskal-Prim), Cayley formula aur binary trees. Har network design isi par chalta hai.

⏱ 15 min• Intermediate• Graph Theory

Tree kya hai

WHAT

Tree ek aisa CONNECTED graph hai jisme KAHIN bhi CYCLE nahi. Simple definition, par iske properties itne strong hain ki networking se algorithms tak sab isi par chalti hai.

WHEN

Hierarchical data (file system, org chart), network design (bina loop ke wires), BST/heap — trees har jagah. Spanning tree tab chahiye jab minimal connection chahiye.

WHERE

File explorer, family tree, database indexes (B-tree), routing protocols, DSA ki BST — sab trees hain. GATE mein properties direct poochhi jaati hain.

Tree ka golden rule: n vertices ho toh exactly n − 1 edges. Ek aur edge jod do toh cycle ban jaayegi, tree nahi rahega. Ye relation exam mein bar bar aata hai.

Tree ki properties

Tree ki properties (n vertices wala tree):

1) Edges = n − 1 (zyada → cycle, kam → disconnected)
2) Connected + (n−1) edges → tree (equivalent definition)
3) Connected + acyclic → tree
4) n ≥ 2 wale tree mein kam se kam 2 LEAF hote hain
   (leaf = degree 1 ka vertex)
5) Koi do vertices ke beech EXACTLY EK unique path
   (unique path hi cycle na hone ka proof hai)

Proof sketch (1 aur 2):
  Tree = connected + acyclic. n vertices ko jodne ke liye
  n−1 edges hi kaafi hain; ek aur edge jodne par do
  vertices ke beech doosra path = cycle pakki.
Interview line: Q: "Graph with n vertices, no cycle — kitne spanning trees?" A: 1 — kyunki woh khud ek tree hai. Aur tree se edge hatana toh disconnect, jodna toh cycle. Isliye tree minimum edges wala connected graph hai.

Spanning tree aur MST

Spanning tree = uske SARE vertices cover karne wala subgraph jo tree ho. Naya network bana rahe ho toh sabse kam wire = spanning tree (aur minimum spanning tree):

Spanning Tree : graph ka wo TREE jo SARE vertices cover kare
  n vertices → spanning tree mein bhi n − 1 edges
  (extra edges hata do, vertices connect rehne chahiye)

Minimum Spanning Tree (MST) : total weight minimum
  KRUSKAL : edges weight se sort → chhoti se chhoti add karo
            (agar cycle na banaye) — greedy, edge-based
  PRIM    : ek vertex se shuru → har step nearest edge add
            — greedy, vertex-based
  (dono ka TOTAL weight same aata hai — answer unique)

Cayley formula : K_n (complete graph) ke spanning trees
  = n^(n−2)      K4 → 4² = 16 spanning trees

Kruskal aur Prim dono greedy algorithms hain — fark sirf approach mein hai. Kruskal edges ko sort karta hai, Prim vertex se grow karta hai. Dono ka MST weight same — ye GATE mein verify karne wala question ban jaata hai.

Binary trees

BINARY TREE : har node ke max 2 children (left, right)

FULL      : har node 0 ya 2 children
COMPLETE  : har level full + last level left se

Height h (root = level 0):
  Level h pe max 2^h nodes
  Total max = 2^0 + 2^1 + ... + 2^h = 2^(h+1) − 1
  Example: h = 3 → max 15 nodes

Level 1 se count karo (root = level 1) toh total = 2^h − 1

Full binary tree: leaves = internal nodes + 1
  (10 internal nodes → 11 leaves)

n nodes ki height: worst case n − 1 (skewed),
balanced case ≈ log₂ n (BST search isi par depend karta hai)

Exercise

Question: 7 vertices wale tree mein kitne edges hote hain? Sirf number daalo.

Question: K4 (complete graph, 4 vertices) ke kitne spanning trees hain — Cayley formula se? Sirf number daalo.

Question: Tree mein cycle ho sakta hai? (haan / nahi)

Common mistakes

Desi example: Gaon mein paani ki pipeline

Panchayat ko har ghar tak paani pahunchana hai — aur cycle nahi chahiye (loop se pressure issue aur wastage). Tree hi sabse sasta connection hai:

text
Houses = vertices, pipes = edges
n ghar → n − 1 pipe (tree, bina loop)
Ek aur pipe jod di → cycle = wastage + pressure issue
MST (Kruskal/Prim) = sabse sasta pipeline plan
Cayley n^(n−2) : n ghar ka full network = itne tree plans
Exam mapping (GATE / DSA)
  • GATE: tree properties (n−1 edges, 2 leaves) + Cayley formula — regular.
  • DSA: BST/heap recursion isi concept se banti hai.
  • Networking interviews: "why spanning tree protocol?" — loop avoid karne ke liye.
  • Trap: "Tree can have a cycle" — galat, cycle ho gayi toh tree nahi rahega.
Lesson complete?

Trees clear! Agla lesson — Boolean Algebra & K-Map — jahan logic ko circuits mein badal kar simplify kiya jaata hai (digital electronics ka favourite).

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course