Suppose that the vertices of a graph represent towns and the edges of the graph are roads between these towns. Label each edge with the distance between the towns. If it is desired to run optical cable along the roads so that all the towns are connected, where should the cable be put to minimize the amount of cable used? To answer this type of question we need to be able to find a spanning tree in the graph so that the sum of the numbers on the edges is as small as possible (amongst all spanning trees).

**Prop 3.1.6:** Every tree on n vertices contains exactly n-1 edges.

*Notation:* The number of components of a graph G is denoted by c(G).

- A forest G on n vertices has n - c(G) edges.
- Any graph G on n vertices has at least n - c(G) edges.

**Cor 3.1.10:** A simple n-vertex graph with more than ½(n-1)(n-2) edges must be connected.

**Theorem 3.1.11:** Let T be a graph with n vertices. Then the following statements are equivalent.

- T is a tree.
- T contains no cycles and has n-1 edges.
- T is connected and has n-1 edges.
- T is connected and every edge is a cut-edge.
- Any two vertices of T are connected by
*exactly*one edge. - T contains no cycles, and for any new edge e, the graph T+e has exactly one cycle.

*Def:* rooted tree.

**Theorem 3.2.1:** A directed tree is representable as a rooted tree if and only if one vertex has indegree 0 and all other vertices have indegree 1.

*Def:* level (or depth) of a vertex, height of a tree, parent, child, sibling , descendant, ancestor, leaf, internal vertex.

*Def:* m-ary tree, complete m-ary tree.

**Prop 3.2.2:** An m-ary tree has at most m^{k} vertices at level k.

**Theorem 3.2.3:** Let T be an n-vertex m-ary tree of height h. Then h+1 n (m^{h+1} - 1)/(m-1).

*Def:* ordered tree

**Theorem 3.3.1:** The complete binary tree of height h has 2^{h+1} - 1 vertices.

*Def:* binary code, prefix code

Let T be a binary tree with leaves s_{1}, s_{2}, ... s_{m} such that each leaf s_{i} is assigned a weight w_{i}. The average weighted depth of the binary tree T, denoted wt(T) is given by

**Huffman Algorithm**

*Input:* A set of symbols S = {s_{1},s_{2},...s_{m}} and a set of corresponding weights w_{i}, where w_{i} is the weight associated to the symbol s_{i}.

*Output:* A binary tree representing a prefix code for the set of symbols S whose codewords have minimum average weighted length.

Initialize F to be a forest of isolated vertices, labeled s_{1},...s_{m} with respective weights w_{1},..., w_{m}.

For i = 1 to m-1 do

- Choose from forest F two trees, T and T', of smallest weights (ties settled by some arbitrary ordering of the trees in F).
- Create a new binary tree whose root has T and T' as its left and right subtrees.
- Label the edge to T with a 0 and the edge to T' with a 1.
- Assign the new tree the weight equal to the sum of the weights of T and T'.
- Replace trees T and T' in forest F by the new tree.

The binary tree produced by this algorithm is called a ** Huffman tree**,and the corresponding prefix code is called a

**Lemma 3.3.3:** If the leaves of a binary tree are assigned weights, and if each internal vertex is assigned a weight equal to the sum of its children's weights, then the tree's average weighted depth equals the sum of the weights of its internal vertices.

**Theorem 3.3.4:** For a given list of weights w_{1},w_{2},...,w_{l}, a Huffman tree has the smallest possible average weighted depth among all binary trees whose leaves are assigned those weights.