0% found this document useful (0 votes)
5 views31 pages

DS Unit 6

Uploaded by

v3781289
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views31 pages

DS Unit 6

Uploaded by

v3781289
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Discrete Mathematics

Tree

Dr. S. D. Padiya
padiyasagar@gmail.com
SSGMCE, Shegaon
Batch
Batch Syllabus
2
2
0 Tree Searching,
0
2

2
4  Minimal spanning trees,
-
1  Simple Precedence Grammars,
2
-
0
 Rooted tree,
2
2
5  Expression tree,
2
 B-tree,

 Distance between spanning trees of a graph.


 PERT and Related Techniques.

SDPadiya,
SSGMCE
Batch
Batch Tree
2
2
0 Tree is a discrete structure that represents hierarchical relationships between
0
2

2
4 individual elements or nodes. A tree in which a parent has no more than two
-
1 children is called a binary tree.
2
-
0
2
2
5 Definition:
2
A Tree is a connected acyclic undirected graph. There is a unique path between
every pair of vertices in G. A tree with N number of vertices contains (N−1)
number of edges. The vertex which is of 0 degree is called root of the tree. The
vertex which is of 1 degree is called leaf node of the tree and the degree of an
internal node is at least 2.
SDPadiya,
SSGMCE
Batch
Batch Tree
2
2
0  In graph theory, a tree is an undirected, connected and acyclic graph. In other
0
2

2
4 words, a connected graph that does not contain any single cycle is called a tree.
-
1  A tree represents hierarchical structure in a graphical form.
2
-
0
2  The elements of trees are called their nodes and the edges of the tree are
2
5
2 called branches.

 A tree with n vertices has (n-1) edges.

 Trees provide many useful applications as simple as a family tree to as complex


as trees in data structures of computer science.

 A leaf in a tree is a vertex of degree 1 or any vertex having no children is called


SDPadiya, a leaf.
SSGMCE
Batch
Batch Tree
2
2
0 Example:
0
2

2
4
-
1
2
-
0
2
2
5
2

SDPadiya,
SSGMCE
Batch
Batch Tree: Properties
2
2
0 1. Every tree which has at least two vertices should have at least two leaves.
0
2

2
4 2. Trees have many characterizations:
-
1
2 Let, T be a graph with n vertices, then the following statements are true.
-
0
2
2  T is a tree.
5
2  T contains no cycles.

 T is connected and has (n -1) edge.

 T is connected graph, and every edge is a cut-edge.

 Any two vertices of graph T are connected by exactly one path.

 T contains no cycles, and for any new edge e, the graph T+e has exactly
SDPadiya,
SSGMCE one cycle.
Batch
Batch Tree: Properties
2
2
0 3. Adding one edge to a tree defines exactly one cycle.
0
2

2
4 4. Every connected graph contains a spanning tree.
-
1
2 5. Every tree has at least two vertices of degree two.
-
0
2
2
5
2

SDPadiya,
SSGMCE
Batch
Batch Tree:
2
2
0 1) Directed Trees: A directed tree is an acyclic directed graph. It has one node
0
2

2
4 with in-degree 0, while all other nodes have in-degree 1 as shown in fig:
-
1
2
-
0
2
2
5
2

SDPadiya,
SSGMCE
Batch
Batch Tree:
2
2
0 2) Ordered Trees: If in a tree at each level, an ordering is defined, then such a
0
2

2
4 tree is called an ordered tree. The trees shown in the figures represent the same
-
1 tree but have different orders.
2
-
0
2
2
5
2

SDPadiya,
SSGMCE
Batch
Batch Tree:
2
2
0 3) Rooted Trees: If a directed tree has exactly one node or vertex called root
0
2

2
4 whose incoming degrees is 0 and all other vertices have incoming degree one,
-
1 then the tree is called rooted tree.
2
-
0
2
2
5
2

SDPadiya,
SSGMCE
Batch
Batch Tree:
2
2
0 4) Expression Tree: The expression tree is a
0
2

2
4 binary tree in which each internal node
-
1 corresponds to the operator and each leaf node
2
-
0
corresponds to the operand.
2
2
5 For example expression tree for 3 + ((5+9)*2)
2
There are different types of expression formats:

 Prefix expression

 Infix expression and

 Postfix expression

