Unit 5 Trees
Unit 5 Trees
GRAPH THEORY
TREE:
A Graph G (V.E) is said to be tree if
G is connected
G has no cycles
ROOTED TREE:
A rooted tree is a graph that has a single
node as the starting point. All of the other nodes in the
tree are connected to the root either directly or indirectly.
In The Above Graph
Root –1
Level 1 Vertices – 2,7
Level 2 Vertices – 4,3,8,9
Level 3 Vertices – 5, 6
BINARY TREE:
A rooted tree T is called binary tree if
each internal vertex T is of out degree 1 or 2 that is if
every vertex has at most two children.
Which is the required spanning tree with 12 vertices have 11 edges of given graph
B. Depth First Search
Depth First Search (DFS) algorithm starts from a vertex then it
traverses to its adjacent vertex (say x) that has not been visited before and marks as
"visited" and goes on with the adjacent vertex of and so on.
If at any vertex, it encounters that all the adjacent vertices are
visited, then it backtracks until it finds the first vertex having an adjacent vertex that
has not been traversed before. Then, it traverses that vertex, continues with its
adjacent vertices until it traverses all visited vertices and has to backtrack again. In
this way, it will traverse all the vertices reachable from the initial vertex
DEPTH FIRST SEARCH (DFS) ALGORITHM:
Step 1: Arbitrarily choose a vertex from the vertices of the graph and designate it as
the root
Step 2: Form a path starting at this vertex by successively adding edges as long as
possible where each new edge is incident with the last vertex in the path without
producing any cycle.
Step 3: If the path goes through all vertices of the graph, the tree consisting of this
path spanning tree. Otherwise, move back to the next to last vertex in the path, and, if
possible, form a new starting at this vertex passing through vertices that were not
already visited.
Step 4: If this cannot be done, move back another vertex in the path, that is two
vertices bad the path, and repeat.
Step 5: Repeat this procedure, beginning at the last vertex visited, moving back up the
go vertex at a time, forming new paths that are as long as possible until no more edges
can be add
Step 6: This process ends since the graph has a finite number of edges and it is
connected spanning tree is produced.
Applications of DFS
The applications of using the DFS algorithm are given as follows -
DFS algorithm can be used to implement the topological sorting.
It can be used to find the paths between two vertices.
It can also be used to detect cycles in the graph.
DFS algorithm is also used for one solution puzzles.
DFS is used to determine if a graph is bipartite or not.
Ex:
This approach is called a depth first search. Consider the following graph:
A depth first search proceeds as follows:
Step1: start at a and visit remaining vertices as much as possible without cycles, if all
vertices are covered stop the procedure otherwise go to next step
Here above graph is connected and g has no cycles contains all vertices of graph G
Hence it is spanning tree with 11 vertices has 10 edges.
Weighted Graph:
A graph is said to be weighted graph if each edge
in G associated with numerical value.
Minimal Spanning Tree: A spanning Tree is said to be
Minimal Spanning tree if the total weight of spanning tree is
minimum among all spanning trees of graph.
Minimal spanning Tree can obtain by using fallowing algorithms
1. Kruskal’s Algorithm
The weight of the edges of the above graph is given in the below table -
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2
Now, sort the edges given above in the ascending order of their weights.
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
Step 2 - Add the edge DE with weight 2 to the MST as it is not creating the cycle.
Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or
loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not forming the
cycle.
Step 5 - After that, pick the edge AE with weight 5. Including this edge will create the
cycle, so discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so
discard it.
Step 7 - Pick the edge AD with weight 10. Including this edge will also create the
cycle, so discard it.
So, the final minimum spanning tree obtained from the given weighted graph by using
Kruskal's algorithm is -
Sol:
Step1: construct 6× 6 matrix with weight. In the matrix diagonal elements with “__”
and if two vertices are no edge replace weight by “∞”
A B C D E F
A ____ 20 9 13 ∞ ∞
B 20 ____ 1 ∞ 4 5
C 9 1 ____ 2 ∞ ∞
D 13 ∞ 2 ____ 3 14
E ∞ 4 ∞ 3 ____ ∞
F ∞ 5 ∞ 14 ∞ ____
Step4: add edge form B that is we add edge BE with minimum weight 4
Step5: add edge form E that is we add edge ED with minimum weight 3
Step6: add edge form D that is we add edge DF with minimum weight 14 since DA
with minimum weight 13 form loop so we select next minimum weight 14 that is DF
edge
This is the minimal spanning tree and its total weight is (9+1+4+3+14) = 31
TREE TRAVERSAL:
Traversing means to visit all the nodes of the tree. There are three
standard methods to traverse the binary trees. These are as follows:
1. Pre order Traversal
2. Post order Traversal
3. In order Traversal
Pre order Traversal: The pre order traversal of a binary tree is a recursive process.
The preorder traversal of a tree is
Visit the root of the tree.
Traverse the left sub tree in preorder.
Traverse the right sub tree in preorder.
Post order Traversal: The post order traversal of a binary tree is a recursive process.
The post order traversal of a tree is
Traverse the left sub tree in post order.
Traverse the right sub tree in post order.
Visit the root of the tree.
In order Traversal: The in order traversal of a binary tree is a recursive process. The
in order traversal of a tree is
Traverse in in order the left sub tree.
Visit the root of the tree.
Traverse in in order the right sub tree.
Ex:
1. Determine the pre order, post order and in order traversal of the binary tree as
shown in fig:
The pre order, post order and in order traversal of the tree is as follows:
Pre order 1 2 3 4 5 6 7 8 9 10 11
Post order 3 5 4 2 7 10 9 11 8 6 1
In order 3 2 5 4 1 7 6 9 10 8 11
Algorithms:
(a)Algorithm to draw a Unique Binary Tree when Inorder and Preorder
Traversal of the tree is Given:
1. We know that the root of the binary tree is the first node in its preorder. Draw
the root of the tree.
2. To find the left child of the root node, first, use the inorder traversal to find the
nodes in the left subtree of the binary tree. (All the nodes that are left to the
root node in the inorder traversal are the nodes of the left subtree). After that,
the left child of the root is obtained by selecting the first node in the preorder
traversal of the left subtree. Draw the left child.
3. In the same way, use the inorder traversal to find the nodes in the right subtree
of the binary tree. Then the right child is obtained by selecting the first node in
the preorder traversal of the right subtree. Draw the right child.
4. Repeat the steps 2 and 3 with each new node until every node is not visited in
the preorder. Finally, we obtain a unique tree.
Ex:
Draw the unique binary tree when the inorder and preorder traversal is given as
follows:
Inorder B A D C F E J H K G I
Preorder A B C D E F G H J K I
Solution:
We know that the root of the binary tree is the first node in preorder traversal.
Now, check A, in the in order traversal, all the nodes that are of left A, are
nodes of left subtree and all the nodes that are right of A, are nodes of right
subtree.
Read the next node in the preorder and check its position against the root node,
if its left of the root node, then draw it as a left child, otherwise draw it a right
child.
Repeat the above process for each new node until all the nodes of the preorder
traversal are read.
finally, we obtain the binary tree as shown in fig:
b) Algorithm to draw a Unique Binary Tree when Inorder and Postorder
Traversal of the tree is Given:
1. We know that the root of the binary tree is the last node in its postorder. Draw
the root of the tree.
2. To find the right child of the root node, first, use the inorder traversal to find
the nodes in the right subtree of the binary tree. (All the nodes that are right to
the root node in the inorder traversal are the nodes of the right subtree). After
that, the right child of the root is obtained by selecting the last node in the
postorder traversal of the right subtree. Draw the right child.
3. In the same way, use the inorder traversal to find the nodes in the left subtree of
the binary tree. Then the left child is obtained by selecting the last node in the
postorder traversal of the left subtree. Draw the left child.
4. Repeat the steps 2 and 3 with each new node until every node is not visited in
the postorder. After visiting the last node, we obtain a unique tree.
Example: Draw the unique binary tree for the given Inorder and Postorder traversal is
given as follows:
Inorder 4 6 10 12 8 2 1 5 7 11 13 9 3
Postorder 12 10 8 6 4 2 13 11 9 7 5 3 1
Sol:
We know that the root of the binary tree is the last node in the postorder
traversal. Hence, one in the root node.
Now, check the inorder traversal, we know that root is at the center, hence all
the nodes that are left to the root node in inorder traversal are the nodes of left
subtree and, all that are right to the root node are the nodes of the right subtree.
Now, visit the next node from back in postorder traversal and check its position
in inorder traversal, if it is on the left of root then draw it as left child and if it is
on the right, then draw it as the right child.
Repeat the above process for each new node, and we obtain the binary tree as
shown in fig: