0% found this document useful (0 votes)
4 views18 pages

Unit 5 Trees

The document covers various concepts in graph theory, focusing on trees, spanning trees, and algorithms for traversing and constructing trees. It explains types of trees such as rooted trees, binary trees, and complete binary trees, along with algorithms like Breadth First Search (BFS) and Depth First Search (DFS) for finding spanning trees. Additionally, it discusses weighted graphs and minimal spanning trees, detailing Kruskal's and Prim's algorithms for their construction.

Uploaded by

vanithatellakula
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)
4 views18 pages

Unit 5 Trees

The document covers various concepts in graph theory, focusing on trees, spanning trees, and algorithms for traversing and constructing trees. It explains types of trees such as rooted trees, binary trees, and complete binary trees, along with algorithms like Breadth First Search (BFS) and Depth First Search (DFS) for finding spanning trees. Additionally, it discusses weighted graphs and minimal spanning trees, detailing Kruskal's and Prim's algorithms for their construction.

Uploaded by

vanithatellakula
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/ 18

Unit 5

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.

COMPLETE BINARY TREE:


A rooted tree T is called complete
binary tree if each internal vertex T is of out degree 2
that is if every vertex has two children.
SPANNING TREE:
Let G be connected graph and T be any sub graph of G is said to be spanning tree if
 T is tree
 T contains all vertices of G
or
 A spanning tree is a connected graph using all vertices in which there are no
circuits.
 In other words, there is a path from any vertex to any other vertex, but no
circuits.
Ex:

 to obtain the spanning tree of graph G we use two algorithms


A. Breadth First Search (BFS)
B. Depth First Search (DFS)
A. BREADTH FIRST SEARCH (BFS)
 The idea of BFS is to visit all vertices on a given level before going into the
next level until are visited.
ALGORITHM:
Step 1: Arbitrarily choose a vertex and designate it as the root. Then add all edges
incident to this such that the addition of edges does not produce any cycle.
Step 2: The new vertices added at this stage become the vertices at level 1 in the
spanning tree, trarily order them.
Step 3: Next, for each vertex at level 1, visited in order, add each edge incident to this
vertex to tree as long as it does not produce any cycle.
Step 4: Arbitrarily order the children of each vertex at level 1. This produces the
vertices at level the tree.
Step 5: Continue the same procedure until all the vertices in the tree have been added.
Step 6: The procedure ends, since there are only a finite number of edges in the graph.
Step 7: A spanning tree is produced since we have produced a tree without cycle
containing every x of the graph.
Application of BFS
 Finding the shortest path
 Minimum spanning tree for un-weighted graph
 GPS navigation system
 Detecting cycles in an undirected graph
 Finding all nodes within one connected component
Ex:
Consider the following graph:

A breadth first search proceeds as follows:


Step 1:
1. Start Root a
2. Visit b, d (the vertices at distance 1 from root a) Level -1
3. Visit c, e, h (the vertices at distance 2 from root a) Level -2
4. Visit f, k, i (the vertices at distance 3 from root a) Level -3
5. Visit g, j, m (the vertices at distance 4 from root a) Level -4
6. Visit L (the vertices at distance 5 from root a) Level -5
Step 2: Draw the edges from root to level 1 vertices

Step 3: Draw the edges from level 1 to level 2 vertices

Step 4: Draw the edges from level 2 to level 3 vertices

Step 5: Draw the edges from level 3 to level 4 vertices


Step 6: Draw the edges from level 4 to level 5 vertices

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

Step 2: From last vertex J if possible visit I otherwise backtrack to vertex k


Step 3: From vertex k if possible visit I otherwise backtrack to vertex h
Step 4: From vertex h if possible visit I if loop exist omit edge

Step 5: From vertex h if possible visit g if loop exist omit edge

Step 6: From vertex h if possible visit d otherwise backtrack to vertex f


Step 7: From vertex f if possible visit d

Step 8: From vertex f if possible visit b otherwise back track to vertex e


Step 9: From vertex e if possible visit b otherwise back track to vertex c
Step 10: From vertex c if possible visit b

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

 It is used to find the minimum spanning tree for a connected weighted


graph.
 The main target of the algorithm is to find the subset of edges by using
which we can traverse every vertex of the graph
Procedure:
Step 1: First, sort all the edges from low weight to high.
Step 2: Now, take the edge with the lowest weight and add it to the spanning tree. If
the edge to be added creates a cycle, then reject the edge.
Step 3: Continue to add the edges until we reach all vertices, and a minimum
spanning tree is created.
Example of Kruskal's algorithm
Suppose a weighted graph is –

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

Now, let's start constructing the minimum spanning tree.


Step 1 - First, add the edge AB with weight 1 to the MST

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 -

The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.


Now, the number of edges in the above tree equals the number of vertices minus 1.
So, the algorithm stops here.
Prim's Algorithm:
 Prim, is a greedy algorithm that finds a minimum spanning tree 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.
Algorithm
 Step 1: Initialize the minimal spanning tree with a single vertex, randomly
chosen from the graph.
 Step 2: Repeat steps 3 and 4 until all the vertices are included in the tree.
 Step 3: Select an edge that connects the tree with a vertex not yet in the tree, so
that the weight of the edge is minimal and inclusion of the edge does not form a
cycle.
 Step 4: Add the selected edge and the vertex that it connects to the tree.
Ex: Suppose we want to find minimum spanning tree for the following graph G using
Prim’s algorithm.

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 ∞ ____

Step2: Here we start with the vertex ‘a’ and proceed.


Step3: add edge form C that is we add edge CB with minimum weight 1

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:

You might also like