A graph G = (V, E) consists of:
Example: V = \{1,2,3,4,5\}, E = \{(1,2), (1,3), (2,3), (2,4), (3,5), (4,5)\}
Graphs model relationships: social networks, road maps, molecules, dependencies, …
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
Many important optimization problems are defined on graphs:
All of these are NP-hard on general graphs — no polynomial-time algorithm is known.
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).
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.
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.
Trees have a key property that makes DP possible:
Optimal substructure: The optimal solution for T_u depends only on the optimal solutions of its children’s subtrees.
For each node u, we define two values:
MWIS_{in}(u): maximum weight of an independent set of T_u that includes u.
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)
Case 1 — Node u is IN the set:
Case 2 — Node u is OUT of the set:
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).
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])
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.
Key insight: The tree structure allows us to solve an NP-hard problem in linear time by exploiting the natural hierarchical decomposition.
CS F364: Design & Analysis of AlgorithmsTulasimohan Molli