Maths 1: Week 11 - The Master Encyclopedia of Trees & Traversal

1. The Genesis: The Efficient Network 📜

1.1 The Economy of Connection

In the previous week, we learned how to build a network. This week, we learn efficiency. Imagine you are a telecommunications company. You want to connect 100 cities with fiber-optic cable. Cables are expensive. You don’t want any loops (cycles) because that’s wasted money. You want the minimum amount of cable to ensure every city can talk to every other city.

This “Minimalist” network is called a Tree. Trees are the fundamental structures of data science—they power file systems, decision-making algorithms (Decision Trees), and organization charts.

Once we have our network, we need to know how to “Walk” through it. If you are a spider looking for a fly, or Google looking for a webpage, you need a Search Strategy. This week explores the algorithms that navigated the world before GPS.

1.2 The Philosophical Intuition

A Tree is a graph that has Zero Waste. It is the point where connectivity meets simplicity. Traversal is the “Action” of a graph—it’s how we move from knowledge to discovery.


2. Axiomatic Foundations: Defining the Tree 🏛️

2.1 Formal Definition of a Tree

A Tree is a connected graph with No Cycles (Acyclic).

  • Forest: A graph where every component is a tree (multiple disconnected trees).

2.2 The Equivalence Axioms (The Rule)

For a graph with vertices, the following statements are all different ways of saying “It is a Tree”:

  1. is connected and has no cycles.
  2. is connected and has exactly edges.
  3. Any two vertices in are connected by Exactly One unique path.
  4. Adding just ONE more edge creates exactly one cycle. These rules are the “DNA” of the tree. If even one is true, they are all true.

2.3 The “Leaf” Principle

Every tree with vertices must have at least two vertices of degree 1 (called “Leaves”). Intuition: A tree has to start and end somewhere—it can’t just loop forever.


3. Topology of Search: BFS & DFS 🛠️

3.1 Breadth-First Search (BFS)

  • Strategy: Explore all neighbors at distance 1, then all at distance 2, and so on.
  • Mental Model: A ripple in a pond.
  • Data Structure: Uses a Queue (First-In, First-Out).
  • Core Use: Finding the Shortest Path in terms of number of jumps.

3.2 Depth-First Search (DFS)

  • Strategy: Pick a path and go as deep as possible until you hit a dead end, then backtrack.
  • Mental Model: Exploring a cave or a maze.
  • Data Structure: Uses a Stack (First-In, Last-Out).
  • Core Use: Checking for cycles or exploring the reach of a network.

4. Weighted Graphs & Shortest Paths 🖋️

4.1 Edge Weights

In many graphs (like maps), edges have costs (Distance, Time, Fuel).

  • The Optimization Goal: Find the path between A and B with the Minimum Total Weight.

4.2 Dijkstra’s Algorithm (The GPS Heart)

This is a “Greedy” algorithm. It finds the shortest path by always keeping track of the “currently known shortest distance” to every node and updating it as it finds better ways.

  • Limitation: Only works if weights are positive. If you have negative weights (a road that gives you fuel), you need the Bellman-Ford algorithm.

5. Spanning Trees: Connecting the World 📈

5.1 Minimum Spanning Tree (MST)

If a graph has weights, the MST is the subgraph that:

  1. Is a Tree (connects everyone with no loops).
  2. Has the lowest possible sum of edge weights.
  • The Algorithms: Kruskal’s (sorting edges and adding smallest ones) and Prim’s (growing from a root node).

6. The Encyclopedia of Worked Examples (10 Case Studies) 📚

Case 1: The Edge Check

Problem: Can a connected graph with 5 vertices and 5 edges be a tree?

  • Logic: A tree with 5 vertices MUST have exactly edges.
  • Result: No. Since it’s connected and has 5 edges, it must contain a cycle.

Case 2: Counting Edges in a Forest

Problem: A forest has 10 vertices and 3 separate components. How many edges does it have?

  • Formula: For each component with vertices, edges .
  • Calculation: Total edges .
  • Calculation: .
  • Result: 7 edges.

Case 3: BFS Level Order

Problem: Perform BFS from Node A in a star graph where A is the center.

  • Thinking Process: Mark A. Visit all neighbors. Since all other nodes are neighbors of A, they are all at Level 1.
  • Result: A {All other nodes}.

Case 4: Identifying Cycles via DFS

Problem: If during a DFS you find a “Back Edge” (an edge pointing to a node already being processed), what does it mean?

  • Result: It means a Cycle exists.

Case 5: The Leaf Lemma Calculation

Problem: A tree has 10 vertices. Two vertices have degree 4, and one has degree 2. The rest are leaves. How many leaves are there?

  • Step 1: Let be the number of leaves. Total vertices .
  • Step 2: Total edges in a tree with 10 vertices is 9.
  • Step 3: Handshaking Lemma: Sum of degrees .
  • Step 4: Sum .
  • Step 5: Wait—. The problem description given must be impossible.
  • Result: Logically impossible configuration.

Case 6: Path Uniqueness in Trees

Problem: In a tree, how many paths exist between Node 1 and Node 10?

  • Axiom: In a tree, any two points have exactly one path.
  • Result: 1.

Case 7: Dijkstra’s Step-by-Step

Problem: Start A. Neighbors B(cost 5), C(cost 2). To reach D, travel BD(1) or CD(10).

  • Thinking Process: Standard BFS would pick ABD (2 jumps). Dijkstra picks AB (cost 5) vs AC (cost 2). It starts at C. CD cost is 10 (Total 12). Then it checks BD (Total 6).
  • Result: Shortest path is A B D (Cost 6).

Case 8: Spanning Tree Count

Problem: How many spanning trees does a triangle graph have?

  • Logic: A triangle has 3 edges. To make it a tree, we must remove 1 edge to break the cycle.
  • Result: 3 possible spanning trees.

Case 9: Tree as a Bipartite Graph

Problem: Is every tree bipartite?

  • Logic: A tree has NO cycles. Therefore, it has no ODD cycles.
  • Result: Yes, every tree is bipartite.

Case 10: Rooted Trees (Generations)

Problem: In a family tree, what is the degree of a “Child” in the middle of the tree?

  • Logic: 1 edge to the Parent + edges to Children.
  • Result: Degree is .

7. Fundamental “How-To” Recipes 🍳

Recipe: Executing BFS (The Ripple)

  1. Queue: Put Starting Node in a Queue and mark it “Visited.”
  2. Loop: While Queue is not empty:
    • Pop the front node .
    • For every neighbor of that isn’t visited:
      • Mark “Visited.”
      • Put in the Queue.
  3. Path: The order they are added to the Queue is the BFS Visit Order.

Recipe: Prim’s Algorithm for MST

  1. Pick a Root: Start with any node.
  2. Find Cheapest Edge: Find the lightest edge connecting a “Visited” node to an “Unvisited” node.
  3. Add & Repeat: Add that edge and node to your tree. Repeat until everyone is connected.

8. Encyclopedia Mastery Challenge 🏆

  1. The Proof: If you have a connected graph with vertices and edges, why does it have exactly one cycle?
  2. The BFS Shortest Path: Why is BFS guaranteed to find the path with fewer edges, but NOT necessarily the path with lowest total weight? (Hint: Think about weights).
  3. The Rooted Relation: How many edges are in a “Complete Binary Tree” with 4 levels?
  4. The Inverse DFS: Can you find a graph where the BFS Visit Order and DFS Visit Order are the same? (Hint: Think about a line).

🚀 Master Status: You have completed the Encyclopedic Expansion of Trees and Traversal. You now hold the power to find the most efficient paths through any complex network. 助