3 lecture
3 lecture
7.1 Introduction
A (free) tree T is
A simple graph
such that for
every pair of
vertices v and w
there is a unique
path from v to w
Rooted tree
Example:
the tree on the right has height 3
Organizational charts
Huffman codes
An internal vertex is a
vertex that has at least one
child
A terminal vertex is a
vertex that has no children
The tree in the example
has 4 internal vertices and
4 terminal vertices
Subtrees
A subtree of a tree T is a tree T' such that
V(T') V(T) and
E(T') E(T)
Characterization of trees
Theorem 7.2.3
If T is a graph with n vertices, the following are
equivalent:
a) T is a tree
b) T is connected and acyclic
(“acyclic” = having no cycles)
c) T is connected and has n-1 edges
d) T is acyclic and has n-1 edges
7.3 Spanning trees
and
T contains all the vertices of G
Spanning tree search
Breadth-first search
method
Depth-first search
method (backtracking)
7.4 Minimal spanning trees
Given a weighted graph G, a
minimum spanning tree is
a spanning tree of G
that has minimum “weight”
1. Prim’s algorithm
Step 0: Pick any vertex as a Step 3: Repeat Step
starting vertex (call it a). T = {a}. 2, choosing the edge
Step 1: Find the edge with of smallest weight
smallest weight incident to a. that does not form a
Add it to T Also include in T the cycle until all vertices
next vertex and call it b. are in T. The resulting
Step 2: Find the edge of subgraph T is a
smallest weight incident to minimum spanning
either a or b. Include in T that tree.
edge and the next incident
vertex. Call that vertex c.
2. Kruskal’s algorithm
Step 1: Find the edge in Step 3: Repeat Step 2 until
the graph with smallest you reach out to every vertex
weight (if there is more of the graph. The chosen
than one, pick one at edges form the desired
random). Mark it with any
given color, say red. minimum spanning tree.
Step 2: Find the next
edge in the graph with
smallest weight that
doesn't close a cycle.
Color that edge and the
next incident vertex.
7.5 Binary trees
A binary tree is a tree
where each vertex has
zero, one or two children
Full binary tree
2: In-order traversal
More on tree traversals
3: Post-order traversal
4: Reverse post-order
traversal
Arithmetic expressions
Standard: infix form
(A+B) C – D/ E
Fully parenthesized form (in-
order & parenthesis):
(((A + B) C) – (D / E))
Postfix form (reverse Polish
notation):
A B + C D E / -
Prefix form (Polish notation):
- + A B C / D E
7.7 Decision trees
A decision tree is a binary tree containing an
algorithm to decide which course of action to
take.
7.8 Isomorphism of trees
Given two trees T1 and T2
T is isomorphic to T
1 2
if we can find a one-to-one and
onto function f :T1 → T2
that preserves the adjacency
relation
i.e. if v, w V(T1) and e = (v, w) is an
edge in T1, then e’ = (f(v), f(w)) is an
edge in T2.
Isomorphism of rooted trees
Let T1 and T2 be rooted trees with roots r1 and r2,
respectively. T1 and T2 are isomorphic as
rooted trees if
there is a one-to-one function f: V(T ) V(T )
1 2
such that vertices v and w are adjacent in T 1 if
and only if f(v) and f(w) are adjacent in T 2
f(r1) = r2
Example:
T and T are isomorphic
1 2
as rooted trees
Isomorphism of binary trees
Let T1 and T2 be binary trees with roots r1 and r2,
respectively. T1 and T2 are isomorphic as binary
trees if
a) T1 and T2 are isomorphic as rooted trees
through an isomorphism f, and
b) v is a left (right) child in T1 if and only if f(v) is
a left (right) child in T2
Note: This condition is more restrictive that isomorphism only as
rooted trees. Left children must be mapped onto left children
and right children must be mapped onto right children.
Binary tree isomorphism
Example: the following two trees are
isomorphic as rooted trees, but
not isomorphic as binary trees
Summary of tree isomorphism
There are 3 kinds of tree isomorphism
Isomorphism of trees
Isomorphism of rooted trees
(root goes to root)
Isomorphism of binary trees
(left children go to left children, right children go to
right children)
Non-isomorphism of trees
Many times it may be easier to determine
when two trees are not isomorphic rather than
to show their isomorphism.
A tree isomorphism must respect certain
properties, such as
the number of vertices
the number of edges
the degrees of corresponding vertices
roots must go to roots
position of children, etc.
Non-isomorphism of rooted trees