Download Trees-Discrete Mathematics-Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!
Trees
Discrete Mathematics
What is a Tree?
• Tree:
– A tree is a connected graph without cycles. A tree
with (precisely) n vertices has (precisely) n−1 edges.
Tree Representation
• Trees are structures used to represent hierarchical
relationship
• Each tree consists of nodes and edges
– Each node represents an object
– Each edge represents the relationship between two
nodes.d
edge
node
Applications of Trees
President
VP
Organization Chart
Expression Tree
VP Personnel
VP Marketing
Director Customer Relation
Director Sales
docsity.com
Trees Terminology
• For any two nodes u and v, if there is an edge pointing
from u to v, u is called the parent of v while v is called
the child of u. Such edge is denoted as (u, v).
• In a tree, there is exactly one node without parent,
which is called the root.
5
u
v
root
u: parent of v v: child of u
leaves
Trees Terminology
• In a tree, the nodes without children are called leaves.
Otherwise, they are called internal nodes.
internal nodes
leaves
Trees Terminology
• If two nodes have the same parent, they are siblings.
• A node u is an ancestor of v if u is parent of v or parent
of parent of v or …
• A node v is a descendent of u if v is child of v or child of
child of v or …
u
v w
x
v and w are siblings u and v are ancestors of x v and x are descendents of u
Trees Terminology
• A subtree is any node together with all its descendants.
T
v (^) v
A subtree of T
docsity.com
Ways to Traverse a Tree
• There are three main ways to traverse a tree:
– Pre-order:
- (1) visit node, (2) recursively visit left subtree, (3) recursively visit right subtree
– In-order:
- (1) recursively visit left subtree (2) visit node (3)• (1) recursively visit left subtree, (2) visit node, (3) recursively right subtree
– Post-order:
- (1) recursively visit left subtree, (2) recursively visit right subtree, (3) visit node
– Level-order:
- Traverse the nodes level by level
Examples for expression tree
• By pre-order, (prefix)
• By in-order, (infix)
• By post-order, (postfix)
• By level-order
Spanning Trees
• Spanning Tree:
– Let G be a connected graph. A spanning tree for G is
a tree T such that the vertices of T are exactly the
vertices of G and such that every edge of T is also an
edge of G.
Spanning Trees
• Spanning Tree:
– The graph
– has some of the following spanning trees
docsity.com
Spanning Trees
• Find Spanning Tree (Pseudocode):
- pick an initial node and call it part of the spanning tree
- do a search from the initial node:
- each time you find a node that is not in the spanning tree, add to the spanning tree both the new node and the edge you followed to get to it
An undirected graph One possible result of a BFS starting from top
One possible result of a DFS starting from top
Spanning Trees
Example:
• Find all spanning trees of the following graph
Spanning Trees
• Minimum Spanning Tree:
– A MST is a subgraph of an undirected weighted graph
G, such that
- it is a tree (i.e., it is acyclic)
- it covers all the vertices V (contains |V| - 1 edges)
- the total cost associated with tree edges is the minimumthe total cost associated with tree edges is the minimum among all possible spanning trees
- not necessarily unique
Spanning Trees
• Example:
– Suppose you have a connected undirected graph with
a weight (or cost) associated with each edge. The cost
of a spanning tree would be the sum of the costs of its
edges. A minimum-cost spanning tree is a spanning
tree that has the lowest costtree that has the lowest cost
A B
E D
F C
16
19
21 11 33 14 18 10
6 5
A connected, undirected graph
A B
E D
F C
16 11
18
6 5
A minimum-cost spanning tree
docsity.com
Prim's Algorithm
a
b
h
c d
e
g f
i
25
g 1 2
Prim's Algorithm
a
b
h
c d
e
g f
i
26
Prim's Algorithm
a
b
h
c d
e
g f
i
27
g 1 2
Prim's Algorithm
a
b
h
c d
e
g f
i
28
docsity.com
Prim's Algorithm
a
b
h
c d
e
g f
i
29
Prim's Algorithm
a
b
h
c d
e
g f
i
30
g 1 2
Prim's Algorithm
a
b
h
c d
e
g f
i
31
Prim's Algorithm
a
b
h
c d
e
g f
i
32
g 1 2
docsity.com
Kruskal’s Algorithm
• Example:
37
Kruskal’s Algorithm
• Example:
38
Kruskal’s Algorithm
• Example:
39
Kruskal’s Algorithm
• Example:
40
docsity.com
Kruskal’s Algorithm
• Example:
41
Kruskal’s Algorithm
• Example:
42
Kruskal’s Algorithm
• Example:
43
Kruskal’s Algorithm
• Example:
44
docsity.com
Recap
• Graphs can be used to represent many real-life
problems.
• There are numerous important graph algorithms.
• We have studied some basic concepts and
algorithms.l i h
- Graph Traversal
- Topological Sort
- Spanning Tree
- Minimum Spanning Tree
- Shortest Path
49
docsity.com