SDPadiya,
SSGMCE
Batch
Batch Tree:
2
2
0 There are 3 traversal techniques to represent the 3 different expression formats.
0
2

2
4 1) In-order Traversal
-
1
2 We can produce an infix expression by recursively printing out
-
0
2
2 The left expression, the root, and the right expression.
5
2 2) Post-order Traversal

The postfix expression can be evaluated by recursively printing out

The left expression, the right expression and then the root

3) Pre-order Traversal

We can also evaluate prefix expression by recursively printing out:


SDPadiya,
SSGMCE The root, the left expression and the right expression.
Batch
Batch Tree:
2
2
0 B Tree: It is a specialized m-way tree that can be widely used for disk access. A
0
2

2
4 B-Tree of order m can have at most m-1 keys and m children. One of the main
-
1 reason of using B tree is its capability to store large number of keys in a single
2
-
0
node and large key values by keeping the height of the tree relatively small.
2
2
5 A B tree of order m contains all the properties of an M way tree. In addition, it
2
contains the following properties.

 Every node in a B-Tree contains at most m children.

 Every node in except the root and the leaf contain at least m/2 children.

 The root nodes must have at least 2 nodes.

SDPadiya,  All leaf nodes must be at the same level.


SSGMCE
Batch
Batch Tree:
2
2
0 It is not necessary that, all the nodes contain the same number of children but,
0
2

2
4 each node must have m/2 number of nodes.
-
1
2
-
0
A B tree of order 4 is shown in the following image.
2
2
5
2

SDPadiya,
SSGMCE
Batch
Batch Tree:
2
2
0 Path length of a Vertex: The
0
2

2
4 path length of a vertex in a
-
1 rooted tree is defined to be
2
-
0
the number of edges in the
2
2
5 path from the root to the
2
vertex.
Example:
The path length of node b is 1.
The path length of node f is 2.
The path length of node l is 3.
SDPadiya,
SSGMCE The path length of node q is 4.
Batch
Batch Tree:
2
2
0 Binary Search Trees: Binary search Example: The tree shown in fig is a
0
2

2
4 trees have the property that the node binary search tree.
-
1 to the left contains a smaller value
2
-
0
than the node pointing to it and the
2
2
5 node to the right contains a larger
2
value than the node pointing to it.

It is not necessary that a node in a


'Binary Search Tree' point to the nodes
whose value immediately precede and
SDPadiya,
SSGMCE follow it.
Batch
Batch Tree:
2
2
0 Inserting into a Binary Search Tree: Consider a binary tree T. Suppose we have
0
2

2
4 given an ITEM of information to insert in T. The ITEM is inserted as a leaf in the
-
1 tree. The following steps explain a procedure to insert an ITEM in the T.
2
-
0
2
2 1) Compare the ITEM with the root node.
5
2
2) If ITEM>ROOT NODE, proceed to the right child, and it becomes a root node for
the right subtree.
3) If ITEM<ROOT NODE, proceed to the left child.
4) Repeat above steps until we meet a node which has no left and right subtree.
5) Now, if the ITEM is greater than the node, the ITEM is inserted as right child,
SDPadiya, and if the ITEM is less than the node, the ITEM is inserted as left child.
SSGMCE
Batch
Batch Tree:
2
2
0 Example: Show the binary search tree after inserting 3, 1,4,6,9,2,5,7 into an initially
0
2

2
4 empty binary search tree.
-
1 Solution: The insertion of the above nodes in the empty binary search tree is
2
-
0
shown in fig:
2
2
5
2

SDPadiya,
SSGMCE
Batch
Batch Tree:
2
2 Example: Show the binary search tree after inserting 3, 1,4,6,9,2,5,7 into an initially
0
0
2
empty binary search tree.
2
4
-
1
2
-
0
2
2
5
2

SDPadiya,
SSGMCE
Batch
Batch Spanning Tree:
2
2 A spanning tree is a sub-graph of an undirected connected graph, which includes
0
0
2
all the vertices of the graph with a minimum possible number of edges. If a vertex
2
4
- is missed, then it is not a spanning tree.
1
2
-
0
2
2
5 The edges may or may not have weights assigned to them.
2

