BITS Pilani
CS F364: Design and Analysis of Algorithms

Graphs & Dynamic Programming on Trees

Lecture 12 |2026-06-04

Graphs & Dynamic Programming on Trees

  • Graphs & Dynamic Programming on Trees
  • Dynamic Programming on Trees

What is a Graph?

A graph G = (V, E) consists of:

  • A set V of vertices (nodes)
  • A set E of edges (connections between vertices)

Example: V = \{1,2,3,4,5\}, E = \{(1,2), (1,3), (2,3), (2,4), (3,5), (4,5)\}

    1 --- 2
    | \   |
    |  \  |
    |   \ |
    3 --- 4 --- 5

Graphs model relationships: social networks, road maps, molecules, dependencies, …

Graph Terminology

  • Undirected graph: edges have no direction — (u,v) = (v,u)
  • Directed graph (digraph): edges have direction — u \to v
  • Path: sequence of vertices where consecutive vertices are connected by edges
  • Cycle: a path that starts and ends at the same vertex
  • Connected graph: there is a path between every pair of vertices
  • Tree: a connected, acyclic undirected graph

Special Graphs

Path: vertices in a line

Cycle: vertices in a ring

Tree: connected, no cycles — |E| = |V| - 1

Complete graph K_n: every pair of vertices is connected

In this lecture, we focus on Trees — the simplest non-trivial graph structure that still captures many real-world problems.

Trees have a natural hierarchical structure: pick any node as root, and every node has a parent (except root) and children.

    1
   / \
  2   3
 / \
4   5

Problems on Graphs

Many important optimization problems are defined on graphs:

  • Independent Set: subset of vertices with no edges between them
  • Vertex Cover: subset of vertices that touches every edge
  • Clique: subset of vertices where every pair is connected
  • Hamiltonian Path: path visiting every vertex exactly once
  • Graph Coloring: assign colors so adjacent vertices differ

All of these are NP-hard on general graphs — no polynomial-time algorithm is known.

Independent Set

Independent Set (IS)

Given an undirected graph G = (V, E), an Independent Set is a set S \subseteq V such that no two vertices in S are adjacent:

\forall u, v \in S,\quad (u,v) \notin E

Maximum Weight Independent Set (MWIS)

Given G = (V, E) and weights w: V \to \mathbb{R}^+, find an independent set S that maximizes \sum_{v \in S} w(v).

MWIS is NP-hard

  • Finding the Maximum Independent Set (or MWIS) in a general graph is NP-hard.
  • Even approximating it within a constant factor is hard.
  • No polynomial-time algorithm is known (and likely none exists).

But — if we restrict the graph to a tree, we can solve MWIS in linear time O(|V|) using Dynamic Programming!

This is a recurring theme in algorithms: special graph classes admit efficient solutions for otherwise hard problems.

Dynamic Programming on Trees

  • Graphs & Dynamic Programming on Trees
  • Dynamic Programming on Trees

The Corporate Party Problem

Your company has a hierarchy shaped like a tree — CEO at the root, managers below, employees at the leaves.

You are planning a party and each employee u has a fun factor w(u).

Rule: If an employee attends the party, their direct manager cannot attend.

(No two adjacent people in the tree can both attend.)

Goal: Choose a set of invitees that maximizes total fun, respecting the rule.

This is exactly the Maximum Weight Independent Set problem — on a tree. We’ll solve it in O(n) using DP.

Tree Structure Enables DP

Trees have a key property that makes DP possible:

  • Pick an arbitrary vertex r as the root.
  • This defines a parent-child relationship throughout the tree.
  • Every node u has a set of children C(u).
  • The subtree T_u rooted at u is independent of other subtrees.

Optimal substructure: The optimal solution for T_u depends only on the optimal solutions of its children’s subtrees.

DP States

For each node u, we define two values:

  1. MWIS_{in}(u): maximum weight of an independent set of T_u that includes u.

  2. MWIS_{out}(u): maximum weight of an independent set of T_u that excludes u.

The answer for the whole tree is: \text{Optimal} = \max\big(MWIS_{in}(r),\; MWIS_{out}(r)\big)

Recurrences

Case 1 — Node u is IN the set:

  • None of its children can be in the set. MWIS_{in}(u) = w(u) + \sum_{v \in C(u)} MWIS_{out}(v)

Case 2 — Node u is OUT of the set:

  • Each child may be either in or out — choose the better option. MWIS_{out}(u) = \sum_{v \in C(u)} \max\big(MWIS_{in}(v),\; MWIS_{out}(v)\big)

Base Case and Traversal

Base case (leaf node): MWIS_{in}(u) = w(u),\qquad MWIS_{out}(u) = 0

Traversal order: We must process children before the parent → Post-order traversal (bottom-up).

  1. Recursively compute DP values for all children of u.
  2. Compute MWIS_{in}(u) and MWIS_{out}(u) using the children’s values.
  3. Return to parent.

Algorithm

MWIS-Tree(u):
    // Post-order: compute children first
    for each child v in C(u):
        MWIS-Tree(v)
    
    // Compute DP values
    MWIS_in[u] = w(u)
    MWIS_out[u] = 0
    
    for each child v in C(u):
        MWIS_in[u]  += MWIS_out[v]
        MWIS_out[u] += max(MWIS_in[v], MWIS_out[v])

Initial call: MWIS-Tree(r) where r is the chosen root.

Answer: \max(MWIS_{in}[r],\; MWIS_{out}[r])

Trace Example

Tree: Root A (weight 5) has children B (weight 4) and C (weight 6). B and C are leaves.

    A (5)
   / \
  B   C
 (4) (6)

Bottom-up computation:

Node MWIS_{in} MWIS_{out} Explanation
B 4 0 Leaf: w(B), 0
C 6 0 Leaf: w(C), 0
A 5 + 0 + 0 = 5 \max(4,0) + \max(6,0) = 10 Root uses children

Answer: \max(5, 10) = 10 — Independent set \{B, C\} with weight 4+6=10.

Complexity Analysis

  • Time: Each node visited once. At each node, work is proportional to the number of its children. Sum of children over all nodes = |V| - 1. Total: \Theta(|V|).
  • Space: O(|V|) for storing DP values and recursion stack.

Key insight: The tree structure allows us to solve an NP-hard problem in linear time by exploiting the natural hierarchical decomposition.

Summary

  • Graphs model relationships and many problems on them are NP-hard.
  • Trees are a special class of graphs with no cycles, which makes DP possible.
  • DP on Trees generalizes sequence DP: propagate solutions from children to parents.
  • MWIS on Trees is solvable in O(n) using two DP states per node.
  • This pattern extends to many other tree problems (tree vertex cover, tree coloring, etc.).