discrete structures trees (ch. 11) dr. muhammad humayoun assistant professor comsats institute of...
TRANSCRIPT
1
Discrete StructuresTrees (Ch. 11)
Dr. Muhammad HumayounAssistant Professor
COMSATS Institute of Computer Science, [email protected]
https://sites.google.com/a/ciitlahore.edu.pk/dstruct/Modified slides of Dr. M. Atif
2
First we need
some definitions from Graph theory
3
Path
• A path is a sequence of edges that begins at a vertex of a graph and travels from vertex to vertex along edges of the graph.
• Formal Definition: Let n Z+ ∈ and G an undirected graph. A path of length n from vertices u to v in G is a sequence of n edges e1 , ..., en of G for which there exists a sequence x0=u, x1, . . . , xn−1, xn=v of vertices.
4
Example
• Find a path between a and e.• a, d, c, f , e is a simple path of length 4, because {a, d}, {d, c},
{c, f }, and {f, e} are all edges.• a, e is a simple path of length 1, because {a, e} is the edge
connecting both vertices.• a, b, f , e is a simple path of length 3, because {a, b}, {b, f},
and {f, e} are all edges.• …
5
Circuit
• The path is a circuit if it begins and ends at the same vertex, that is, if u = v, and has length greater than zero.
Path a, b, c, d, a is a circuit.
A path or circuit is simple if it does not contain the same edge more than once.
6
Connected Graph
• An undirected graph is called connected if there is a path between every pair of distinct vertices of the graph.
• The graph G1 is connected.• The graph G2 is not
connected. (For instance, there is no path in G2 between vertices a and d.)
7
Trees• A (free) tree is an undirected graph T such that– T has no simple circuits– T is connected
• A tree cannot contain multiple edges or loops. Therefore any tree must be a simple graph.
• Theorem: An undirected graph is a tree iff there is a unique simple path between any two of its vertices.
Tree
8
• Which of the graphs are trees?• G1 and G2 are trees, because both are connected
graphs with no simple circuits.• G3 is not a tree because e, b, a, d, e is a simple circuit
in this graph. • G4 is not a tree because it is not connected.
9
Forest
• A forest is an undirected graph such that– It has no simple circuits– It is not necessarily connected
• The connected components of a forest are trees
ForestOne graph with 3 trees
10
11
Rooted Tree• A rooted tree is a tree in which one vertex has
been designated as the root and every edge is directed away from the root.
• Root, parent, child, siblings relations between vertices
Tree and Rooted Tree
12
SubtreeIf a is a vertex in a tree, the subtree with a as its root is the subgraph of the tree consisting of a and its descendants and all edges connected to these descendants.
13
Spanning Tree
• A spanning tree of a connected graph is a spanning subgraph that is a tree
14
Spanning tree of the simple graph
• it is not a tree because it contains three simple circuits.
• Which ones?– {a, e}, {e,f} and {c,g}
• Spanning tree:• Can we produce more
spanning trees?
15
16
Some facts
• A simple graph is connected if and only if it has a spanning tree.
• A spanning tree is not unique unless the graph is a tree
• Spanning trees have applications to the design of communication networks
17
Tree Traversal
• Ordered rooted trees are often used to store information.
• We need procedures for visiting each vertex of an ordered rooted tree to access data.
• We will study some important algorithms for visiting all the vertices of an ordered rooted tree.
• Ordered rooted trees can also be used to represent various types of expressions.
18
Ordered Rooted Tree• An ordered rooted tree is a rooted tree where
the children of each internal vertex are ordered.
19
Traversal Algorithms
• Procedures for systematically visiting every vertex of an ordered rooted tree are called traversal algorithms.
• Most commonly used algorithms:–Preorder traversal – Inorder traversal–Postorder traversal
• All recursively defined.
20
21
Preorder Traversal
• Let T be an ordered rooted tree with root r. If T consists only of r, then r is the preorder traversal of T .
• Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right in T. The preorder traversal begins by visiting r.
• It continues by traversing T1 in preorder, then T2 in preorder, and so on, until Tn is traversed in preorder
22
23
24
25
26
Procedureprocedure preorder(T:ordered rooted tree)r:= root of TList/print rfor each child e of r from left to right T(e) := subtree with e as its root preorder(T(e))
27
Inorder Traversal
28
Inorder traversal
• Let T be an ordered rooted tree with root r. If T consists only of r, then r is the inorder traversal of T .
• Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right.
• The inorder traversal begins by traversing T1 in inorder, then visiting r. It continues by traversing T2 in inorder, then T3 in inorder, . . . , and finally Tn in inorder.
29
30
31
32
Procedureprocedure inorder (T : ordered rooted tree)r := root of Tif r is a leaf then list/print relse l := first child of r from left to right T (l) := subtree with l as its root inorder(T (l)) list/print r for each child c of r except for l from left to right T (c) := subtree with c as its root inorder(T (c))
33
Postorder Traversal
34
Postorder Traversal
• Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T .
• Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right. The postorder traversal begins by traversing T1 in postorder, then T2 in postorder, . . . , then Tn in postorder, and ends by visiting r.
35
36
37
38
Proceedure
procedure postorder(T : ordered rooted tree)r := root of Tfor each child c of r from left to right T (c) := subtree with c as its root postorder(T (c))list/print r
39
Infix, Prefix, and Postfix Notation
• We can represent complicated expressions, such as compound propositions, combinations of sets, and arithmetic expressions using ordered rooted trees.
40
Ordered rooted tree of an Expression
• ((x + y) ↑ 2) + ((x − 4)/3)
41
Infix notation
• It is the common arithmetic and logical formula notation, in which operators are written between the operands they act on (e.g. 2 + 2).
• Inorder traversal.
42
Prefix form
• Also known as Polish notation.• A form of notation for logic, arithmetic, and
algebra.• It places operators to the left of their operands.• (5 − 6) * 7 Infix notation• * - 5 6 7 Prefix notation• 5 − (6 * 7) Infix notation• − 5 * 6 7 Prefix notation
43
Prefix form
• What is prefix form of ((x + y) ↑ 2) + ((x − 4)/3)?• + ↑ + x y 2 / − x 4 3.
• Preorder traversal
44
Evaluating a prefix form
What is the value of the prefix expression + − 2 3 5∗ /↑ 2 3 4?
45
Postfix form
• Also known as Reverse Polish Notation.• We obtain the postfix form of an expression by
traversing its tree in postorder.
46
Example
What is the postfix form of the expression: ((x + y) ↑ 2) + ((x - 4)/3) ?
Postorder Traversal
x y + 2 ↑ x 4 - 3 / +
47
Evaluating a Postfix Expression
Postfix expression: 7 2 3 * - 4 t 9 3/+
48
Representing other types of expressions with Rooted trees
Compound proposition: (¬ (p q)) ↔ (∧ ¬ p ∨¬ q).
49
Binary Trees• Binary rooted Trees• A binary tree is a tree data structure in which
each node has at most two child nodes, usually distinguished as "left" and "right".
50
END