The total number of spanning trees with n vertices that can be created from a
complete graph is equal to n(n-2).
If we have n = 4, the maximum number of possible spanning trees is equal to 44-2 =
16. Thus, 16 spanning trees can be formed from a complete graph with 4 vertices.
SDPadiya,
SSGMCE
Batch
Batch Spanning Tree:
2
2 Example of a Spanning Tree
0
0
2
Let's understand the spanning tree with examples below:
2
4
-
1 Let the original graph be: Some of the possible spanning trees that can
2
-
0
be created from the above graph are:
2
2
5 A B A B A B
2

D C D C D C
SDPadiya, Original Tree Spanning Tree
SSGMCE
Spanning Tree
Batch
Batch Spanning Tree:
2
2 Example of a Spanning Tree
0
0
2
Some of the possible spanning trees that can be created from the above graph are:
2
4
-
1
2 A B A B A B
-
0
2
2
5
2

D C D C D C
Original Tree Spanning Tree Spanning Tree

SDPadiya,
SSGMCE
Batch
Batch Spanning Tree:
2
2 Example of a Spanning Tree
0
0
2
Let's understand the spanning tree with examples below:
2
4
-
1 Let the original graph be: Some of the possible spanning trees that can
2
-
0
be created from the above graph are:
2
2
5 A B A B A B
2

D C D C D C
SDPadiya, Original Tree Spanning Tree
SSGMCE
Spanning Tree
Batch
Batch Spanning Tree:
2
2 A B A B A B A B
0
0
2

2
4
-
1
2
-
0
2
2
5
2
D Original Tree C D Spanning Tree C D Spanning Tree C D Spanning TreeC
A B A B A B A B

SDPadiya,
SSGMCE D C D C D C D C
Batch
Batch Spanning Tree: A B A B A B
2
2 A B
0
0
2

2
4
-
1
2
-
0
2
2
5
2 D C D C D C
D Original Tree C
A B A B A B A B

SDPadiya,
SSGMCE D C D C D C D C
Batch
Batch Minimum Spanning Tree:
2
2 A minimum spanning tree is a spanning tree in which the sum of the weight of the
0
0
2
edges is as minimum as possible.
2
4
- Example of a Spanning Tree
1
2
-
0 Let's understand the above definition with the help of the example below.
2
2
5 The initial graph is:
2 B B B
A 5 A A 5

2 6 4 2 4 2

3 3 3
SDPadiya, D C D C D C
SSGMCE Original Tree ST (9) ST(10)
Batch
Batch Minimum Spanning Tree:
2
2
0
0
2

2
4
-
A 5 B A 5 B A 5 B
1
2
-
0
2
2
5 4
2 2 6 4 2 4

ST

3 D C 3
D C D C
Original Tree ST (11) ST (12)

SDPadiya,
SSGMCE
Batch
Batch Minimum Spanning Tree:
2
2
0
0
2

2
4
- B A 5 B A B
1 A 5
2
-
0
2
2
5
4 6 2 6 4
2 2 6

3 3
D C D C D C
Original Tree ST (14) ST (12)

SDPadiya,
SSGMCE
Batch
Batch Minimum Spanning Tree:
2
2
0 A minimum spanning tree (MST) or minimum weight spanning tree is a subset
0
2

2
4 of the edges of a connected, edge-weighted undirected graph that connects all
-
1 the vertices together, without any cycles and with the minimum possible total
2
-
0
edge weight. That is, it is a spanning tree whose sum of edge weights is as small
2
2
5 as possible.
2
More generally, any edge-weighted undirected graph (not necessarily
connected) has a minimum spanning forest, which is a union of the minimum
spanning trees for its connected components.
Uniqueness
If each edge has a distinct weight then there will be only one, unique minimum
SDPadiya,
SSGMCE spanning tree.
Batch
Batch Minimum Spanning Tree:
2
2
0 Kruskal's Algorithm
0
2

2
4 It is a greedy algorithm that finds a MST for a connected weighted graph. It finds
-
1 a tree of that graph which includes every vertex and the total weight of all the
2
-
0
edges in the tree is less than or equal to every possible spanning tree.
2
2
5
2
Prim's Algorithm
It is also a greedy algorithm that finds a MST for a connected weighted graph. It
finds a tree of that graph which includes every vertex and the total weight of all
the edges in the tree is less than or equal to every possible spanning tree. Prim’s
algorithm is faster on dense graphs.
SDPadiya,
SSGMCE
Thank You
Dr. S. D. Padiya
padiyasagar@gmail.com
SSGMCE, Shegaon

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy